【问题标题】:Android, set button background color with animation?Android,用动画设置按钮背景颜色?
【发布时间】:2015-04-06 03:08:28
【问题描述】:

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

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

搞定了,看看下面的答案

【问题讨论】:

  • 找到了一个解决方案,并发布了一个答案,但我会离开,所以其他人有四个解决方案而不是三个。
  • 好的先生,很好.. ........

标签: android animation


【解决方案1】:

搞定了!感谢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);

像魅力一样工作!

【讨论】:

    猜你喜欢
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    相关资源
    最近更新 更多