I have an image, a half circle frame image and I need to put that image inside this frame. But I need to apply mask to my image so it is only displayed inside frame.
For example this is my image:
And my desired result should be like that:
The red frame also an image view which inside is transparent.
How can I achieve this in Android?
There's a great tutorial on Styling Android blog in four parts that explains how you can achieve this.
Edit:
I've edited the code in part two of the tutorial and created the effect:
private Bitmap processImage(Bitmap bitmap) {
Bitmap bmp;
bmp = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bitmap,
BitmapShader.TileMode.CLAMP,
BitmapShader.TileMode.CLAMP);
float radius = bitmap.getWidth() / 2f;
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
RectF rect = new RectF(-bitmap.getWidth() / 2f, 0,
bitmap.getWidth() / 2f, bitmap.getHeight());
canvas.drawOval(rect, paint);
return bmp;
}
I just replaced the drawRoundRect
at the end of the code with drawOval
and it essentially draws a circle that half of it is out of canvas.