2010-11-11 66 views
2

在我的應用程序中,我有一個活動,當我按下菜單按鈕時打開當前活動的頂部。 在這個覆蓋圖中,我希望我的視圖在活動出現時淡入,並在關閉之前再次淡出它們。 這裏是我的代碼:關閉之前的動畫視圖活動

public class OverlayActivity extends Activity { 
TextView t; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.overlay); 
    t = (TextView) findViewById(R.id.view_overlay_text); 
    t.setAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in)); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    boolean r = false; 
    switch (keyCode) { 
    case KeyEvent.KEYCODE_BACK: 
     finishOverlay(); 
     r = true; 
     break; 
    default: 
     r = super.onKeyDown(keyCode, event); 
     break; 
    } 
    return r; 
} 

private void finishOverlay() { 
    Animation a = AnimationUtils.loadAnimation(this, R.anim.fade_out); 
    a.setAnimationListener(fadeOutComplete); 
    t.setText("TEST"); // <--- if i add this line the code suddenly works 
    t.setAnimation(a); 
} 

Animation.AnimationListener fadeOutComplete=new Animation.AnimationListener() { 
    public void onAnimationEnd(Animation animation) { 
     finish(); 
    } 

    public void onAnimationRepeat(Animation animation) { 
     // not needed 
    } 

    public void onAnimationStart(Animation animation) { 
     // not needed 
    } 
}; 

}

不知何故,淡出的動畫,如果我這樣做t.setText( 「SomeText」 則會)纔有效。如果我忽略該行,則不會生成動畫,因此AnimationListener不會被觸發。

UPDATE: 一些信息做clearify問題: 的onCreate:TextView的淡入,我可以看到它在屏幕上 onkeydown事件 「BACK」:finishOverlay之稱。 (它實際上) finishOverlay:動畫不適用於我想淡入的視圖。爲什麼?這是相同的參考。它可能是某種範圍的問題嗎?

回答

0

您需要發佈您的textview的XML代碼。

我懷疑textview中的文本是空白的......因此不會出現動畫。

另外,你爲什麼要在代碼中設置textview的可見性。由於textview出現在onCreate方法中,因此您不需要此行,因爲您不應將其設置爲在XML文件中不可見。

+0

那是真的。我不必設置知名度。那是錯誤的,但我沒有改變這個問題。 textview不是空白。我用imageview嘗試了同樣的事情。也沒有工作。所以它對於textview並不特別。 – notme 2010-11-11 13:11:38

+0

我想我會開始考慮你爲什麼要試圖以你的方式「追趕」後退按鈕。通常我見過人們使用onDestroy()來進行活動。 – user432209 2010-11-11 16:46:56

0

我已更新您的代碼,現在此代碼與您希望的相同。

public class mp3list extends Activity { 
TextView t; 
Animation a; 
Animation.AnimationListener fadeOutComplete; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mp3list); 
    t = (TextView) findViewById(R.id.tvc); 
    run(); 
    startOverlay(); 
    } 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
boolean r = false; 

switch (keyCode) { 
case KeyEvent.KEYCODE_BACK: 
    run(); 
    finishOverlay(); 
    r = true;   
    break; 
default: 
    r = super.onKeyDown(keyCode, event); 
    break; 

} 
return r; 
} 

private void startOverlay() { 

a = AnimationUtils.loadAnimation(this, R.anim.fade_in); 
a.setAnimationListener(fadeOutComplete); 
t.setText("helo world"); // <--- if i add this line the code suddenly works 
t.setAnimation(a); 
} 

private void finishOverlay() { 
a = AnimationUtils.loadAnimation(this, R.anim.fade_out); 
a.setAnimationListener(fadeOutComplete); 
t.setText("helo world"); // <--- if i add this line the code suddenly works 
t.setAnimation(a); 
finish(); 
} 

public void run(){ 

    fadeOutComplete = new Animation.AnimationListener() { 
     public void onAnimationEnd(Animation animation) { 

     } 

     public void onAnimationRepeat(Animation animation) { 
      // not needed 
     } 

     public void onAnimationStart(Animation animation) { 
      // not needed 
     } 
     }; 
    } 

}