2017-09-25 51 views
0

我創建了一個Android應用程序。最近我爲它添加了一個SplashScreen。但如果我運行的應用程序沒有重定向到主應用程序,動畫正常播放。但是當它被重定向到主要活動時,啓動畫面根本看不到。它直接啓動主要活動。有人可以摒棄它的原因嗎?爲什麼在Flash屏幕在Android中完成其動畫之前重定向到下一個活動?

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     StartAnimations(); 


/* If comment these lines animations works fine, */ 
     Intent mainIntent = new Intent(SpalshScreenActivity.this,MainActivity.class); 
     startActivity(mainIntent); 
     finish(); 

    } 


private void StartAnimations() { 
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha); 
    anim.reset(); 
    LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay); 
    l.clearAnimation(); 
    l.startAnimation(anim); 

    anim = AnimationUtils.loadAnimation(this, R.anim.translate); 
    anim.reset(); 
    ImageView iv = (ImageView) findViewById(R.id.logo); 
    iv.clearAnimation(); 
    iv.startAnimation(anim); 


} 

我可以通過添加下面的線程來避免此行爲。

Thread timer = new Thread() { 
      @Override 
      public void run() { 
       super.run(); 
       try { 
        Thread.sleep(2200); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } finally { 
        Intent mainIntent = new Intent(SpalshScreenActivity.this, MainActivity.class); 
        startActivity(mainIntent); 
        finish(); 
       } 
      } 
     }; 
     timer.start(); 

但我想知道這種行爲的原因。

回答

-1

您可以使用AnimationListener,實現下面聽衆與你的動畫和動畫一旦完成,你就可以開始新的活動

anim.setAnimationListener(new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 

    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     Intent mainIntent = new Intent(SpalshScreenActivity.this, MainActivity.class); 
     startActivity(mainIntent); 
     finish(); 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 

    } 
}); 
+0

是什麼張貼相同的答案我 –

+0

@TimCastelijns爲了您的實物資料先生點蒂姆。我們同時發佈了答案,這是巧合。你不應該下載我的答案。當我寫我的答案時,你的答案不在那裏。 – Kuls

+0

哦,男孩,我不知道誰已經downvoted我的答案 –

相關問題