1. 方法一:SplashActivity 作為主 Activity婉弹,定時跳轉(zhuǎn)到 MainActivity:
package com.ttt.zhihudaily.activity;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import com.ttt.zhihudaily.R;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// 動畫
ImageView imageView=(ImageView)findViewById(R.id.splash_image);
TextView textView1=(TextView)findViewById(R.id.splash_text_1);
TextView textView2=(TextView)findViewById(R.id.splash_text_2);
AlphaAnimation imageAnimation=new AlphaAnimation(0.0f,1.0f);
imageAnimation.setDuration(1000);
imageView.setAnimation(imageAnimation);
AlphaAnimation textAnimation=new AlphaAnimation(0.0f,1.0f);
textAnimation.setDuration(300);
textView1.setAnimation(textAnimation);
textView2.setAnimation(textAnimation);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent=new Intent(SplashActivity.this,MainActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
}
},2000);
}
}
2. 方法二:直接在 MainActivity 中搂抒,通過設置 Visibility 實現(xiàn)肆资。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<android.support.v4.widget.DrawerLayout...>
<LinearLayout
android:id="@+id/splash_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white">
<ImageView
android:id="@+id/splash_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:scaleType="centerCrop"
android:src="@drawable/splash_image"/>
<TextView
android:id="@+id/splash_text_1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:layout_weight="1"
android:textSize="40sp"
android:textColor="@color/colorPrimary"
android:text="知乎日報"/>
<TextView
android:id="@+id/splash_text_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:textSize="15sp"
android:textColor="@color/colorPrimary"
android:text="By TTTqiu"/>
</LinearLayout>
</RelativeLayout>
......
private void initSplash(){
// 防止啟動界面時 RelativeLayout 下的主界面會響應事件
drawerLayout.setVisibility(View.INVISIBLE);
ImageView splashImage=(ImageView)findViewById(R.id.splash_image);
TextView splashText1=(TextView)findViewById(R.id.splash_text_1);
TextView splashText2=(TextView)findViewById(R.id.splash_text_2);
AlphaAnimation imageAnimation=new AlphaAnimation(0.0f,1.0f);
imageAnimation.setDuration(1000);
splashImage.setAnimation(imageAnimation);
AlphaAnimation textAnimation=new AlphaAnimation(0.0f,1.0f);
textAnimation.setDuration(1000);
textAnimation.setStartOffset(800);
splashText1.setAnimation(textAnimation);
splashText2.setAnimation(textAnimation);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
LinearLayout splashLayout=(LinearLayout)findViewById(R.id.splash_layout);
AlphaAnimation layoutAnimation=new AlphaAnimation(1.0f,0.0f);
layoutAnimation.setDuration(500);
splashLayout.setAnimation(layoutAnimation);
splashLayout.setVisibility(View.GONE);
drawerLayout.setVisibility(View.VISIBLE);
}
},2500);
}
......
最后編輯于 :
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者