记录glide加载图片,设置圆角

admin2024-05-15  2

支持所有角的圆角,自动计算合适的半径,不用担心图片比预定值小导致的圆角过大的问题

修改自:https://blog.csdn.net/qq_15059163/article/details/97613790
增加了指定图片尺寸、解决了图片某些情况下圆角过大的问题

public class GlideRoundCornersTransUtils implements Transformation<Bitmap> {


    private BitmapPool mBitmapPool;
    private int mRadius;//半径
    private int mDiameter;//直径
    private CornerType mCornerType = CornerType.ALL;
    private DisplayMetrics metrics;
    int width, height;


    public GlideRoundCornersTransUtils(Context context, int radius, CornerType type, int width, int height) {
        mBitmapPool = Glide.get(context).getBitmapPool();
        metrics = context.getResources().getDisplayMetrics();
        mRadius = (int) (radius * (metrics.densityDpi / 160f));
        mCornerType = type;
        mDiameter = 2 * mRadius;
        this.width = width;
        this.height = height;
    }

    public enum CornerType {
        /**
         * 所有角
         */
        ALL,
        /**
         * 左上
         */
        LEFT_TOP,
        /**
         * 左下
         */
        LEFT_BOTTOM,
        /**
         * 右上
         */
        RIGHT_TOP,
        /**
         * 右下
         */
        RIGHT_BOTTOM,
        /**
         * 左侧
         */
        LEFT,
        /**
         * 右侧
         */
        RIGHT,
        /**
         * 下侧
         */
        BOTTOM,
        /**
         * 上侧
         */
        TOP,
    }

    @Override
    public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
        Bitmap source = resource.get();
        // 剪裁图片到指定的尺寸
        Bitmap croppedBitmap = cropBitmap(source, width, height);
        int width = croppedBitmap.getWidth();
        int height = croppedBitmap.getHeight();
        Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(new BitmapShader(croppedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        drawRoundRect(canvas, paint, width, height);
        return BitmapResource.obtain(bitmap, mBitmapPool);
    }

    private Bitmap cropBitmap(Bitmap source, int targetWidth, int targetHeight) {
        int sourceWidth = source.getWidth();
        int sourceHeight = source.getHeight();
        float srcRatio = (float) sourceWidth / sourceHeight;
        float targetRatio = (float) targetWidth / targetHeight;
        int width, height;
        if (srcRatio > targetRatio) {
            // 源图片更宽,需要剪裁宽度
            width = (int) (sourceHeight * targetRatio);
            height = sourceHeight;
        } else {
            // 源图片更高,需要剪裁高度
            width = sourceWidth;
            height = (int) (sourceWidth / targetRatio);
        }
        // 计算剪裁的起始点
        int x = (sourceWidth - width) / 2;
        int y = (sourceHeight - height) / 2;
        //计算当前适合的半径、直径,因为当前图片的宽高有可能比预定值小,因此,半径、直径也要跟着缩小
        mRadius=mRadius*width/targetWidth;
        mDiameter=mDiameter*width/targetWidth;
        // 剪裁图片
        return Bitmap.createBitmap(source, x, y, width, height);
    }

    private void drawRoundRect(Canvas canvas, Paint paint, float width, float height) {
        Log.e("测试", "全局: width:" + this.width + ",height:" + this.height + ",局部:width:" + width + ",height:" + height+",角度:"+mRadius);
        switch (mCornerType) {
            case LEFT_TOP:
                drawLeftTopCorner(canvas, paint, width, height);
                break;
            case LEFT_BOTTOM:
                drawLeftBottomCorner(canvas, paint, width, height);
                break;
            case RIGHT_TOP:
                drawRightTopCorner(canvas, paint, width, height);
                break;
            case RIGHT_BOTTOM:
                drawRightBottomCorner(canvas, paint, width, height);
                break;
            case LEFT:
                drawLeftCorner(canvas, paint, width, height);
                break;
            case RIGHT:
                drawRightCorner(canvas, paint, width, height);
                break;
            case BOTTOM:
                drawBottomCorner(canvas, paint, width, height);
                break;
            case TOP:
                drawTopCorner(canvas, paint, width, height);
                break;
            case ALL:
            default:
                canvas.drawRoundRect(new RectF(0, 0, width, height), mRadius, mRadius, paint);
                break;
        }
    }


    /**
     * 画左上角
     */
    private void drawLeftTopCorner(Canvas canvas, Paint paint, float width, float height) {
        canvas.drawRect(new RectF(mRadius, 0, width, height), paint);
        canvas.drawRect(new RectF(0, mRadius, mRadius, height), paint);
        canvas.drawArc(new RectF(0, 0, mDiameter, mDiameter), 180, 90, true, paint);
    }

    /**
     * 画左下角
     */
    private void drawLeftBottomCorner(Canvas canvas, Paint paint, float width, float height) {
        canvas.drawRect(new RectF(0, 0, width, height - mRadius), paint);
        canvas.drawRect(new RectF(mRadius, height - mRadius, width, height), paint);
        canvas.drawArc(new RectF(0, height - mDiameter, mDiameter, height), 90, 90, true, paint);
    }

    /**
     * 画右上角
     */
    private void drawRightTopCorner(Canvas canvas, Paint paint, float width, float height) {
        canvas.drawRect(new RectF(0, 0, width - mRadius, height), paint);
        canvas.drawRect(new RectF(width - mRadius, mRadius, width, height), paint);
        canvas.drawArc(new RectF(width - mDiameter, 0, width, mDiameter), 270, 90, true, paint);
    }

    /**
     * 画右下角
     */
    private void drawRightBottomCorner(Canvas canvas, Paint paint, float width, float height) {
        canvas.drawRect(new RectF(0, 0, width, height - mRadius), paint);
        canvas.drawRect(new RectF(0, height - mRadius, width - mRadius, height), paint);
        canvas.drawArc(new RectF(width - mDiameter, height - mDiameter, width, height), 0, 90, true, paint);
    }

    /**
     * 画左 角
     */
    private void drawLeftCorner(Canvas canvas, Paint paint, float width, float height) {
        canvas.drawRect(new RectF(mRadius, 0, width, height), paint);
        canvas.drawRect(new RectF(0, mRadius, mRadius, height - mRadius), paint);
        canvas.drawArc(new RectF(0, 0, mDiameter, mDiameter), 180, 90, true, paint);
        canvas.drawArc(new RectF(0, height - mDiameter, mDiameter, height), 90, 90, true, paint);
    }

    /**
     * 画右角
     */
    private void drawRightCorner(Canvas canvas, Paint paint, float width, float height) {
        canvas.drawRect(new RectF(0, 0, width - mRadius, height), paint);
        canvas.drawRect(new RectF(width - mRadius, mRadius, width, height - mRadius), paint);
        canvas.drawArc(new RectF(width - mDiameter, 0, width, mDiameter), 270, 90, true, paint);
        canvas.drawArc(new RectF(width - mDiameter, height - mDiameter, width, height), 0, 90, true, paint);
    }

    /**
     * 画上 角
     */
    private void drawTopCorner(Canvas canvas, Paint paint, float width, float height) {
        canvas.drawRect(new RectF(0, mRadius, width, height), paint);
        canvas.drawRect(new RectF(mRadius, 0, width - mRadius, mRadius), paint);
        canvas.drawArc(new RectF(0, 0, mDiameter, mDiameter), 180, 90, true, paint);
        canvas.drawArc(new RectF(width - mDiameter, 0, width, mDiameter), 270, 90, true, paint);
    }

    /**
     * 画下 角
     */
    private void drawBottomCorner(Canvas canvas, Paint paint, float width, float height) {
        canvas.drawRect(new RectF(0, 0, width, height - mRadius), paint);
        canvas.drawRect(new RectF(mRadius, height - mRadius, width - mRadius, height), paint);
        canvas.drawArc(new RectF(0, height - mDiameter, mDiameter, height), 90, 90, true, paint);
        canvas.drawArc(new RectF(width - mDiameter, height - mDiameter, width, height), 0, 90, true, paint);
    }


    @Override
    public String getId() {
        return "RoundedTransformation(radius=" + mRadius + ", diameter=" + mDiameter + ")";
    }
}

调用

 view.post(()->{
		Glide.with(context)
                    .load(glideUrl)
                    .override(view.getWidth(),view.getHeight())
                    .bitmapTransform( new GlideRoundCornersTransUtils(context,radius,
                    CornerType.ALL,view.getWidth(),view.getHeight()))
                    .into(view);
  });
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!