为什么在Flash屏幕在Android中完成其动画之前重定向到下一个活动?

问题描述:

我创建了一个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(); 

但我想知道这种行为的原因。

您可以使用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我的答案 –