1

我正在创建一个应用程序来在表面屏幕上绘制自由形状,但我只能绘制分离点我的问题是。我希望这些点在我绘制它们时彼此连接,而不是从屏幕上抬起手指。我的意思是只要我触摸屏幕draw.here是我到目前为止的代码。

   public class SurfaceViewActivity extends Activity  {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new DrawingView(this));
}

class DrawingView extends SurfaceView {

    private final SurfaceHolder surfaceHolder;
    private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private List<Point> pointsList = new ArrayList<Point>();

    public DrawingView(Context context) {
        super(context);
        surfaceHolder = getHolder();
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (surfaceHolder.getSurface().isValid()) {

                // Add current touch position to the list of points
                pointsList.add(new Point((int)event.getX(), (int)event.getY()));

                // Get canvas from surface
                Canvas canvas = surfaceHolder.lockCanvas();

                // Clear screen
                canvas.drawColor(Color.BLACK);

                // Iterate on the list
                for(int i=0; i<pointsList.size(); i++) {
                    Point current = pointsList.get(i);

                    // Draw points
                   canvas.drawPoint(current.x, current.y, paint);

                }

                // Release canvas
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
        return false;
    }
}

}

4

2 回答 2

3

您可以使用此功能绘制流畅的线条

  public void drawBrzierLine(Canvas mCanvas, float xi, float yi, float xd, float yd) {

    Point start = new Point((int) xi, (int) yi);
    Point end = new Point((int) xd, (int) yd);
    Path mPath = new Path();
    mPath.reset();
    mPath.moveTo(start.x, start.y);
    mPath.quadTo(start.x, start.y, end.x, end.y);
    mCanvas.drawPath(mPath, mPaint);

}
于 2013-09-03T08:05:29.127 回答
2

onTouchEvent(MotionEvent event)你只处理ACTION_DOWN. 所以这个代码只会在你按下屏幕时运行。改为使用ACTION_MOVEhttp://developer.android.com/reference/android/view/MotionEvent.html

于 2013-09-03T07:52:28.710 回答