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是可以创建一次多次使用 不需要加载一次就去创建一次。


"

阅读更多

没有更多推荐了,返回首页