Zach
Zach

Reputation: 10139

Draw on android screen with fingers

I am trying to create a drawing on the screen with the touch event. I am successful in doing the same. here is the code I am using for that

public class DrawView extends View implements OnTouchListener {
    private static final String TAG = "DrawView";

    List<Point> points = new ArrayList<Point>();
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
    }

    @Override
    public void onDraw(Canvas canvas) {
        for (Point point : points) {

            canvas.drawCircle(point.x, point.y, 8, paint);
            // Log.d(TAG, "Painting: "+point);
        }
    }

    public boolean onTouch(View view, MotionEvent event) {
        // if(event.getAction() != MotionEvent.ACTION_DOWN)
        // return super.onTouchEvent(event);
        Point point = new Point();
        point.x = event.getX();
        point.y = event.getY();
        points.add(point);
        invalidate();
        Log.d(TAG, "point: " + point);
        return true;
    }
}

class Point {
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}

As you can see I am drawing a circle on every point the user touches. I am getting a continuos line/curve when user moves his finger slowly. But if the user moves his fingers fast I am not getting a continuous drawing. Instead a list of separated dots on the path the finger moved.

How can I do this properly? Thanks

Upvotes: 1

Views: 4092

Answers (2)

Luis
Luis

Reputation: 12058

I've answered a similar question, for hand drawing over a mapview on this post: Hand Draw over MapView

You can use the same code, replacing GeoPoint by Point and removigin the conversion between Geopoints and Points.

Upvotes: 0

Hakan Serce
Hakan Serce

Reputation: 11266

You should draw a path rather than the points themselves. Check here for information: http://developer.android.com/reference/android/graphics/Path.html

Upvotes: 1

Related Questions