gpt4 book ai didi

android - ImageView 的自定义形状

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:51 26 4
gpt4 key购买 nike

假设我有一个完全矩形的图像:
enter image description here

现在,当我在 ImageView 中显示它时,我希望切掉一个角,如下所示: enter image description here

如何在运行时实现这一点?

最佳答案

我已经用这段代码解决了这个问题:

    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;
}

关于android - ImageView 的自定义形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13494603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com