Android - Glide4.x的使用

最近从Glidev3升级到了Glide4.x,我用的是最新的Glide4.1.1哈,由于3.x到4.x的变化还是很大的,在此记录一下,也和需要的朋友分享一下。

Glide官方学习地址

使用

1、在app的build.gradle中添加如下配置

dependencies {
//Glide框架引入
compile 'com.github.bumptech.glide:glide:4.1.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.1.1'
}

2、在project的build.gradle中添加如下仓库地址**

repositories {
mavenCentral()
maven { url 'https://maven.google.com' }
}

3、自定义AppGlideModule:
为运用程序定义一个带有@GlideModule注解的AppGlideModule,运用程序会使用和AppGlideMoudle同一个包下的GlideApp类。通过GlideApp.with()方式使用Glide的Generated API。具体操作,就是新建一个类,继承与AppGlideModule,还必须带有@GlideModule注解,

@GlideModule
public final class MyAppGlideModule extends AppGlideModule {

}

注意:

  • 必需带有@GlideModule注解。
  • 官方文档明确说明,这个类,你可以让它保留空白,不用实现任何方法,就可以使用Glide的Generated API。当然你也可以实现一些方法,具体参考官方文档。

4、Glide加载圆形图片,和Glide3.x差异不大

**
* Glide圆形图片
*/

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) {

}
}

//调用
/**
* 加载圆形图片
*/
public static void loadCircleImg(Context context, String url, final ImageView imageView) {
GlideApp.with(context)
.load(url)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.centerCrop()
.transform(new GlideCircleTransform())
.into(imageView);
}

5、Glide加载圆角图片
说道这个我不得不吐槽一下网上的各路大神了,4.x和3.x加载圆角真不一样了,下面举个网上的例子,和3.x差不多

public class GlideRoundTransform extends BitmapTransformation {
}

尼玛,根本显示不了圆角啊,后来查资料才发现原来是Glide圆角和centerCrop的冲突,不过也得到了解决,感谢这位作者

Glide .centerCrop() 和transform圆角图片不能同时存在解决方法

下面贴出我的加载圆角的方法

/**
* 圆角图片
*/

public class GlideRoundTransform extends CenterCrop {
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) {
Bitmap transform = super.transform(pool, toTransform, outWidth, outHeight);
return roundCrop(pool, transform);
// 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) {

}
}

//调用
/**
* 加载圆角图片
*
* @param context
* @param url
* @param imageView
*/
public static void loadRoundImg(Context context, String url, final ImageView imageView) {
GlideApp.with(context)
.load(url)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.centerCrop()
.transform(new GlideRoundTransform(8))
.into(imageView);
}

6、Glide加载图片进度的监听

GlideApp.with(context)
.load(url)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.centerCrop()
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Drawable> target, boolean b) {
Log.i(TAG, "图片加载失败 ");
return false;
}

@Override
public boolean onResourceReady(Drawable drawable, Object o, Target<Drawable> target, DataSource dataSource, boolean b) {
Log.i(TAG, "图片加载完成: ");
return false;
}
})
.into(imageView);
坚持原创技术分享,您的支持将鼓励我继续创作!