Search code examples
javaandroidondraw

how I can slow grind down the line in android?


I mean I want to drawline but slowly. I write this code to draw line in ondraw method.

.
.
.
.
caneta.setARGB(255, 255, 0,0);caneta.setStrokeWidth(10); 
canvas.drawLine(0, ys * 1/2, this.getWidth(), ys * 1/2, caneta);
.
.
.

how I did it slowly?


Solution

  • That's nearly like a gameloop works:

    -invalidate your canvas ever X milliseconds (using a loop and Thread.sleep())

    -increment your X/Y coords after every loop

    -handle the new coords in onDraw() again

    Example:

    private int x1, x2;
        private int y1, y2;
        private View v;
    
        public void start()
        {
            for (int i = 0; i <= 250; i++)
            {
                 v.invalidate();
    
                 x2 += 1;
    
                try
                {
                    Thread.sleep(50);
                }
                catch (InterruptedException e)
                {
                }
            }
        }
    

    in your existing view class, where you already have your onDraw method

     protected void onDraw(Canvas canvas)
            {
            //draw your line here using your X and Y member
                    canvas.drawLine(x1, y1, x2, y2, caneta);
            }