How to apply half circle mask to an ImageView(如何将半圆形蒙版应用于 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.
我已经在教程的第二部分编辑了代码并创建了效果:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将半圆形蒙版应用于 ImageView
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01