2015-02-07 68 views
2

我想動畫的按鈕的alpha,這適用於當我設置從1到0的alpha。但是,在最後的動畫,我不能將它從0重置爲1,因爲按鈕的alpha已經是1(並且這導致按鈕在屏幕上「跳躍」而沒有任何淡化)。看起來,Animation對象不是直接設置視圖alpha,而是視圖的一些表現屬性。有沒有人有我如何能讓這個動畫正常工作的想法?AlphaAnimation對象不會更改被動畫的視圖的alpha屬性,爲什麼?

我的代碼:

private void performFavoriteButtonFade(boolean isFavorite) { 
    AlphaAnimation fadeAnimation = new AlphaAnimation(0, 0); 
    if (isFavorite) { 
     if (this.favoriteButton.getAlpha() == 1) { 
      fadeAnimation = new AlphaAnimation(1, 0); 
     } 
    } else { 
     if (this.favoriteButton.getAlpha() == 0) { 
      fadeAnimation = new AlphaAnimation(0, 1); 
     } else { 
      fadeAnimation = new AlphaAnimation(1, 1); 
     } 
    } 

    fadeAnimation.setDuration(300); 
    fadeAnimation.setFillAfter(true); 

    this.favoriteButton.startAnimation(fadeAnimation); 
    this.favoriteButton.setVisibility(View.VISIBLE); 
} 
<ImageButton 
     android:id="@+id/favoriteButton" 
     android:src="@drawable/favorite_icon" 
     android:background="@android:color/transparent" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_alignParentBottom="true" 
     android:layout_marginRight="30dp" 
     android:layout_marginBottom="20dp" 
     android:visibility="invisible" 
     android:onClick="didTapNotFavorite" 
     /> 

注:

我設置,因爲answer in this post的視圖可視性,使AlphaAnimation正常工作。

回答

3

你似乎在使用舊式的動畫。你可以使用新的風格,它應該在不改變可視性或填充後工作。相反,在你的XML設置公開程度的阿爾法正好被設置爲0。

淡出:

Button button = new Button(mActivity); 
button.animate().alpha(0).setDuration(300).start(); 

淡入:

Button button = new Button(mActivity); 
button.animate().alpha(1f).setDuration(300).start(); 
0

試試這個:

AlphaAnimation fadeAnimation; 
     if (isFavorite) { 
      if (this.favoriteButton.getAlpha() == 1) { 
       fadeAnimation = new AlphaAnimation(1, 0); 
       fadeAnimation.setInterpolator(new AccelerateInterpolator()); 
      } 
     } else { 
      if (this.favoriteButton.getAlpha() == 0) { 
       fadeAnimation = new AlphaAnimation(0, 1); 
       fadeAnimation.setInterpolator(new DecelerateInterpolator()); 
      } else { 
       fadeAnimation = new AlphaAnimation(1, 1); 
      } 
     } 
相關問題