Search code examples
javaandroidcanvasmotionevent

Canvas; How to remove shapes retroactively


So I am using onDraw in a custom View class to draw shapes on a RelativeLayout + TableLayout all that works fine, and I have another class that I use to draw lines from Point A to Point B etc. example below:

enter image description here

My Goal:

If I drag my finger from Point A (objectA) to Point B(objectB), how can I delete those 2 View Objects from the canvas? I added a method:

objectA.delete();
objectB.delete();

that should delete both A and B when I drag my finger through MotionEvent, but it only deletes one and not the other, so i am thinking it's not retroactive?

Codes below:

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
   /// get the child that corresponds to the first touch
    DotView objectA = getChildForTouch((TableLayout) v, x, y);

    return true;
 case MotionEvent.ACTION_MOVE:

 ///used the x - y to get the object for every other  shape the user`S finger passes over.
 DotView objectB = getChildForTouch((TableLayout) v, x, y);


   /// just update positions
   line.setCoords(mStartX, mStartY, (int) x, (int) y);

 objectA.delete(); ///Delete first shape
 objectB.delete(); ///Delete second shape

break;

case MotionEvent.ACTION_UP:
    ///Gets the last shape where the user released their fingers
    endView = getChildForTouch((TableLayout) v, x, y);
break;

Delete method inside the: DotView extends View class:

private static class DotView extends View {

        private static final int DEFAULT_SIZE = 100;
        private Paint mPaint = new Paint();
        private Rect mBorderRect = new Rect();
        private Paint mCirclePaint = new Paint();
        private int mRadius = DEFAULT_SIZE / 4;

        public DotView(Context context) {
            super(context);
            mPaint.setStrokeWidth(2.0f);
            mPaint.setStyle(Style.STROKE);
            mPaint.setColor(Color.RED);
            mCirclePaint.setColor(Color.CYAN);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.parseColor("#0099cc"));
            mBorderRect.left = 0;
            mBorderRect.top = 0;
            mBorderRect.right = getMeasuredWidth();
            mBorderRect.bottom = getMeasuredHeight();
            canvas.drawRect(mBorderRect, mPaint);
            canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2,
                    mRadius, mCirclePaint);
        }


        public void delete(){
        mPaint.setColor(Color.TRANSPARENT);
        }

    }

Just something simple to fake delete the circles

Thanks in advance guys.. more codes can be provided if need be.

Edit: If I can accomplish this in any other way please feel free to share. (Draw circles on a Grid and delete the ones I used my finger to draw my line over)


Solution

  • First of all try changing your delete() method to this:

    public void delete(){
        mPaint.setColor(Color.TRANSPARENT);
        invalidate();
    }
    

    You need to let your View know that you want it to redraw itself (that's why the invalidate() call).