vendredi 10 avril 2015

blur image on touch in android

I want to develop an android application like this with some extra features: http://ift.tt/1HCyJse


I am using the following code so far:



class TouchView extends View {
Bitmap bgr;
Bitmap overlayDefault;
Bitmap overlay;
Paint pTouch;
int X = -100;
int Y = -100;
Canvas c2;
private final Path clipPath = new Path();
@SuppressLint("NewApi")
public TouchView(Context context) {
super(context);

if (Build.VERSION.SDK_INT >= 11) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

String file = getIntent().getStringExtra("file");

bitmap = BitmapFactory.decodeFile(file).copy(Config.ARGB_8888, true);


Bitmap newbitmap = Bitmap
.createScaledBitmap(bitmap, 512, 512, true);
Bitmap newtemp = BitmapFactory.decodeFile(file).copy(Config.ARGB_8888, true);
Bitmap temp = Bitmap.createScaledBitmap(newtemp, 512, 512, true);



Bitmap blurredBitmap = fastblur(temp, 10);

bgr = blurredBitmap.copy(Config.ARGB_8888, true);

overlay = newbitmap.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);
}

@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(final Canvas canvas) {
super.onDraw(canvas);

// draw background
canvas.drawBitmap(bgr, 0, 0, null);

c2.drawCircle(X, Y, 50, pTouch);
// draw the overlay over the background
canvas.drawBitmap(overlay, 0, 0, null);
}
}


I am selecting the image from gallery and getting its path from previous activity in "file" string.


Using the above code, if I select a transparent background .png image, it works fine. But when I select some camera images of .jpg format it is not working and show me the black portion on touch areas instead of blur background image. I also tried to convert an image into png but didnt get any success.


I also set hardware acceleration to false in manifest but in vain.


Please help me. Thanks


Aucun commentaire:

Enregistrer un commentaire