0

So I have this 9png : enter image description here

And I have this image : enter image description here

I need to achieve something like this: enter image description here

This is the closest I can get to it: enter image description here

This is how I tried to do it:

public class CustomView extends ImageView {

private Bitmap mImage;
private Bitmap mMask;
private int mPosX = 0;
private int mPosY = 0;

private final Paint maskPaint;
private final Paint imagePaint;

public CustomView(Context context) {
    super(context);
    maskPaint = new Paint();
    maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

    imagePaint = new Paint();
    imagePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));

    mImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.dummy_video);
    mMask = BitmapFactory.decodeResource(context.getResources(), R.drawable.friend_bubble);
}

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

    canvas.save();
    canvas.drawBitmap(mMask, 0, 0, maskPaint);
    canvas.drawBitmap(mImage, mPosX, mPosY, imagePaint);
    canvas.restore();
}

}

My guess is that the 9png just keeps on drawing and doesn't know when to stop.

Can anyone help? :)

kimv
  • 1,569
  • 4
  • 16
  • 26
  • 2
    No, the 9 patch only stretches in a View, not in a Bitmap object itself. Then you have to first make it scale into some View (possibly hidden), then take the scaled version and use that one as a mask. – Phantômaxx Oct 08 '15 at 07:54
  • This will help you to clip images with custom shapes [link](http://stackoverflow.com/questions/12614542/maskingcrop-image-in-frame) – Ayyappan Oct 08 '15 at 08:06
  • 1
    @FrankN.Stein thanks for the tip. it works like that. I'm just not sure whether this is very optimized in a listview. There will be a lot of images – kimv Oct 08 '15 at 08:38
  • 1
    @AyyappanN for some reason the right side of my image does not have rounded corners, and it scales the whole image to the original height of the 9png. anyway I think I'll go with frank's method – kimv Oct 08 '15 at 08:42

0 Answers0