2012-07-12 106 views
2

我想显示一个启动屏幕动画,其中图像淡入淡出。我想要在图像淡出后加载第二个活动。Android中的初始屏幕Alpha动画

  1. 淡入时间(1000毫秒)
  2. 等待(1000毫秒)
  3. 淡出时间(1000毫秒)
  4. 等待(1000毫秒)
  5. 加载第二活动

我该如何解决这个问题?我目前使用的代码是:

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.animation.AccelerateInterpolator; 
import android.view.animation.AlphaAnimation; 
import android.view.animation.Animation; 
import android.widget.ImageView; 
public class Splash extends Activity 
{ 
    ImageView img; 
    Thread timer; 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 
     img = (ImageView) findViewById (R.id.imgSplash); 
     img.startAnimation(FadeIn(1000)); 
     try 
     { 
      Thread.sleep(1000); 
     } 
     catch (InterruptedException e1) 
     { 
      e1.printStackTrace(); 
     } 

     img.startAnimation(FadeOut(1000)); 

     try 
     { 
      Thread.sleep(1000); 
     } 
     catch (InterruptedException e1) 
     { 
      e1.printStackTrace(); 
     } 
     timer.start(); 
     Intent intent = new Intent(); 
     intent.setClass(Splash.this,MainScreen.class); 
     startActivity(intent); 
    } 
    public void onPause() 
    { 
     super.onPause(); 
     finish(); 
    } 
    private Animation FadeIn(int t) 
    { 
     Animation fade; 
     fade = new AlphaAnimation(0.0f,1.0f); 
     fade.setDuration(t); 
     fade.setInterpolator(new AccelerateInterpolator()); 
     return fade; 
    } 
    private Animation FadeOut(int t) 
    { 
     Animation fade; 
     fade = new AlphaAnimation(1.0f,0.0f); 
     fade.setDuration(t); 
     fade.setInterpolator(new AccelerateInterpolator()); 
     return fade; 
    } 
} 

请帮忙。

+1

第二项活动没有开始?或者你是否验证它会开始? – Addison 2012-07-12 15:46:44

+0

1)你的'timer'永远不会被初始化 – fiddler 2012-07-12 15:50:39

+2

请不要强迫你的用户在每次他们想要使用你的应用程序时等待4秒。通过这样做,你正在浪费他们的时间(这会让他们烦恼)。如果您在开始活动之前需要加载某些内容,则会在加载过程中显示出现飞溅。您只需在应用程序可以实现之前添加任意延迟时间即可浪费时间。如果你在浪费你的用户时间方面死气沉沉的话,那么你可以在http://blog.iangclifton.com/2011/01/01/android-splash-screens-done-right/上看到这篇文章。它会指导你如何正确地做到这一点。 – FoamyGuy 2012-07-12 15:52:26

回答

3

您可以使用AnimationSet进行设置。 Animation.setStartOffset()方法允许说明动画何时开始(fadeIn为0,fadeOut为2000)。下一个活动在3秒后启动,使用Handler.postDelayed()

private final Handler handler = new Handler(); 

private final Runnable startActivityRunnable = new Runnable() { 

    @Override 
    public void run() { 
     Intent intent = new Intent(); 
      intent.setClass(Splash.this,MainScreen.class); 
     startActivity(intent); 
    } 
}; 

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 
    img = (ImageView) findViewById (R.id.imgSplash); 

    setContentView(img); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 

    AnimationSet set = new AnimationSet(true); 

    Animation fadeIn = FadeIn(1000); 
    fadeIn.setStartOffset(0); 
    set.addAnimation(fadeIn); 

    Animation fadeOut = FadeOut(1000); 
    fadeOut.setStartOffset(2000); 
    set.addAnimation(fadeOut); 

    img.startAnimation(set); 

    handler.postDelayed(startActivityRunnable, 3000); 
} 

public void onPause() 
{ 
    super.onPause(); 
    handler.removeCallbacks(startActivityRunnable); 
} 
+0

只是在复制/粘贴错误...我修好了。谢谢 – fiddler 2012-07-12 16:18:35

+0

这个新版本怎么样? '回'问题应该解决,对吧? – fiddler 2012-07-12 16:24:40

+0

是的,应该这样做。请注意,尽管如果你没有后台工作,我仍然建议不要使用启动画面。 – FoamyGuy 2012-07-12 17:43:50