android动态改变alpha,android-AlphaAnimation对象不更改被动画视图的...

博主尝试为按钮的Alpha设置动画,将Alpha从1设为0效果良好,但动画最后无法从0重置为1,按钮仅“跳跃”不褪色。博主给出相关代码,并提到设置视图可见性以让AlphaAnimation正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我正在尝试为按钮的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正常工作.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值