Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
模拟android按钮的闪烁_Android_Timer - Fatal编程技术网

模拟android按钮的闪烁

模拟android按钮的闪烁,android,timer,Android,Timer,我想让我的按钮在两种颜色之间每秒切换三次背景色- btn\U tp\U深色和btn\U tp\U浅色 Timer timer = new Timer(); timer.schedule(new TimerTask() { private View Button; public void run() { Button = (View) findViewById(R.id.filmTransparent11);

我想让我的按钮在两种颜色之间每秒切换三次背景色- btn\U tp\U深色btn\U tp\U浅色

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        private View Button;
        public void run() {
            Button = (View) findViewById(R.id.filmTransparent11);
            Button.setBackgroundResource(R.drawable.btn_tp_dark);
        }
    }, 300);
这应该是模拟眨眼效果,但我不知道如何实现这一点

 <Button
            android:id="@+id/filmTransparent11"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="11"
            android:background="#00000000"
            android:onClick="next1" />

不要在android中麻烦地使用
计时器。而是使用
处理程序

您可以创建如下循环任务:

final Button button = (View) findViewById(R.id.filmTransparent11);
final Handler handler = new Handler();
final Runnable changeBackground = new Runnable() {
  private int i;
  @Override
  public void run() {
    // Set background based on task execution counter
    if (++i % 2 == 0) {
      button.setBackgroundResource(R.drawable.btn_tp_light);
    } else {
      button.setBackgroundResource(R.drawable.btn_tp_dark);
    }

    // Repeat task
    handler.postDelayed(this, 300);
  }
};

// Initiate the task
handler.postDelayed(changeBackground, 300);

实现它的一种方法是使用,带有。动画的持续时间为300ms,您可能希望使用
ValueAnimator.INFINITE
ValueAnimator.REVERSE
作为重复模式