Android 应用页面延缓载入

1.新建一个线程,使用handle的延缓运行线程

复制代码
new Handler().postDelayed(new Runnable() {
                   // 为了减少代码使用匿名Handler创建一个延时的调用
    public void run() {
        Intent i = new Intent(SplashActivity.this, MainActivity.class );
        startActivity(i);
        finish();
        overridePendingTransition(R.anim. in_1, R.anim.out_1);
        }
    }, 2000 );//延缓2秒
复制代码

2.通过handler延缓发送广播

复制代码
public class SplashActivity extends Activity {

       private Handler handler;
       private final int NOTIFY_CLOSE_ACTIVITY = 1;
       private int SPLASH_DISPLAY_LENGHT = 2000; // 延迟两秒

       @Override
       protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash);

            initialize();
            handler.removeMessages(NOTIFY_CLOSE_ACTIVITY);
            handler.sendEmptyMessageDelayed(NOTIFY_CLOSE_ACTIVITY, SPLASH_DISPLAY_LENGHT);
       
      }

       private void initialize() {
             handler = new Handler() {
                   @Override
                   public void handleMessage(Message msg) {
                         // TODO Auto-generated method stub
                         super.handleMessage(msg);
                         switch (msg.what ) {
                         case NOTIFY_CLOSE_ACTIVITY :
                              goMain();
                               break;
                         default:
                               break;
                         }
                  }
            };
      }
      
       private void goMain(){
            Intent intent = new Intent(SplashActivity.this, MainActivity.class );
             this.startActivity(intent);
             this.finish();
      };
}
复制代码

3.使用动画

复制代码
public class SplashActivity extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
            
            View view = View. inflate(this, R.layout. activity_splash, null);
            setContentView(view);
            
             //动画效果参数直接定义
        Animation animation = new AlphaAnimation(0.1f, 1.0f);
        animation.setDuration(5000);
        view.setAnimation(animation);
       
        animation.setAnimationListener( new AnimationListener() {
           
            @Override
            public void onAnimationStart(Animation animation) {
            }
           
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
           
            @Override
            public void onAnimationEnd(Animation animation) {
                goMain();
            }
        });
       
      }
      
       private void goMain(){
            Intent intent = new Intent(SplashActivity.this, MainActivity.class );
             this.startActivity(intent);
             this.finish();
      };
}
复制代码

4.使用CountDownTimer http://www.cnblogs.com/over140/archive/2011/12/20/2294220.html

定时执行在一段时候后停止的倒计时,在倒计时执行过程中会在固定间隔时间得到通知(译者:触发onTick方法)

复制代码
        new CountDownTimer(3000 ,1000){

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
                System.out.println("-------onFinish---------");
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                System.out.println("-------onFinish---------");
            }
            
        }.start();
复制代码

 

posted @   似水流云  阅读(323)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示