3

假设我有一个完全矩形的图像:
在此处输入图像描述

现在,当我在 中显示它时ImageView,我希望切掉一个角,如下所示: 在此处输入图像描述

我怎样才能在运行时实现这一点?

4

1 回答 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;
    }
于 2012-12-06T10:25:30.580 回答