tags:

views:

38

answers:

1

I am not able to display the rectangles after a certain delay in code. Here is what I am doing

DashPathEffect dashPath =new DashPathEffect(new float[]{1,0}, 1);

        paint.setPathEffect(dashPath);
        paint.setStrokeWidth(300);
        final int  size =300;

canvas.drawLine(0, size ,100 , size, paint);

try {

            Thread.sleep(4000, 0);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

canvas.drawLine(110, size ,200 , size, paint);


I am not able to notice any delay between these two rectagles in the mobile screen. Both appear at the same time. All I am trying to do is, draw the rectangles one after other with some delay in between. What this code is rather doing is, waiting for 4 seconds and then displaying both rectangles at same time. Thank you.

+1  A: 

You should never sleep in the UI thread. As you saw, this causes the entire program to lock up and wait for the sleep to complete.

Instead, in this case you might want to look into using a handler. See timed ui-updates for a full description on how to do this.

Mayra
Maximus
Ok, 99.9% of the time, unless you really know what you are doing and have a good explanation for it, sleeping in the UI thread is a bad idea.
Mayra
Yes... Agreed :)
Maximus
Hello all,I did try to do the work from a separate thread. What I did is, create a new class B that has above code, and instantiate the class B from the thread Spawned by A. To explain in simple terms, Class A spawns thread , inside the thread I instantiate more than one objects of class B and have thread.sleep in between instatiation. This way I am thinking of able to draw rectanlges with delay.