序言
glide可以在load的时候传入一个资源id来加载本地图标,但是在开发过程中。还得区分数据类型来分别处理。这样的使用成本比较大。希望通过自定义ModelLoader实现通过自定义的url来加载Drawab。降低使用成本
实现
一共四个类
类名 | 作用 |
---|---|
GlideIcon | 通过自定义url的编码和提取值的作用 |
GlideIconModelLoader | 自定义的ModelLocader实现自定义的url到drawable的获取过程 |
LocalFetcher | 获取drawab |
MyGlideModule | 用来注册自定义的配置 |
GlideIcon
package com.zhuguohui.app.lib.glide;
import android.util.Log;
import com.zhuguohui.app.lib.app.BaseApp;
/**
* <pre>
* Created by zhuguohui
* Date: 2024/6/5
* Time: 13:43
* Desc:
* </pre>
*/
public class GlideIcon {
static String ICON_START = "GlideIcon://";
public static boolean isIcon(String url) {
return url != null && url.startsWith(ICON_START);
}
public static String get(int drawableId) {
return ICON_START + drawableId;
}
public static int getValue(String url) throws Exception {
if (isIcon(url)) {
String resourceName = url.substring(ICON_START.length());
return Integer.parseInt(resourceName);
}
throw new IllegalArgumentException(url + " is not a glideIcon");
}
}
GlideIconModeLoader
package com.zhuguohui.app.lib.glide;
import android.graphics.drawable.Drawable;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
/**
* <pre>
* Created by zhuguohui
* Date: 2024/6/5
* Time: 13:38
* Desc:
* </pre>
*/
public class GlideIconModeLoader implements ModelLoader<String, Drawable> {
static class KeyImpl implements Key {
String url;
public KeyImpl(String url) {
this.url = url;
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
messageDigest.update(url.getBytes(StandardCharsets.UTF_8));
}
}
@Override
public LoadData<Drawable> buildLoadData(String s, int width, int height, Options options) {
return new LoadData<>(new KeyImpl(s), new LocalFetcher(s));
}
@Override
public boolean handles(String s) {
return GlideIcon.isIcon(s);
}
public static ModelLoaderFactory<String, Drawable> factory=new ModelLoaderFactory<String, Drawable>() {
@Override
public ModelLoader<String, Drawable> build(MultiModelLoaderFactory multiFactory) {
return new GlideIconModeLoader();
}
@Override
public void teardown() {
}
};
}
LocalFetcher
package com.zhuguohui.app.lib.glide;
import android.graphics.drawable.Drawable;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.data.DataFetcher;
import com.zhuguohui.app.lib.app.BaseApp;
import java.io.InputStream;
import java.util.BitSet;
/**
* <pre>
* Created by zhuguohui
* Date: 2024/6/5
* Time: 11:22
* Desc:
* </pre>
*/
public class LocalFetcher implements DataFetcher<Drawable> {
String url;
public LocalFetcher(String url) {
this.url = url;
}
@Override
public void loadData(Priority priority, DataCallback<? super Drawable> callback) {
try {
int id = GlideIcon.getValue(url);
callback.onDataReady(BaseApp.app().getResources().getDrawable(id));
} catch (Exception e) {
callback.onLoadFailed(e);
}
}
@Override
public void cleanup() {
}
@Override
public void cancel() {
}
@Override
public Class<Drawable> getDataClass() {
return Drawable.class;
}
@Override
public DataSource getDataSource() {
return DataSource.MEMORY_CACHE;
}
}
MyGlideModule
package com.zhuguohui.app.lib.glide;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
import java.io.InputStream;
/**
* <pre>
* Created by zhuguohui
* Date: 2024/6/5
* Time: 11:11
* Desc:
* </pre>
*/
@GlideModule
public class MyGlideModule extends AppGlideModule {
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
super.registerComponents(context, glide, registry);
registry.prepend(String.class, Drawable.class, GlideIconModeLoader.factory);
}
}
使用
注册
需要再gralde中导入下面两个依赖, 特别是 注解处理器没有这个无法注册
//glide --start
api 'com.github.bumptech.glide:glide:4.16.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
//glide --end
代码中
直接这样用就行了
String imageUrl = GlideIcon.get(R.mipmap.icon_xiaoxi_xitong);
位置
MyGlideModule 这个类可以在lib module中。
注意
bug
在使用 .transition(DrawableTransitionOptions.with(factory)) 实现淡入淡出的过程中发现第一次显示的时候无法显示本地图片。后来查了一下这是glide的bug。后面有空再来处理。
Glide.with(getContext())
.load(url)
// .transition(DrawableTransitionOptions.with(factory))
.apply(options)
.into(imageView);
经过检测上面的bug是因为使用了第三方库
CircleImageView