How to pause for 5 seconds before drawing the next thing on Android?
13,173
Solution 1
Don't wait in onDraw method it's called in the UI thread and you'll block it. Use flags to handle which line will be drawn
boolean shouldDrawSecondLine = false;
public void setDrawSecondLine(boolean flag) {
shouldDrawSecondLine = flag;
}
public void onDraw(Canvas canvas) {
int w = canvas.getWidth();
int h = canvas.getHeight();
canvas.drawLine(w/2, 0, w/2, h-1, paint);
if (shouldDrawSecondLine) {
canvas.drawLine(0, h/2, w-1, h/2, paint);
}
}
Than use it in your code like this
final View view;
// initialize the instance to your view
// when it's drawn the second line will not be drawn
// start async task to wait for 5 second that update the view
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
return null;
}
@Override
protected void onPostExecute(Void result) {
view.setDrawSecondLine(true);
view.invalidate();
// invalidate cause your view to be redrawn it should be called in the UI thread
}
};
task.execute((Void[])null);
Solution 2
you can use a CountDownTimer like this :
public void onDraw(Canvas canvas) {
int w = canvas.getWidth();
int h = canvas.getHeight();
canvas.drawLine(w/2, 0, w/2, h-1, paint);
// PAUSE FIVE SECONDS
new CountDownTimer(5000,1000){
@Override
public void onTick(long miliseconds){}
@Override
public void onFinish(){
//after 5 seconds draw the second line
canvas.drawLine(0, h/2, w-1, h/2, paint);
}
}.start();
}
Regards,
Solution 3
Well... you could set a flag in (which would be in some other method) and within your onDraw()
based on the value of this flag draw that line.
i.e. maybe something like (though I'm not sure why you would need a pause)
invalidate();
pause/sleep();
//set flag
invalidate();
Related videos on Youtube
Author by
Kalina
Updated on June 04, 2022Comments
-
Kalina almost 2 years
Say I want to draw a line, then wait five seconds, then draw another line. I have a method like this:
public void onDraw(Canvas canvas) { int w = canvas.getWidth(); int h = canvas.getHeight(); canvas.drawLine(w/2, 0, w/2, h-1, paint); // PAUSE FIVE SECONDS canvas.drawLine(0, h/2, w-1, h/2, paint); }
How do I pause?
-
Kalina almost 13 yearsThat seems to wait five seconds, and then draw both lines.
-
source.rar almost 13 yearsProbably will and in any case you should never delay in your onDraw(). Would make your app feel laggy.
-
Kalina almost 13 years@source.rar Where and how would I do this, then?
-
Egor almost 13 years@DeeV, I thought it was meant that all the drawing is executed in a separate thread.
-
CodeFusionMobile almost 13 years@TheBeatlemaniac All drawing is done on the UI thread, and AFAIK, putting in a delay like that won't draw anything. The screen won't update until the onDraw function returns.
-
Kalina almost 13 yearsThis requires me to change h, w, and canvas to final variables. When I do that and run it, only the first line is drawn.
-
Houcine almost 13 yearsi think you have missed to start the countdowntimer :!!! try to display a msg logcat in the onFinish() method , and see if it is executed or not