我正在尝试为按钮的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);
}
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正常工作.
解决方法:
您似乎正在使用旧的动画样式.您可以使用新样式,它应该可以正常工作,而无需更改可见性或之后填充.无需在XML中设置可见性,只需将alpha设置为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();
标签:android-animation,android
来源: https://codeday.me/bug/20191028/1954301.html