如何将半圆蒙版应用于ImageView [英] How to apply half circle mask to an ImageView
问题描述
我有一个图像,半圆帧图像,我需要将该图像放在此框架内。但是我需要对我的图像应用蒙版,因此它只显示在帧内。
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.
例如这是我的图像:
我想要的结果应该是这样的:
And my desired result should be like that:
红框也是内部透明的图像视图。
The red frame also an image view which inside is transparent.
如何在Android中实现这一目标?
How can I achieve this in Android?
推荐答案
有一个关于设计Android博客分为四部分,介绍如何实现这一目标。
There's a great tutorial on Styling Android blog in four parts that explains how you can achieve this.
编辑:
我已编辑过c ode在本教程的第二部分中创建了效果:
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;
}
我刚刚更换了 drawRoundRect
在代码的末尾,带有 drawOval
,它实际上绘制了一个圆圈,其中一半是画布外的。
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.
这篇关于如何将半圆蒙版应用于ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!