I am developing an application that needs a function in an object.
I have an image A that covers image B. With a finger, I need to erase image A to show image B. Erasing should follow your current image of finger A
I am trying to use some kind of code, but still I could not erase the image A. This is the code I use to draw a line on the image (_imageToErase is Image A):
Canvas canvas; Paint paint; float downx = 0, downy = 0, upx = 0, upy = 0; ImageView _imageToErase; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.g_layout); _imageToErase = (ImageView) findViewById(R.id.image_to_erase); _imageToErase.setOnTouchListener(this); } @Override public void onWindowFocusChanged(boolean hasFocus){ int width = _imageToErase.getWidth(); int height = _imageToErase.getHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); canvas = new Canvas(bitmap); paint = new Paint(); paint.setColor(Color.WHITE); paint.setStrokeWidth(25); paint.setAntiAlias(true); _imageToErase.setImageBitmap(bitmap); } public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action){ case MotionEvent.ACTION_DOWN: downx = event.getX(); downy = event.getY(); break; case MotionEvent.ACTION_MOVE: upx = event.getX(); upy = event.getY(); canvas.drawLine(downx, downy, upx, upy, paint); _imageToErase.invalidate(); downx = upx; downy = upy; break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_CANCEL: break; default: break; } return true; }
This code only creates a line that follows the finger, but does not delete the image.
How to change this code to erase the image? Thanks
EDIT
The link suggested in the comments did not solve my problem. Just add this line:
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
not working for me.
source share