AlphaAnimation 对象不会改变动画视图的 alpha 属性,为什么?
AlphaAnimation object don't change alpha property of the view being animated, why?
我正在尝试为按钮的 alpha 制作动画,当我将 alpha 从 1 设置为 0 时效果很好。但是,在动画的最后,我无法将它从 0 重置为 1,因为按钮的 alpha 已经是 1(这会导致按钮在屏幕上只是 "jump" 而没有任何淡入淡出)。似乎 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 正常工作。
你似乎在使用旧的动画风格。您可以使用新样式,它应该可以在不改变可见性或之后填充的情况下工作。不要在 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();
试试这个:
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);
}
}
我正在尝试为按钮的 alpha 制作动画,当我将 alpha 从 1 设置为 0 时效果很好。但是,在动画的最后,我无法将它从 0 重置为 1,因为按钮的 alpha 已经是 1(这会导致按钮在屏幕上只是 "jump" 而没有任何淡入淡出)。似乎 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 正常工作。
你似乎在使用旧的动画风格。您可以使用新样式,它应该可以在不改变可见性或之后填充的情况下工作。不要在 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();
试试这个:
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);
}
}