Scratch the screen to display the image on android

I am working on an Android project for my assignment. I'm trying to create a scratch application, you know how we scratch the screen to get rid of the blocking layer to display the image. but the problem is that I don’t know where to start.

I have a search in stackoverflow questions related to this but that don't help. from my search there, I found a key for this project using Bitmap.getPixel(int x, int y) .

so in my thought I need to get a pixel from a bitmap and draw it on canvas. but i don't know how to implement it? or who has a better way to do this?

Can anyone help me out? Any textbooks on similar things or related topics?

Thanks in advance!


here is my sample code:

 @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); tw = w; th = h; eraseableBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(eraseableBitmap); Bitmap muteableBitmap = Bitmap.createBitmap(eraseableBitmap.getWidth(), eraseableBitmap.getHeight(), Bitmap.Config.ARGB_8888); } @Override public boolean onTouchEvent(MotionEvent event) { static_x = event.getX(); static_y = event.getY(); if (event.getAction() == MotionEvent.ACTION_DOWN) { touch_start(static_x, static_y); } if (event.getAction() == MotionEvent.ACTION_MOVE) { touch_move(static_x, static_y); } if (event.getAction() == MotionEvent.ACTION_UP) { touch_up(); } return true; } 

Here is the look of my project:

layout

+4
source share
4 answers

Interest Ask. My theoretical plan:

You have 2 bitmaps, an “erased” bitmap and a “hidden” bitmap. Erasing pixels from an existing bitmap is not possible, since the Bitmap in Android is immutable . Therefore, instead of erasing pixels from the “erased” bitmap to show the bitmap below, first draw the “erased” bitmap. Next, create a blank, the modified bitmap . Flip all the pixels in the “hidden” bitmap, showing only where the “erased” bitmap was “erased”.

 Bitmap mutableBitmap = Bitmap.create(erasableBitmap.getWidth(),erasableBitmap.getHeight(), Bitmap.Config.ARGB_8888); for(Pixel erasedPixel : erasedList) { mutableBitmap.setPixel(x,y, hiddenBitmap.getPixel(erasedPixel.x, erasedPixel.y)); } ... // in a different file class Pixel{ int x, y; } 

you will need to fill in the erasedList yourself.

Once you're done, draw on the canvas like this:

 canvas.drawBitmap(0,0,eraseableBitmap); canvas.drawBitmap(0,0,mutableBitmap); 

make sure you first draw a “erasable” raster map so that it is drawn with new pixels.

If you need help figuring out how to set erased pixels, let me know in the comments and I will help you.

EDIT

To determine which pixels the user tried to erase: in your opinion, capture the onTouch event and you will get the coordinates where the user touched the screen. Save this for any map or hash table, and you should be fine. Create a Pixel object and add it to the global Pixel List .

EDIT 2

To increase the size of the “scratch”, what you need to do is a little complicated. You need to somehow create an area around the x, y point, affected just like the erased one. A circle would be perfect, but it would be easier to use a square.

 for(Pixel erasedPixel: erasedList) { //it actually more complicated than this, since you need to check for boundary conditions. for(int i = -SQUARE_WIDTH/2; i < SQUARE_WIDTH/2; i++){ for(int j = -SQUARE_WIDTH/2; j < SQUARE_WIDTH/2; j++){ mutableBitmap.setPixel(erasedPixel.x+i, erasedPixel.y+j, hiddenBitmap.getPixel(erasedPixel.x+i, erasedPixel.y+j)); } } } 
+7
source

I created a call to the WScrarchView library, where you can implement scratch view of just a few lines in an xml layout. Hope this helps those still looking for a similar solution https://github.com/winsontan520/Android-WScratchView

Of course, you can copy the codes and customize based on your needs :)

+4
source

I think the simplest solution would be two layers on top of each other (easily reachable with RelativeLayout). The bottom layer will contain any content that needs to be expanded. The top layer is a canvas filled with an opaque color. When you discover the start of a touch event, trace your finger and paint a fully transparent color, replacing the old color in the same way. You can find guides on how to draw with your finger here.

+1
source

You can use the Samsung S-Pen SDk to clean the image for free, but it works from version 2.3

-1
source

All Articles