1.最近公司项目内存占用较高,加上GIF图片掉祯,发现gilded官网升级到最新的4.0.0(正式版)
解决了这个问题 话不多说直接上代码。
2.解决加载本地APP图标错误
a.编写拦截器
Glide.get(this).getRegistry().replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory())//okhttp拦截器
.append(AppIcon.class, ApplicationInfo.class, new ApplicationIconModelLoader.Factory())
.append(ApplicationInfo.class, Drawable.class, new ApplicationIconDecoder())// 本地应用图片加载拦截器
.append(String.class, InputStream.class, new WebpUrlLoader.Factory())
.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());//webp图标加载拦截器
引用:
compile 'com.github.bumptech.glide:glide:4.0.0'
compile 'com.github.bumptech.glide:okhttp3-integration:4.0.0@aar'
b.构建ApplicationIconModelLoader
public class ApplicationIconModelLoader implements ModelLoader<AppIcon, ApplicationInfo> {
public static class Factory implements ModelLoaderFactory<AppIcon, ApplicationInfo> {
@Override
public ModelLoader<AppIcon, ApplicationInfo> build(MultiModelLoaderFactory multiFactory) {
return new ApplicationIconModelLoader();
}
@Override
public void teardown() {
}
}
@Nullable
@Override
public LoadData<ApplicationInfo> buildLoadData(final AppIcon appIcon, int width, int height, Options options) {
return new LoadData<>(new ObjectKey(appIcon), new DataFetcher<ApplicationInfo>() {
@Override
public void loadData(Priority priority, DataCallback<? super ApplicationInfo> callback) {
PackageManager packageManager = AegonApplication.getContext().getPackageManager();
PackageInfo packageInfo = null;
ApplicationInfo applicationInfo = null;
try {
packageInfo = packageManager.getPackageInfo(appIcon.getPackName(),
PackageManager.GET_SIGNATURES);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageInfo != null)
applicationInfo = packageInfo.applicationInfo;
callback.onDataReady(applicationInfo);
}
@Override
public void cleanup() {
}
@Override
public void cancel() {
}
@Override
public Class<ApplicationInfo> getDataClass() {
return ApplicationInfo.class;
}
@Override
public DataSource getDataSource() {
return DataSource.LOCAL;
}
});
}
@Override
public boolean handles(AppIcon appIcon) {
return true;
}
}
c:重点编写转换的包名的类
public class ApplicationIconDecoder implements ResourceDecoder<ApplicationInfo, Drawable> {
@Nullable
@Override
public Resource<Drawable> decode(ApplicationInfo source, int width, int height, Options options)
throws IOException {
Drawable icon = source.loadIcon(AegonApplication.getContext().getPackageManager());
return new DrawableResource<Drawable>(icon) {
@Override
public Class<Drawable> getResourceClass() {
return Drawable.class;
}
@Override
public int getSize() { // best effort
if (drawable instanceof BitmapDrawable) {
return Util.getBitmapByteSize(((BitmapDrawable) drawable).getBitmap());
} else {
return 1;
}
}
@Override
public void recycle() { /* not from our pool */ }
};
}
@Override
public boolean handles(ApplicationInfo source, Options options) throws IOException {
return true;
}
d.调用代码
getRequestManager(context)
.load(AppIcon.parseAppIcon(packName))
.apply(imageDefaultOptions(R.drawable.default_app_icon))
.into(imageView);
3.代码可能不全,最近我会传上我的github
4.新手小白 Markdown 编辑器 不是特别会用,分享用glide中遇到的坑.