0

下面的代码用于闪烁按钮:

public void blink() {
    new Thread(new Runnable() {
        public void run() {
            while (true) {
                int timeToBlink = 500;

                try { Thread.sleep(timeToBlink); } 
                catch (Exception e) {}

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (btn.getVisibility() == View.VISIBLE) {
                            btn.setVisibility(View.INVISIBLE);
                        } else {
                            btn.setVisibility(View.VISIBLE);
                        }
                    }
                });
            }
        }
    }).start();
}
4

4 回答 4

2

使用 alpha 动画怎么样?

下面的片段会帮助你。

fade.xml(保存在 res/anim/fade.xml 下)

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromAlpha="0.0"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toAlpha="1.0" />

MainActivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn= (Button) findViewById(R.id.btn);
        Animation myFadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this,
                R.anim.fade);
        btn.startAnimation(myFadeInAnimation);

    }

}
于 2013-07-25T13:31:03.727 回答
0

可能是您的应用程序处于无限循环中,因此 runOnUIThread 的 runnable 永远不会被执行。尝试使用 alpha 动画

于 2013-07-25T13:36:34.580 回答
0

倒数计时器怎么样?

final CountDownTimer blinkTimer = new CountDownTimer(120000 , 1000) { 
public void onTick(long millisUntilFinished) {
    if (btn.getVisibility() == View.VISIBLE) {
        btn.setVisibility(View.INVISIBLE);
    } else {
        btn.setVisibility(View.VISIBLE);
    } 
}

public void onFinish() {
    blinkTimer.start(); 
}
};
于 2013-07-25T13:18:26.040 回答
0

你可以使用这个:

Button button = (Button) findViewById(R.id.button1);

Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50); //You can manage the blinking time with this parameter
animation.setStartOffset(20);
animation.setRepeatMode(Animation.REVERSE);
animation.setRepeatCount(Animation.INFINITE);
button.startAnimation(animation);
于 2015-12-01T09:27:57.720 回答