1. 程式人生 > >Glide4.0整合及使用注意事項

Glide4.0整合及使用注意事項

1、由於Glide4.0和Glide3.x的用法出入很大 在此總結一下。
在Glide3.x中 載入網路圖片通常習慣預設顯示預載入和載入失敗的圖片
String path= "https://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg";
Glide.with(activity).load(path).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
在4.0中就不能直接呼叫如上的方法了,需要使用RequestOption這個類,具體用法如下:
RequestOptions options = new RequestOptions()
        .centerCrop()
        .placeholder(R.mipmap.ic_launcher)
        .error(R.mipmap.ic_launcher)
        .priority(Priority.HIGH);
Glide.with(this).load(path).apply(options).into(imageView);
2、使用Glide4.0顯示圓形圖片
首先我們需要建立一個轉換圓形的類,具體程式碼如下:
public class GlideCircleTransform extends BitmapTransformation {


    public GlideCircleTransform() {
        super();
    }


    @Override
    public Bitmap transform(BitmapPool pool, Bitmap toTransform,
                          int outWidth, int outHeight) {
        return circleCrop(pool, toTransform);
    }


    private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;
        int size = Math.min(source.getWidth(), source.getHeight());
        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;
        // TODO this could be acquired from the pool too
        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);
        return result;
    }


    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {


    }
}

然後定義RequestOptions:
RequestOptions options = new RequestOptions()
        .centerCrop()
        .placeholder(R.mipmap.ic_launcher)
        .error(R.mipmap.ic_launcher)
        .priority(Priority.HIGH)
        .transform(new GlideCircleTransform());
呼叫glide顯示圖片:
Glide.with(this).load(path).apply(options).into(imageView);

3、使用Glide4.0顯示帶圓角的圖片
一樣需要建立一個可以顯示圓角圖片的轉換類
public class GlideRoundTransform extends BitmapTransformation {
    private static float radius = 0f;


    public GlideRoundTransform() {
        this(4);
    }


    public GlideRoundTransform(int dp) {
        super();
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }


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


    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;


        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }


        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        return result;
    }


    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {


    }
}
然後定義RequestOptions:
RequestOptions options = new RequestOptions()
        .centerCrop()
        .placeholder(R.mipmap.ic_launcher)
        .error(R.mipmap.ic_launcher)
        .priority(Priority.HIGH)
        .transform(new GlideRoundTransform());
呼叫glide顯示圖片:
Glide.with(this).load(path).apply(options).into(imageView);


以上就是總結出Glide4.0的部分新用法。
備註:定義的RequestOption是可以建立一次多次使用 不需要載入一次就去建立一次。