2012-07-12 140 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