2013-10-30 365 views
9

我正在爲RelativeLayout使用從右向左移動的動畫。如何在Android中動畫結束後刪除動畫視圖?

我試圖在onAnimationEnd()中將佈局的可見性設置爲'GONE',但它不起作用。動畫視圖仍然存在於停止的地方。

這是我使用的代碼:

創建動畫從右到左:

TranslateAnimation animate = new TranslateAnimation(0,-rlImages.getWidth()/2,0,0); 
animate.setDuration(1000); 
animate.setFillAfter(true); 

動畫設置爲佈局:

centre_leftanimate.startAnimation(animate); 

添加聽衆動畫:

animate.setAnimationListener(new AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     // TODO Auto-generated method stub 
     centre_leftanimate.setVisibility(View.GONE); // I wants to make the visibility of view to gone,but this is not working               
     half_left.setVisibility(View.VISIBLE); 
    } 
}); 

如何使動畫視圖的可見性在動畫結束後不可見?

請建議。

回答

1

您可以根據需要設置視圖的可見性。 如果您在動畫開始後立即將其設置爲View.INVISIBLE,則動畫將可見,並且視圖將在動畫停止後立即消失。 我認爲你的代碼的問題可能是你正在使用GONE而不是INVISIBLE。 第二個問題可能是您在設置偵聽器之前啓動動畫,但使用我的解決方案,您實際上不需要任何偵聽器。 另外,拿走FillAfter選項。

TranslateAnimation animate = new TranslateAnimation(0,-rlImages.getWidth()/2,0,0); 
animate.setDuration(1000); 
centre_leftanimate.startAnimation(animate); 
centre_leftanimate.setVisibility(View.INVISIBLE); 
+0

它在onAnimationEnd中嘗試了Invisible,但它仍然不工作。我的動畫視圖出現在動畫結束的相同位置。我希望動畫視圖在動畫結束後不可見。 – user1891910

+1

您是否刪除了setFillAfter(true)? –

+0

請嘗試上面的代碼,不要聽衆。 –

17

我有同樣的問題,除了我實際上刪除了正在動畫的視圖,但動畫仍然存在!

簡單的解決方案是實際取消動畫,然後您可以隱藏或刪除視圖。

animatedView.getAnimation().cancel(); 

,或者如果你仍然有你的動畫對象:

animation.cancel(); 

編輯:似乎有一些與上述溶液遺留下來,所以我加了這一點:

animatedView.setAnimation(null); 
+3

爲什麼我們不只是調用animatedView.clearAnimation();代替? – HendraWD

2

我有同樣的問題,在我的情況下刪除

android:fillAfter="true" 

在動畫負責刪除視圖和此代碼解決我的問題。但其他人會有更好的代碼。

viewToHide.startAnimation(animationForHide); 
viewToHide.setVisibility(View.GONE); 
0

我有同樣的問題,我用這個問題的答案Android - remove View when its animation finished解決這個問題。

編輯: 你可以做這樣的事情:

final MenuActivity MN= MenuActivity.this; 
    AFade.setAnimationListener(new Animation.AnimationListener(){ 
      @Override 
      public void onAnimationStart(Animation arg0) { 
      }   
      @Override 
      public void onAnimationRepeat(Animation arg0) { 
      }   
      @Override 
      public void onAnimationEnd(Animation arg0) { 
       LayoutMain.post(new Runnable() { 
        public void run() { 
         // it works without the runOnUiThread, but all UI updates must 
         // be done on the UI thread 
         MN.runOnUiThread(new Runnable() { 
          public void run() { 
           LayoutMain.removeView(NextButton); 
          } 
         }); 
        } 
       }); 
      } 
     }); 

LayoutMain是包含視圖的佈局。 MN是你的活動。

+0

你能多解釋一下嗎? –

+0

你必須在我的編輯中做類似的事情 –

0

我知道這是一個古老的問題,但今天我遇到了這個問題,我想表明我是如何解決它的,因爲雖然已經發布的答案有幫助,但他們都沒有完全適合我的情況。

在我的情況下,我創建了一個dinamically的自定義視圖,應用了2個動畫(alpha和translation)並在動畫完成後刪除了視圖。這是我做的:

//Create fade out animation 
AlphaAnimation fadeOut = new AlphaAnimation(1f, 0f); 
fadeOut.setDuration(1000); 
fadeOut.setFillAfter(true); 
//Create move up animation 
TranslateAnimation moveUp = new TranslateAnimation(0, 0, 0, -getHeight()/6); 
moveUp.setDuration(1000); 
moveUp.setFillAfter(true); 
//Merge both animations into an animation set 
AnimationSet animations = new AnimationSet(false); 
animations.addAnimation(fadeOut); 
animations.addAnimation(moveUp); 
animations.setDuration(1000); 
animations.setAnimationListener(new Animation.AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { } 
     @Override 
     public void onAnimationEnd(Animation animation) { 
      //Hide the view after the animation is done to prevent it to show before it is removed from the parent view 
      view.setVisibility(View.GONE); 
      //Create handler on the current thread (UI thread) 
      Handler h = new Handler(); 
      //Run a runnable after 100ms (after that time it is safe to remove the view) 
      h.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        removeView(view); 
       } 
      }, 100); 
     } 
     @Override 
     public void onAnimationRepeat(Animation animation) { } 
    }); 
view.startAnimation(animations); 

請注意,這是一個自定義視圖(其延伸的FrameLayout裏),一切都在UI線程上運行內部完成。