Android油漆/帆布问题;绘制平滑曲线

时间:2010-04-14 16:02:44

标签: android android-canvas paint draw curvesmoothing

当我在Android上用手指在触摸屏上绘图时,如何获得平滑曲线而不是圆点或圆圈?我使用以下代码 -

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 个答案:

答案 0 :(得分:5)

有一个与API演示包捆绑在一起的FingerPaint应用程序;你可以看一下。这是link

答案 1 :(得分:0)

我想你必须使用某种插值并自己填补空白。如果必须为手指移动的每个像素发送触摸事件,它将彻底杀死系统。

答案 2 :(得分:0)