如何在 android 中使用 onTouchEven() 在不同位置绘制圆圈?

How to draw circles on different positions using onTouchEven() in android?

我试图在用户 touches.I 使用 onTouchEvent() 获取 touch.The 的 x y 坐标的位置绘制一个圆圈,以下代码在 [= =17=] 当我在 onTouchEvent() 中的 'break;' 语句之前使用 invalidate() 函数时,会出现圆圈,但是当我在其他位置触摸时,前一个圆圈会被擦除,并在新触摸的位置绘制一个新圆圈位置。 我如何修改此代码,以便在每个 ACTION_DOWN onTouchEvent() 上在该点上绘制一个圆圈,并且之前绘制的圆圈也不会被删除。

public class TestView3 extends View {

    private static final String TAG = "TestView3";
    Paint paint = new Paint();
    float mX,mY;

    public TestView3(Context context, AttributeSet attributeSet){
        super(context);
        Log.d(TAG, "TestView3: constructor called");
        paint.setColor(Color.BLACK);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(TAG, "onDraw: called");
        //canvas.drawLine(0,0,20,20,paint);
        //canvas.drawLine(20,0,0,20,paint);
        canvas.drawCircle(mX,mY,10,paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG, "onTouchEvent: Action_down happend");
                mX = x;
                mY = y;

                break;
        }
        return true;

    }
}

您必须将您的圈子存储在一个列表中,然后在 onDraw 方法中绘制每个圈子。

以下经过编辑的代码对我有用。

public class TestView3 extends View {

    private static final String TAG = "TestView3";
    Paint paint = new Paint();
    float mX,mY;
    Bitmap mBitmap;
    Canvas mCanvas;

    ArrayList<Point> arrayList = new ArrayList<>();

    public TestView3(Context context, AttributeSet attributeSet){
        super(context);
        Log.d(TAG, "TestView3: constructor called");
        paint.setColor(Color.BLACK);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(TAG, "onDraw: called");
        //canvas.drawLine(0,0,20,20,paint);
        //canvas.drawLine(20,0,0,20,paint);
        for(int i = 0;i < arrayList.size();i++){
            Point point = arrayList.get(i);
            canvas.drawCircle(point.x,point.y,10,paint);


            // Draw line with next point (if it exists)
            if (i + 1 < arrayList.size()) {
                Point next = arrayList.get(i + 1);
                canvas.drawLine(point.x, point.y, next.x, next.y, paint);
            }
        }
       // canvas.drawCircle(mX,mY,10,paint);
    }

/*    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);

    }*/

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG, "onTouchEvent: Action_down happend");

                mX = x;
                mY = y;
                arrayList.add(new Point((int)x,(int)y));
                invalidate();
                break;
        }
        return true;

    }
}