gpt4 book ai didi

android - AlphaAnimation 对象不会更改动画 View 的 alpha 属性,为什么?

转载 作者:太空宇宙 更新时间:2023-11-03 12:52:10 24 4
gpt4 key购买 nike

我正在尝试为按钮的 alpha 设置动画,当我将 alpha 从 1 设置为 0 时效果很好。但是,在动画的最后,我无法将它从 0 重置为 1,因为按钮的alpha 已经是 1(这会导致按钮在屏幕上“跳跃”而没有任何褪色)。似乎 Animation 对象不是直接设置 View alpha,而是设置 View 的一些表示属性。有谁知道我怎样才能让这个动画正常工作?

我的代码:

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"
/>

注意事项:

我设置 View 可见性是因为 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 - AlphaAnimation 对象不会更改动画 View 的 alpha 属性,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28387524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com