閃屏:在打開App時,展示,持續數秒后,自動關閉,進入另外的一個界面,SplashActivity跳轉到MainActivity
Android中有三種實現方法
xml代碼:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.test.SplashActivity"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/splash_iv" android:scaleType="fitXY" android:src="@mipmap/splash"/></RelativeLayout>
(1)利用Handler對象的postDelayed方法可以實現,傳遞一個Runnable對象和一個需要延時的時間即可
new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent=new Intent(SplashActivity.this,MainActivity.class); startActivity(intent); SplashActivity.this.finish(); } },3000);(2)使用動畫持續時間,動畫結束后進行跳轉
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); iv =(ImageView)findViewById(R.id.splash_iv); iv.setImageResource(R.mipmap.splash); //設置透明度動畫從無到有 AlphaAnimation alphaAnimation=new AlphaAnimation(0.0f,1.0f); //設置動畫持續時間 alphaAnimation.setDuration(3000); //開始顯示動畫 iv.startAnimation(alphaAnimation); //給動畫設置監聽,在動畫結束的時候進行跳轉 alphaAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { //動畫開始時執行 Log.e("TAG", "onAnimationStart: " ); } @Override public void onAnimationEnd(Animation animation) { //動畫結束時執行 Log.e("TAG", "onAnimationEnd: " ); Intent intent=new Intent(SplashActivity.this,MainActivity.class); startActivity(intent); finish(); } @Override public void onAnimationRepeat(Animation animation) { //動畫重復播放時執行 Log.e("TAG", "onAnimationRepeat: " ); } }); }(3)利用Timer定時器實現,
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); iv =(ImageView)findViewById(R.id.splash_iv); iv.setImageResource(R.mipmap.splash); Timer timer=new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Intent intent=new Intent(SplashActivity.this,MainActivity.class); startActivity(intent); finish(); } },3000); }以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答