我想動畫的按鈕的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正常工作。