自定义ImageView的形状

3

假设我拥有一幅完整的矩形图片:
enter image description here

现在,当我在ImageView中展示图片时,我想要将一个角剪掉,就像这样: enter image description here

我该如何在运行时实现这个效果?


4
我建议您在imageView上放置一个口罩图片,那通常是最简单的方法。 - yahya
角落应该是透明的还是...? - user
@yahya 谢谢,我会研究一下。或许有什么例子吗? - nhaarman
@Luksprog 是的,那应该是透明的。 - nhaarman
1个回答

4
我用以下代码解决了它:
    public static Bitmap maskImage(Context context, Bitmap original) {
            if (original == null)
                    return null;

            Bitmap result = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Config.ARGB_8888);
            Canvas c = new Canvas(result);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(android.graphics.Color.WHITE);
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);

            Path path = new Path();
            path.moveTo(result.getWidth(), result.getHeight());
            path.lineTo(result.getWidth() - dpToPx(context, CORNERWIDTHDP), result.getHeight());
            path.lineTo(result.getWidth(), result.getHeight() - dpToPx(context, CORNERHEIGHTDP));

            path.close();

            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

            c.drawBitmap(original, 0, 0, null);
            c.drawPath(path, paint);

            paint.setXfermode(null);
            return result;
    }

1
我无法解决dptopx和Cornerwidthdp,请帮助我解决这个问题,以及在哪里使用mask函数。 - Abhishek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接