Android paint / canvas issue; drawing smooth curves

How to 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;
}
}
+3
source share
3 answers

There is a FingerPaint app bundled with the demos API package; you can take a look at that. Here's the link

+5
source

I assume that you will have to use some kind of interpolation and fill in the blanks yourself. This would completely destroy the system if it had to send touch events for each pixel moved by a finger.

0
source

Source: https://habr.com/ru/post/1741119/


All Articles