0

I found some code to draw line and now i wand drawing line progressively so that i cloud see it being drawn.

This is the code

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class DrawView extends View {
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.BLACK);
    }

    @Override
    public void onDraw(Canvas canvas) {
            canvas.drawLine(0, 0, 20, 20, paint);
            canvas.drawLine(20, 0, 0, 20, paint);
    }

}

How can i do that? Tnx

Community
  • 1
  • 1
N.M
  • 421
  • 1
  • 8
  • 18

2 Answers2

1

Did you see that? Look at source code ;) http://www.curious-creature.com/2013/12/21/android-recipe-4-path-tracing/

-1

You will want to break up your drawing into multiple steps. Inside your onDraw call, you will want to draw a part of your line, and update a variable so that the next line segment is drawn. Then you will want to make multiple onDraw() calls in an animation loop. You will need to be careful where you make your calls to the animation loop from. Read about the View class for more information, particular event handling and threading. http://developer.android.com/reference/android/view/View.html

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class DrawView extends View {
    Paint paint = new Paint();
    float x1 = 0;
    float x2 = 20;
    float y1 = 0; 
    float y2 = 20;

    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.BLACK);
    }

    @Override
    public void onDraw(Canvas canvas) {
            if(doClear) {
               //clear canvas to begin new animation
            }
            canvas.drawLine(x1, y1, x2, y2, paint);
    }

    public void animateLoop() {
         while(x1 < 500) {
             x1 += 20;
             y1 += 20;
             x2 += 20;
             y2 += 20;
             //tell android this view needs to be redrawn
             invalidate();
         }
         //when done set doClear to true so
}

If you really want to learn about animation, you should start with something like this example: http://developer.android.com/guide/topics/graphics/drawable-animation.html.

Dodd10x
  • 3,054
  • 1
  • 16
  • 27