Make Certain Area of Bitmap Transparent on Touch

Make certain area of bitmap transparent on touch


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class StartActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TouchView(this));


}

class TouchView extends View{
Bitmap bgr;
Bitmap overlayDefault;
Bitmap overlay;
Paint pTouch;
int X = -100;
int Y = -100;
Canvas c2;

public TouchView(Context context) {
super(context);

bgr = BitmapFactory.decodeResource(getResources(),R.drawable.bgr);
overlayDefault = BitmapFactory.decodeResource(getResources(),R.drawable.over);
overlay = BitmapFactory.decodeResource(getResources(),R.drawable.over).copy(Config.ARGB_8888, true);
c2 = new Canvas(overlay);

pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
pTouch.setColor(Color.TRANSPARENT);
pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));


}

@Override
public boolean onTouchEvent(MotionEvent ev) {

switch (ev.getAction()) {

case MotionEvent.ACTION_DOWN: {

X = (int) ev.getX();
Y = (int) ev.getY();
invalidate();

break;
}

case MotionEvent.ACTION_MOVE: {

X = (int) ev.getX();
Y = (int) ev.getY();
invalidate();
break;

}

case MotionEvent.ACTION_UP:

break;

}
return true;
}


@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);

//draw background
canvas.drawBitmap(bgr, 0, 0, null);
//copy the default overlay into temporary overlay and punch a hole in it
c2.drawBitmap(overlayDefault, 0, 0, null); //exclude this line to show all as you draw
c2.drawCircle(X, Y, 80, pTouch);
//draw the overlay over the background
canvas.drawBitmap(overlay, 0, 0, null);

}


}


}

Making touched area of bitmap to transparent

You need to make two changes to get the top bitmap to erase. First, make it a mutable Bitmap so that you can change the contents as it is erased:

Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.aa);
bm1 = temp.copy(Bitmap.Config.ARGB_8888, true); // mutable = true

Be careful about out of memory errors here.

Secondly, in your onDraw function, update the bitmap contents by writing back to bm1:

@Override
protected void onDraw(Canvas canvas) {
// draw a circle that is erasing bitmap
super.onDraw(canvas);
canvas.drawBitmap(bm2, 0, 0, null);
pcanvas.drawBitmap(bm1, 0, 0, null);
pcanvas.drawCircle(x, y, 40, mPaint);
canvas.drawBitmap(bmOverlay, 0, 0, null);

// erase the top bitmap:
Canvas bitmapCanvas = new Canvas(bm1);
bitmapCanvas.drawBitmap(bm2, 0, 0, null);
bitmapCanvas.drawBitmap(bmOverlay, 0, 0, null);
}

Also, to stop a circle being erased in the top left corner when you start the app, create a boolean with a default value of false and set it inside onTouchEvent when you have valid co-ordinates, and check it before calling drawCircle.

Making Overlaid image transparent on touch in Android?

You need to set the PorterDuff mode on a Paint object:

mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)) 

and draw with Color.TRANSPARENT

Also see this thread in the Android developer group:
http://groups.google.com/group/android-developers/browse_thread/thread/5b0a498664b17aa0/de4aab6fb7e97e38?lnk=gst&q=erase+transparent#

Draw transparent lines on bitmap via touch input

Take a look at

Making Overlaid image transparent on touch in Android?

Make a Transparent Canvas over a BitMap

Check to make sure the overlay bitmap you've created is actually transparent.
Create it with config ARGB_8888 explicitly.
http://developer.android.com/reference/android/graphics/Bitmap.Config.html

And you might want to move the creation of the bitmap out of the reDraw method and only create it once instead.



Related Topics



Leave a reply



Submit