2011-11-03 106 views
0

我正在開發我的第一個Android項目,並且我將首先製作一個簡單的啓動畫面,在主菜單顯示前淡出爲黑色。到目前爲止它的工作。問題在圖像淡出後立即出現,在顯示主菜單之前,它瞬間彈回一秒。淡出問題 - Android動畫

下面是SplashActivity.java代碼:

public class SplashActivity extends Activity 
{ 
LinearLayout mLinearLayout; 

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

    mLinearLayout = new LinearLayout(this); 


    ImageView i = new ImageView(this); 
    i.setImageResource(R.drawable.splash); 
    mLinearLayout.addView(i); 
    setContentView(mLinearLayout); 

    Animation fade = AnimationUtils.loadAnimation(this, R.anim.fade_out); 
    i.startAnimation(fade); 

    fade.setAnimationListener(new AnimationListener() { 
     public void onAnimationEnd(Animation animation) 
     { 
      startActivity(new Intent(SplashActivity.this, MenuActivity.class)); 
      SplashActivity.this.finish(); 
     } 

     public void onAnimationRepeat(Animation arg0) {    
     } 

     public void onAnimationStart(Animation arg0) { 
     } 
    }); 
} 
} 

下面是splash.xml代碼:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
xmlns:android = "http://schemas.android.com/apk/res/android" 
android:orientation = "vertical" 
android:layout_width = "match_parent" 
android:layout_height = "match_parent" 
android:background = "#000"> 
</LinearLayout> 

最後,這裏是爲淡出XML:

<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <alpha 
    android:fromAlpha="1.0" 
    android:toAlpha="0.0" 
    android:duration="500" 
    android:startOffset="2500"> 
    </alpha> 
</set> 

一對夫婦筆記: 現在的初始屏幕只是爲了表演(意思是我知道ri現在它沒有達到任何真正的目的)。 我可以粘貼AndroidManifest xml或其他您認爲可能需要的東西。

任何和所有的幫助表示讚賞。謝謝!

回答

3

動畫結束,然後翻轉回到以前的樣子。 Android動畫令人困惑,但想象一下你所看到的只是一場海市蜃樓。沒有任何關於您正在進行動畫製作的視圖的變化。一旦完成,它就會回到原來的狀態。

作爲一個例子,創建一個按鈕100dp x 100dp,並通過縮放動畫縮小或旋轉。非常緩慢地。就其運行而言,如果您點擊空白區域,該按鈕仍會註冊該命中。那是因爲它仍然「存在」,但你沒有看到它。

你需要做的是設置在動畫聽衆在圖像上可見:

public void onAnimationEnd(Animation animation) 
    { 
     startActivity(new Intent(SplashActivity.this, MenuActivity.class)); 
     SplashActivity.this.finish(); 
     i.setVisibility(View.INVISIBLE); 
    } 

可能需要做的onAnimationStart。實驗。

我在這段時間做了一個演示。可能是有用的:

https://docs.google.com/present/view?id=djqv5kb_187c62jvbf7

+1

請注意,您也可以調用'fade.setFillAfter(true)';這將在動畫結束時將最終結果應用於視圖。 – dmon

2

這將有助於。

public void onAnimationEnd(Animation animation) 
     { 
      i.setVisibility(View.INVISIBLE); 
      startActivity(new Intent(SplashActivity.this, MenuActivity.class)); 
      SplashActivity.this.finish(); 
     } 
+0

這很好,謝謝。 – vince88