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

Android,使用动画设置按钮背景色吗?,第1张

概述我有一个按钮,单击该按钮时,我希望通过在两种背景颜色之间来回切换来使该按钮闪烁.This答案使用AlphaAnimation制作一个闪烁的按钮:finalAnimationanimation=newAlphaAnimation(1,0);//Changealphafromfullyvisibletoinvisibleanimation.setDuration(500)

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

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

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

解决方法:

得到它的工作!感谢this发布!

final AnimationDrawable drawable = new AnimationDrawable();final Handler handler = new Handler();drawable.addFrame(new colorDrawable(color.RED), 400);drawable.addFrame(new colorDrawable(color.GREEN), 400);drawable.setoneshot(false);btn = (button) vIEw.findVIEwByID(R.ID.flashBtn);btn.setBackgroundDrawable(drawable);handler.postDelayed(new Runnable() {    @OverrIDe    public voID run() {        drawable.start();    }    }, 100);

奇迹般有效!

总结

以上是内存溢出为你收集整理的Android,使用动画设置按钮背景色吗?全部内容,希望文章能够帮你解决Android,使用动画设置按钮背景色吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1089158.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存