【发布时间】:2015-02-07 21:20:36
【问题描述】:
我正在尝试为按钮的 alpha 设置动画,当我将 alpha 从 1 设置为 0 时效果很好。但是,在动画的最后,我无法将其从 0 重置为 1,因为按钮的alpha 已经是 1(这会导致按钮在屏幕上“跳跃”而没有任何淡入淡出)。似乎 Animation 对象不是直接设置视图 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 正常工作。
【问题讨论】: