1.最近公司項(xiàng)目?jī)?nèi)存占用較高辟灰,加上GIF圖片掉禎绩脆,發(fā)現(xiàn)gilded官網(wǎng)升級(jí)到最新的4.0.0(正式版)
解決了這個(gè)問題 話不多說直接上代碼禽笑。
2.解決加載本地APP圖標(biāo)錯(cuò)誤
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())// 本地應(yīng)用圖片加載攔截器
.append(String.class, InputStream.class, new WebpUrlLoader.Factory())
.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());//webp圖標(biāo)加載攔截器
引用:
compile 'com.github.bumptech.glide:glide:4.0.0'
compile 'com.github.bumptech.glide:okhttp3-integration:4.0.0@aar'
b.構(gòu)建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:重點(diǎn)編寫轉(zhuǎn)換的包名的類
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.調(diào)用代碼
getRequestManager(context)
.load(AppIcon.parseAppIcon(packName))
.apply(imageDefaultOptions(R.drawable.default_app_icon))
.into(imageView);
3.代碼可能不全株茶,最近我會(huì)傳上我的github
4.新手小白 Markdown 編輯器 不是特別會(huì)用恕洲,分享用glide中遇到的坑.