views:

905

answers:

2

How do I get smooth curves instead of dots or circles, when I draw with my finger on the touch screen, in Android? I am using the following code-

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.WHITE);
    paint.setAntiAlias(true);
}

     @Override
    public void onDraw(Canvas canvas) {
    for (Point point : points) {
        canvas.drawCircle(point.x, point.y, 5, 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;
}
}
A: 

I imagine you'd have to use some kind of interpolation and fill in the gaps yourself. It would completely kill the system if it had to despatch touch events for every single pixel your finger moved across.

Steve H
Thanks Steve. But instead of using canvas.drawCircle(point.x, point.y, 5, paint); I just want to draw a continuous curve because what I get is small circles. I don't know how to do interpolation so if there is a way/method to simply draw/paint curves in Android, then I would like to use that.
Wrapper
in Finger paint you join adjacent points detected by touch interface using a Bezier curve.
Samuh
+2  A: 

There is a FingerPaint application bundled with API demos package; you can take a look at that.

Samuh