How to show splash image while loading activity

前端 未结 3 1496
感动是毒
感动是毒 2021-01-02 19:54

I have an activity that contains many UI views. In its onCreate method, I found single line of setContentView takes 8-12 seconds to be complete. So I want t

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-02 20:30

    try this code for splash page

    private Thread mSplashThread;    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.splesh);
    
        final Splash sPlashScreen = this;   
    
         mSplashThread =  new Thread(){
            @Override
            public void run(){
                try {
                    synchronized(this){
                        wait(5000);
                    }
                }
                catch(InterruptedException ex){                    
                }
    
                finish();
    
                Intent intent = new Intent();
                intent.setClass(sPlashScreen,Login.class);
                startActivity(intent);
                stop();                    
            }
        };
    
        mSplashThread.start();        
     }
    
    // Processes splash screen touch events
    @Override
    public boolean onTouchEvent(MotionEvent evt) {
    
         if(evt.getAction() == MotionEvent.ACTION_DOWN)
         {
             synchronized(mSplashThread){
                 mSplashThread.notifyAll();
             }
         }
         return true;
    }    
    

提交回复
热议问题