闪屏:在打开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"><ImageViewandroid: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() {@Overridepublic void run() {Intent intent=new Intent(SplashActivity.this,MainActivity.class);startActivity(intent);SplashActivity.this.finish();}},3000);
(2)使用动画持续时间,动画结束后进行跳转
@Overrideprotected 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() {@Overridepublic void onAnimationStart(Animation animation) {//动画开始时执行Log.e("TAG", "onAnimationStart: " );}@Overridepublic void onAnimationEnd(Animation animation) {//动画结束时执行Log.e("TAG", "onAnimationEnd: " );Intent intent=new Intent(SplashActivity.this,MainActivity.class);startActivity(intent);finish();}@Overridepublic void onAnimationRepeat(Animation animation) {//动画重复播放时执行Log.e("TAG", "onAnimationRepeat: " );}});}
(3)利用Timer定时器实现,
@Overrideprotected 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() {@Overridepublic void run() {Intent intent=new Intent(SplashActivity.this,MainActivity.class);startActivity(intent);finish();}},3000);}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。