Android图片缓存框架 - Glide自定义圆角 (五)

目录

1. Glide API

2. Glide导入包

3. Glide 7种加载图片方式

4. 取消加载

5 Glide加载GIF

6. Glide的RequestOption

7. Glide自定义圆角

7.1 定义圆角类

7.2 添加圆角

8. RequestBuilder

9. Glide设置Listener

10 Glide初始化配置

 


7. Glide自定义圆角

7.1 定义圆角类

定义:CornersTransform

代码如下:

public class CornersTransform extends BitmapTransformation {
    private float radius;

    public CornersTransform(int radius) {
        this.radius = radius;
    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth,
 int outHeight)
   {
        return cornersImage(pool, toTransform);
    }

    private Bitmap cornersImage(BitmapPool pool, Bitmap bitmapSource) {
        Bitmap tempBitmap = null;
        try {
            if (bitmapSource == null) {
                return null;
            }
            tempBitmap = pool.get(bitmapSource.getWidth(), bitmapSource.getHeight(), 
                                 Bitmap.Config.ARGB_8888);
            if (tempBitmap == null) {
                tempBitmap = Bitmap.createBitmap(bitmapSource.getWidth(), 
                                  bitmapSource.getHeight(), Bitmap.Config.ARGB_8888);
            }
            Canvas canvas = new Canvas(tempBitmap);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(bitmapSource, BitmapShader.TileMode.CLAMP, 
                                   BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);

            RectF rectF = new RectF(0f, 0f, bitmapSource.getWidth(), 
                                   bitmapSource.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempBitmap;
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

    }
}

7.2 添加圆角

int radius = 30;
RequestOptions options = new RequestOptions().transform(new CornersTransform(radius));

Glide.with(getApplicationContext()).load(imageUrl).apply(options).into(imageView);

 

你可能感兴趣的:(Android图片缓存框架,Android开发)