2012-02-22 245 views
16

我一直在瀏覽關於此主題的許多線程,我可以在Android 2.2中處理AnimationListeners時出現的閃爍中找到,但我無法完全解決我的問題。Android動畫閃爍

我得到的是一個LinearLayout'彈出窗口',用戶觸摸下移約100個像素,然後再次觸摸以將其移回。我終於在第一部分沒有任何閃爍的情況下工作了(感謝在視圖上調用clearAnimation()的建議),但是當做相反的操作時(即將視圖移回),閃爍開始。我無法在onAnimationStart()方法中真正調用clearAnimation(),因爲它不會動畫!當然,如果我使用setFillAfter()而沒有任何動畫偵聽器,但是視圖的觸摸區域不會移動(因爲視圖本身沒有「實際」移動),所有動畫都可以完美地工作。

任何幫助將不勝感激。

this.popoverTab.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     popoverTab.setClickable(false); 
     popoverTab.setFocusable(false); 
     if (popoverHidden) { 
      Log.d(TAG, "About to show popover"); 
      // the popover is currently hidden, show it. 
      TranslateAnimation animation = new TranslateAnimation(0, 0, 100, 0); 
      animation.setDuration(700); 
      animation.setFillBefore(true); 
      animation.setAnimationListener(new AnimationListener() { 
       public void onAnimationEnd(Animation animation) { 

       } 

       public void onAnimationRepeat(Animation animation) { 

       } 

       public void onAnimationStart(Animation animation) { 
        footer.layout(footer.getLeft(), (footer.getTop() - 100), footer.getRight(), footer.getBottom()); 
       } 
      }); 
      footer.startAnimation(animation); 
     } else { 
      Log.d(TAG, "About to hide popover"); 
      // the popover is showing, hide it. 
      TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 100); 
      animation.setDuration(700); 
      animation.setFillAfter(true); 
      animation.setAnimationListener(new AnimationListener() { 
       public void onAnimationEnd(Animation animation) { 
        footer.clearAnimation(); 
        footer.layout(footer.getLeft(), (footer.getTop() + 100), footer.getRight(), footer.getBottom()); 
       } 

       public void onAnimationRepeat(Animation animation) { 

       } 

       public void onAnimationStart(Animation animation) { 

       } 
      }); 
      footer.startAnimation(animation); 
     } 
     // invert. 
     popoverHidden = !popoverHidden; 
     popoverTab.setClickable(true); 
     popoverTab.setFocusable(true); 
    } 

}); 

回答

50

,當我有同樣的問題幾天後我發現解決方案... thanx:

http://www.mail-archive.com/[email protected]/msg67535.html

我想出瞭解決這個問題的辦法。線索來自事實 ,當顯示視圖時,一切正常。 顯然,當動畫正在運行時,會由節目強制的 更新發生在後臺,並且不會導致閃爍 。當我們隱藏視圖時在onAnimationEnd()的後端添加一個簡短的動畫使得閃爍變爲 。

這是新onAndimationEnd()的工作代碼

public void onAnimationEnd(Animation animation) { 
      animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f); 
      animation.setDuration(1); 
      mPlayer0Panel.startAnimation(animation); 
    } 
+0

這需要被標記爲答案。 +1 – 2012-12-04 01:23:08

+0

非常感謝你解決這個問題! – 2013-01-01 22:55:38

+1

非常感謝。花了太多時間找到這個。 – braden 2013-07-11 01:47:02

3

你不應該對onAnimationEnd()使用clearAnimation()

試試這個:啓動時

  1. 使用setFillBefore(true)setFillAfter(true)動畫
  2. 設置正確的佈局屬性和結束動畫
+0

當移動面板時,我仍然閃爍。我通過在它後面放置一個虛擬視圖來解決問題,其中包含與前一個視圖相同的內容。 – JRod 2012-02-28 01:54:41

+0

我有同樣的問題,似乎調整view.layout()導致閃爍。 – Jeroen 2012-03-23 13:26:01

0

Alrite,我不得不讓我搜索它同樣的問題,我在這篇文章結束了。爲我的問題找到了一個解決方案,認爲分享我的解決方案。

我有運行多個動畫,但閃爍近期開始,但是當我跟蹤發現問題下繪製動畫的列表循環之後的閃爍發生。 (數組列表)

我只是在定位它之後添加了try catch來避免這個問題,一些動畫在飛行中被刪除,但沒有足夠的時間讓線程更新,因此循環仍然嘗試失敗,但未顯示,而是閃爍,但在嘗試和捕捉數組列表時,繪圖解決了我的問題。

2

我搜索了動畫問題(閃爍和呆滯)的所有stackoverflow帖子。 我沒有找到任何完美的答案。但我已經找到了相同的解決方案,這是下文,

動畫使用在onStart,

view_you_want_to_animate.setDrawingCacheEnabled(true); 

動畫使用onEnd,

view_you_want_to_animate.setDrawingCacheEnabled(false); 

現在,我的看法沒有閃爍或者當我的觀點是動畫時緩慢的行爲。 它適合我。

+0

這對我有效..謝謝! – 2014-07-14 07:12:39

+0

這也適用於我,以及獨立的alpha動畫和translate + scale動畫集。 – BVB 2014-07-29 02:51:07

11
@Override 
public void onAnimationEnd(Animation animation) 
{ 
    footer.clearAnimation(); 
} 

這對我有效。

+1

非常感謝。 – Sayka 2015-12-16 15:29:46