如何让按钮在 Android 中闪烁? [英] How to make a Button blink in Android?

查看:88
本文介绍了如何让按钮在 Android 中闪烁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户(在我的问答游戏中)选择了错误答案,则带有正确答案的按钮应闪烁绿色.到目前为止,我是这样做的:

If the user (in my quizgame) chooses the false answer, the button with the correct answer should blink green. So far i did it like this:

    if(answerTrue)
        for (int i = 0; i < 2000; i = i + 250) {
            handler.postDelayed(rbl_blinkNormal, i);
            i = i + 250;
            handler.postDelayed(rbl_blinkGreen, i);
        }

还有可运行的:绿色:

 rbl_blinkGreen= new Runnable() {
     @Override
     public void run() {
         btn_richtig.setBackgroundResource(R.drawable.color_green_btn);
     }

 };

正常:

 rbl_blinkNormal= new Runnable() {
     @Override
     public void run() {
         btn_richtig.setBackgroundResource(R.drawable.color_black_btn);
     }

 };

它工作正常,但像这样我每 250 毫秒调用一次 postDelayed().这可能会影响我的应用程序性能,有没有更好的方法来做到这一点?

It works fine but like this Im calling the postDelayed() every 250ms. May this impact my application performance and is there any better way to do this?

推荐答案

一旦将按钮的颜色设置为 GREEN,您就可以为按钮设置动画.我的意思是,

You can animate your button once you set its color to say GREEN. I mean,

if(answerTrue){

    // Set the color of the button to GREEN once.

    // Next, animate its visibility with the set color - which is GREEN as follows:

    Animation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(50); //You can manage the blinking time with this parameter
    anim.setStartOffset(20);
    anim.setRepeatMode(Animation.REVERSE);
    anim.setRepeatCount(Animation.INFINITE);
    button.startAnimation(anim);
}

同样,您可以为另一个按钮设置动画并在需要时停止动画.

Similarly, you can animate the other button and stop animation when you feel like.

来源: android 视图中的闪烁文本

这篇关于如何让按钮在 Android 中闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆