ImageView的自定义形状 [英] Custom shape of ImageView

查看:245
本文介绍了ImageView的自定义形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我已经有了一个完整的矩形图像:结果

Say I've got a fully rectangle image:

现在,当我告诉它在的ImageView ,我要切一个角落里了,就像这样:

Now when I show it in an ImageView, I want one corner to be cut off, like this:

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

How can I achieve this on runtime?

推荐答案

我用这个code解决它:

I've solved it using this code:

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

这篇关于ImageView的自定义形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆