fun loadPreviewImage(context: Context, url: String, target: ImageView) {
val requestOptions = RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) //關鍵代碼,加載原始大小
.format(DecodeFormat.PREFER_RGB_565) //設置為這種格式去掉透明度通道,可以減少內存占有
.placeholder(R.drawable.img_error)
.error(R.drawable.img_error)
Glide.with(context)
.setDefaultRequestOptions(requestOptions)
.load(url)
.into(target)
}
針對於大圖,有個很好用的圖片顯示框架,使用的是Bitmap的分塊加載技術:https://github.com/davemorrissey/subsampling-scale-image-view
不過該庫結合使用Glide加載網絡圖片時,需要先使用Glide的downloadOnly把圖片下載到本地,再顯示,這樣才能發揮出分塊加載的性能優勢。