android 背景色动画,Android,使用动画设置按钮背景色吗?

我有一个按钮,单击该按钮时,我希望通过在两种背景颜色之间来回切换来使该按钮闪烁.

This答案使用AlphaAnimation制作一个闪烁的按钮:

final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible

animation.setDuration(500); // duration - half a second

animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate

animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely

animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in

final Button btn = (Button) findViewById(R.id.your_btn);

btn.startAnimation(animation);

但是我无法使其与背景色一起使用.

Android Studio将自动完成以下操作:

animation = new Animation() {

@Override

public void setBackgroundColor(int bg) {

super.setBackgroundColor(bg);

}

};

但是我尝试将其应用于按钮(bg = Color.parseColor(“#ffff9434”)),但没有骰子.

先感谢您.

编辑

还尝试了以下操作,但已弃用并且不起作用(来自here)

Button btn = (Button)this.findViewById(R.id.btn1);

//Let's change background's color from blue to red.

ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};

TransitionDrawable trans = new TransitionDrawable(color);

//This will work also on old devices. The latest API says you have to use setBackground instead.

btn.setBackgroundDrawable(trans);

trans.startTransition(5000);

ETID 2

正常工作,请参见下面的答案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值