【问题标题】:Custom shape of ImageViewImageView 的自定义形状
【发布时间】:2012-12-06 18:25:30
【问题描述】:

假设我有一个完全矩形的图像:

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

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

【问题讨论】:

  • 我建议你在你的 imageView 上放一个遮罩图像...这主要是最简单的方法...
  • 角应该是透明的还是...?
  • @yahya 谢谢,我会调查的。有什么例子吗?
  • @Luksprog 是的,应该是透明的。

标签: android


【解决方案1】:

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

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

【讨论】:

  • 我无法解析 dptopx 和 Cornerwidthdp 请帮助 m 以及在哪里使用遮罩功能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2014-06-16
  • 2017-03-05
  • 2015-06-21
相关资源
最近更新 更多