在时间限制内每秒更改一次视图的颜色

时间:2016-09-23 13:21:25

标签: android

我有一个View,我希望在一个时间限制内更改BackgroundColor视图。假设我想每秒将View的颜色改为红色和绿色,持续20分钟。

我已尝试过的内容:

final static int INTERVAL = 10000; // 1 second
    private static View myView = null;
    boolean whichColor = true;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myView = (View) findViewById(R.id.my_view);
        myView.setBackgroundColor(Color.RED);// set initial colour
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(INTERVAL);
                    } 
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    updateColor();
                    whichColor = !whichColor;
                }
            }
        }).start();
    }

    private void updateColor() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (whichColor) 
                    myView.setBackgroundColor(Color.RED);
                else 
                    myView.setBackgroundColor(Color.GREEN);
            }
        });
    }
}

这是每秒都在改变视图的颜色,但我只想在时间限制内进行更改。有什么方法可以做到这一点吗? 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

您必须保持对Runnable内的计数器的引用:

new Thread(new Runnable() {
    private static final int MAX_COUNT = 20 * 60;
    int counter = 0;

    public void run() {
        while (counter < MAX_COUNT) {                
            try {
                Thread.sleep(INTERVAL);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            counter++;
            updateColor();
            whichColor = !whichColor;
        }
    }
}).start();

替代:

使用Handler类及其消息系统。

创建扩展Handler的类:

public class BlinkingHandler extends Handler {
    private static final int MAX_COUNT = 20 * 60;
    int counter = 0;

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == 123) {
            counter++;
            //Change background color
            if (counter < MAX_COUNT) {
                this.sendEmptyMessageDelayed(123, DateUtils.SECOND_IN_MILLIS);
            }
        }
    }
}

初始化您的Handler

BlinkingHandler handler = new BlinkingHandler();
handler.sendEmptyMessage(123);

答案 1 :(得分:0)

试试TransitionDrawable

的代码
GradientDrawable greenDrawable=new GradientDrawable();
greenDrawable.setColor(getResources().getColor(R.color.green));
GradientDrawable redDrawable=new GradientDrawable();
redDrawable.setColor(getResources().getColor(R.color.red));
Drawable[] drawables={greenDrawable,redDrawable};
final TransitionDrawable transitionDrawable=new TransitionDrawable(drawables);
TextView textView= (TextView) findViewById(R.id.text);
textView.setBackground(transitionDrawable);
CountDownTimer countDownTimer=new CountDownTimer(1000*60*20,1000) {
  @Override
    public void onTick(long millisUntilFinished) {
        transitionDrawable.startTransition(1000);
    }
  @Override
    public void onFinish() {
       //finished 1000*60*20 milliseconds
      }
};
countDownTimer.start();