androidimagebitmapmaskingporter-duff

Masking(crop) image in frame


Having a rich UI application in which I want to show image with complex shape like this

enter image description here

Now what I want is to crop my image as per Mask image, Actually image is coming dynamic and can be imported from Camera or Gallery(square or rectangle shape) and I want that image to fit in my layout frame like above

So just wondering that how do I have achieve this? Any idea /hint welcome

Background frame
enter image description here
Mask
enter image description here

Like this


Solution

  • Finally got the solution while changing mask image and using of Xfermode with Bitmap

    Mask

    enter image description here

     ImageView mImageView= (ImageView)findViewById(R.id.imageview_id);
     Bitmap original = BitmapFactory.decodeResource(getResources(),R.drawable.content_image);
     Bitmap mask = BitmapFactory.decodeResource(getResources(),R.drawable.mask);
     Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
     Canvas mCanvas = new Canvas(result);
     Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
     mCanvas.drawBitmap(original, 0, 0, null);
     mCanvas.drawBitmap(mask, 0, 0, paint);
     paint.setXfermode(null);
     mImageView.setImageBitmap(result);
     mImageView.setScaleType(ScaleType.CENTER);
     mImageView.setBackgroundResource(R.drawable.background_frame);
    

    see output

    enter image description here

    Source can be found here