How to show splash image while loading activity

前端 未结 3 1480
感动是毒
感动是毒 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:25

    Below is the simple code for creating splash screen using CountDownTimer class

    public class SplashDialogActivity extends Activity {
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            // Be sure to call the super class.
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.layout);
          counter.start();
        }
        MyCount counter = new MyCount(5000, 1000);
     public class MyCount extends CountDownTimer{
                public MyCount(long millisInFuture, long countDownInterval) {
                super(millisInFuture, countDownInterval);
                }
    
                @Override
                public void onFinish() {
                    go_back();
                }
    
                @Override
                public void onTick(long millisUntilFinished) {
    
    
    
                  }
         }
    
     public void go_back()
            {
              counter.cancel();
    
                        Intent i=new Intent(this,account.class);
                        i.putExtra("first_time", true);
                        startActivity(i);
    
                this.finish();
            }
    }
    
    0 讨论(0)
  • 2021-01-02 20:26

    use AsyncTask

    put splash in onPreExecute()

    and do your work in doInBackground()

    and close splash in onPostExecute()

    0 讨论(0)
  • 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;
    }    
    
    0 讨论(0)
提交回复
热议问题