【Android】glide:3.7.0->4.8.0 升级导入——踩坑
You can change or stay the same.There are no rules to this thing. ——本杰明.巴顿
正文
官方文档 https://muyangmin.github.io/glide-docs-cn/doc/download-setup.html
准备工作
gradle添加依赖:
dependencies { implementation ("com.github.bumptech.glide:glide:4.8.0") { exclude group: "com.android.support" } implementation "com.android.support:support-fragment:26.1.0" //注意java和kotlin的依赖compiler的不同 //java annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0' //kotlin //kapt 'com.github.bumptech.glide:compiler:4.8.0'}
环境:
Min Sdk Version - 使用 Glide 需要 min SDK 版本 API 14 (Ice Cream Sandwich) 或更高。
Compile Sdk Version - Glide 必须使用 API 27 (Oreo MR1) 或更高版本的 SDK 来编译。
Support Library Version - Glide 使用的支持库版本为 27。
可以使用不同支持库版本,但根据官方文档,不建议
官方声明
如果你需要使用不同的支持库版本,你需要在你的
build.gradle
文件里去从 Glide 的依赖中去除"com.android.support"
。例如,假如你想使用 v26 的支持库:dependencies { implementation ("com.github.bumptech.glide:glide:4.8.0") { exclude group: "com.android.support" } implementation "com.android.support:support-fragment:26.1.0"}
使用与 Glide 依赖的支持库不同的版本可能会导致一些运行时异常 ,例如:
java.lang.NoSuchMethodError: No static method getFont(Landroid/content/Context;ILandroid/util/TypedValue;ILandroid/widget/TextView;)Landroid/graphics/Typeface; in class Landroid/support/v4/content/res/ResourcesCompat; or its super classes (declaration of 'android.support.v4.content.res.ResourcesCompat' at android.support.v7.widget.TintTypedArray.getFont(TintTypedArray.java:119)
也可能造成 Glide 的 API 生成器失败,从而不能正确地生成
GlideApp
类.
生成GlideApp
Glide 4.0之上多数使用GlideApp进行Api操作。
@GlideModulepublic final class MyAppGlideModule extends AppGlideModule {}
简略步骤:
1.创建某个继承于 AppGlideMoudle 的类
2.使用 @GlideModule 注解 (原理就是使用了annotationProcessor生成,也就是依赖com.github.bumptech.glide:compiler:4.8.0的作用)
3.Make Module app,或者暴力Rebuild,生成GlideApp
踩坑:
- compileSdkVersion: 本来使用26,报错,后改成27
- com.android.support:design:本来使用27.0.0,报错,后改为:27.1.0
- Make Module app:报红出错时,别着急。先Make Module或者Rebuild下。先使项目成功编译通过,才能产生GlideApp,因为Glide新版采用的是编译生成代码的方式,否则无法找到引用类。
新老版本用法差异():官方文档 从v3迁移到v4
旧 | 新 | |
淡入淡出 | .crossFade | .transition(DrawableTransitionOptions.withCrossFade()) |
图片加载监听 | 如下方代码 | |
图片转换成bitmap,gif | asBitmap放在load之后 | asBitmap放在with之后 |
缓存 | .diskCacheStrategy(DiskCacheStrategy.SOURCE) | .diskCacheStrategy(DiskCacheStrategy. AUTOMATIC)//默认策略 |
高斯模糊 | // 设置模糊度(在0.0到25.0之间),默认”25"; 图片缩放比例,默认“1”。 .bitmapTransform(new BlurTransformation(context, 5, 1)) | 如下方代码创建BlurTransformation类 |
缓存策略:
- DiskCacheStrategy.NONE: 表示不缓存任何内容
- DiskCacheStrategy.DATA: 表示只缓存原始图片
- DiskCacheStrategy.RESOURCE: 表示只缓存转换过后的图片
- DiskCacheStrategy.ALL : 表示既缓存原始图片,也缓存转换过后的图片
- DiskCacheStrategy.AUTOMATIC: 表示让Glide根据图片资源智能地选择使用哪一种缓存策略(默认选项)一般采用此选项即可
实例对比:
旧:
Glide.with(context) .load(resouce) .diskCacheStrategy(DiskCacheStrategy.SOURCE)//缓存 // 设置模糊度(在0.0到25.0之间),默认”25"; 图片缩放比例,默认“1”。 .bitmapTransform(new BlurTransformation(context, 5, 1)) .crossFade()//淡入淡出 .listener(new LoadListen()) .into(imageView);//图片加载监听器static class LoadListen implements RequestListener
新:
GlideApp.with(context) .load(resouce) .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//缓存 .apply(RequestOptions.bitmapTransform(new BlurTransformation(5, 1))) .transition(DrawableTransitionOptions.withCrossFade())//淡入淡出 .listener(new LoadListen()) .into(imageView);//图片加载监听器 static class LoadListen implements RequestListener { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { return false; } } /** * 高斯模糊类,在glide升级到4.7后如果没将glide-transformations升级就自己写一个 */ public static class BlurTransformation extends BitmapTransformation { private static final int VERSION = 1; private static final String ID = "BlurTransformation." + VERSION; private static int MAX_RADIUS = 25; private static int DEFAULT_DOWN_SAMPLING = 1; private int radius; private int sampling; public BlurTransformation() { this(MAX_RADIUS, DEFAULT_DOWN_SAMPLING); } public BlurTransformation(int radius) { this(radius, DEFAULT_DOWN_SAMPLING); } public BlurTransformation(int radius, int sampling) { this.radius = radius; this.sampling = sampling; } @Override protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { int width = toTransform.getWidth(); int height = toTransform.getHeight(); int scaledWidth = width / sampling; int scaledHeight = height / sampling; Bitmap bitmap = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.scale(1 / (float) sampling, 1 / (float) sampling); Paint paint = new Paint(); paint.setFlags(Paint.FILTER_BITMAP_FLAG); canvas.drawBitmap(toTransform, 0, 0, paint); bitmap = FastBlur.blur(bitmap, radius, true); return bitmap; } @Override public String toString() { return "BlurTransformation(radius=" + radius + ", sampling=" + sampling + ")"; } @Override public boolean equals(Object o) { return o instanceof BlurTransformation && ((BlurTransformation) o).radius == radius && ((BlurTransformation) o).sampling == sampling; } @Override public int hashCode() { return ID.hashCode() + radius * 1000 + sampling * 10; } @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { try { messageDigest.update((ID + radius + sampling).getBytes("utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
ps:报红不可怕,可以直接运行起来(猜测先执行@注解生成java代码(APT),后再编译)
扩展
忘了配套的第三方库glide-transformations
例如:将 glide-transformations从2.0.2升级至jp.wasabeef:glide-transformations:4.0.1,就可使用高斯模糊
.apply(RequestOptions.bitmapTransform(new BlurTransformation(5,1)))高斯模糊
更多相关文章
- Android积木之图片的生成和保存
- 火爆新东西,仿QQ版本的ResideMenuItem框架(最新QQ版本的)
- IDEA和Android Studio设置自定义背景图片
- 版本更新
- android图片压缩工具类分享
- 『ANDROID』Android实现圆形的图片边角
- PC客户端与Android实现图片传送
- Ubuntu 12.04 Desktop 版本编译 Android 4.0.4 出错解决
- android_常用UI控件_02_EditText_01添加图片到edittext中