将图片剪裁为圆形

我们时常会用到圆形图片,那么,是对图片进行裁剪,还是自定义圆形的ImageView,如果没有特殊要求的话,我们肯定选择前者,原因很简单,因为简单!!直接上源码,就是这么粗暴任性~~~

/**
     * 将图片剪裁为圆形
     */
    public static Bitmap createCircleImage(Bitmap source) {
        int length = source.getWidth() < source.getHeight() ? source.getWidth() : source.getHeight();
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Bitmap target = Bitmap.createBitmap(length, length, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(target);
        canvas.drawCircle(length / 2, length / 2, length / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(source, 0, 0, paint);
        return target;
    }

只需要输入bitmap,然后就给你返回圆形的bitmap,然后设置到ImageView中,是不是很简单。

这里另外提一点,如果从ImageView中拿出来bitmap,只需要一行代码:

//从ImageView中获取bitmap
        Bitmap bm =((BitmapDrawable) ( imageView).getDrawable()).getBitmap();
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值