I want to make a pause between drawing two draws. I’ve tried Thread.sleep, handlers, asyncTask and got same result – when activity starts up I must wait for a time that I set to see the first draw, only when I call same method (test) again, I see second draw instead of seeing first draw again. There’s my code:
public void test(){
button.setClickable(false);
button.setBackgroundColor(Color.DKGRAY);
view.setFromAtoB(true);
view.invalidate();
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
return null;
}
@Override
protected void onPostExecute(Void result) {
view.setMoveAB(true);
view.postInvalidate();
button.setBackgroundColor(Color.GRAY);
button.setClickable(true);
}
};
task.execute((Void[])null);
Where’s the problem? Why can’t I see some kind of harmony, first draw, pause, second draw? 🙂 Maybe I’ve blocked UI thread. For drawing I use canvas. In onDraw method I make some calculations and call drawRodsAndDiscs method:
private void drawRodsAndDiscs(Canvas canvas){
Paint paint = new Paint();
drawRods(canvas);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
for (Rect disc : discs) {
canvas.drawRect(disc, paint);
}
}
I would use this code for your problem.
When the timer is finished it is automagically restarted.
Try this: