Android实现闪屏欢迎界面

闪屏:在打开App时,展示,持续数秒后,自动关闭,进入另外的一个界面,SplashActivity跳转到MainActivity 

Android中有三种实现方法 

xml代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<? 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对象和一个需要延时的时间即可

?
1
2
3
4
5
6
7
8
new Handler().postDelayed( new Runnable() {
      @Override
      public void run() {
        Intent intent= new Intent(SplashActivity. this ,MainActivity. class );
        startActivity(intent);
        SplashActivity. this .finish();
      }
    }, 3000 );

(2)使用动画持续时间,动画结束后进行跳转

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@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定时器实现,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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 );
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值