如何将半圆形蒙版应用于 ImageView

How to apply half circle mask to an ImageView(如何将半圆形蒙版应用于 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

基础教程推荐