glide加载圆角和自定义圆角view_Glide加载圆角gif 和圆角图片的三种方法

直接上代码: 看注解

@SuppressLint("AppCompatCustomView")

public class MyTestImageView extends ImageView {

private float width;

private float height;

private float radius;

private Paint paint;

private Matrix matrix;

public MyTestImageView(Context context) {

this(context, null);

}

public MyTestImageView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

paint = new Paint();

paint.setAntiAlias(true);

matrix = new Matrix();

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

width = getMeasuredWidth();

height = getMeasuredHeight();

radius = DensityUtils.dpForPx(3);

}

@SuppressLint("DrawAllocation")

@Override

protected void onDraw(Canvas canvas) {

// 第三种就直接用 PorterDuffXfermode 去交集 DST_IN 来得倒 交集的后 gif 图片展示

//加这个是为了隔离图层,避免黑色的底边

int layerID = canvas.saveLayer(0, 0, width, height, paint, Canvas.ALL_SAVE_FLAG);        @SuppressLint("DrawAllocation") RectF f1 = new RectF(0.0f, 0.0f, width, height);

@SuppressLint("DrawAllocation") Path ra = new Path();

ra.addRoundRect(f1, radius, radius, Path.Direction.CCW);

paint.setColor(ContextCompat.getColor(this.getContext(), R.color.color_fff));

//        canvas.clipPath(ra);//第一种 直接裁剪画布,根据实践有锯齿

super.onDraw(canvas);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

canvas.drawPath(ra, paint);

paint.setXfermode(null);

//回复图层

canvas.restoreToCount(layerID);

// 第二种 给imageview 加相框 不过得适配画笔颜色和布局背景颜色一样。

//        Drawable drawable = getDrawable();

//        if (null != drawable) {

//            @SuppressLint("DrawAllocation")

//            RectF f = new RectF(0.0f, 0.0f, width, height);

//            @SuppressLint("DrawAllocation") Path now = new Path();

//            @SuppressLint("DrawAllocation") Path bg = new Path();

//            bg.addRect(f, Path.Direction.CCW);

//            now.addRoundRect(f, radius, radius, Path.Direction.CCW);

//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)

//                now.op(bg, now, Path.Op.DIFFERENCE);

//            canvas.drawPath(now, paint);

//        }

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值