Subsampling Scale Image View自身只支持本地图片的加载,但项目中还需要显示网络图片,不想分情况选用不同的控件,所以通过Glide将图片下载到本地缓存再显示
//使用Glide下载图片,保存到本地
Glide.with(context)
.download(uri)
.into(new SimpleTarget<File>() {
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
Log.d("load failed", "nothing");
}
@Override
public void onResourceReady(File resource, Transition<? super File> transition) {
mPlaceHolder.setVisibility(GONE);
imageView.setImage(ImageSource.uri(resource.getAbsolutePath()));
imageView.setMaxScale(10f);
}
});一开始是通过Glide获取bitmap的
Glide.with(context)
.load(image)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
}
});但这样还要自己处理保存图片啥的,一开始没异步保存文件,导致viewpager滑动卡顿,后来搜了下Glide能直接获取缓存file的,就方便多了。
虽然实现一个功能的方法有很多种,但果然还是要多查多看多了解api,能省去不少力气~
本文介绍如何结合Subsampling Scale Image View和Glide在Android项目中加载网络图片。通过Glide将图片下载并缓存到本地,然后在Subsampling Scale Image View中展示,避免了因手动处理图片加载导致的性能问题和复杂性。
2241

被折叠的 条评论
为什么被折叠?



