Search code examples
androidandroid-studioandroid-canvastouch-event

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


I am trying to draw a circles at point where the user touches.I am using onTouchEvent() to get the x y coordinates of the touch.The following code adds a circle in right corner of the screen.But when i use invalidate() function before the 'break;' statement in the onTouchEvent(), circle appears but when i touch at other position the previous circle gets erased and and a new circle is drawn at the new touched position. How can I modify this code so that on every ACTION_DOWN onTouchEvent() a circle is drawn on that point and previously drawn circle also not erased.

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;

    }
}

Solution

  • You have to store your circles in a list and draw each of them in the onDraw method.

    The following edited code worked for me.

    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;
    
        }
    }