zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    条款14:在资源管理类中心copying行为(Think carefully about copying behavior in resource-manage classes)
    matlab ()的用法
    正式学习React(五) Reactjs 的 PropTypes 使用方法
    正式学习 react(三)
    webpack ------require,ensure
    转的git
    Session机制详解
    ES5 object的新函数
    HDU 4635 Strongly connected (强连通分量)
    HDU 4635 Strongly connected (强连通分量)
  • 原文地址:https://www.cnblogs.com/sishuiliuyun/p/3305137.html
Copyright © 2011-2022 走看看