基于現(xiàn)有項目存在大量高清美圖展示的模塊陋率,所以在使用并對比了Glide和fresco的加載效果及使用體驗后定下來的,兩個框架都非常優(yōu)秀但其側(cè)重點略有不同之所以會選擇Glide是因為本人挺喜歡Glide的API風格风罩,簡單方便而且不會涉及到自定義view.
Glide地址:https://bumptech.github.io/glide/
本次側(cè)重點會放在對應用的內(nèi)存管理上來,當然對于圖片的處理也是內(nèi)存管理相當重要的一部分.
先上效果圖
meitu_list.png
meitu_detail_0.png
meitu_detai_1.png
meitu_preview.png
使用步驟
Glide添加
compile('com.github.bumptech.glide:glide:4.6.1') {
exclude group: "com.android.support"
}
// glide kotlin 的工具包
kapt 'com.github.bumptech.glide:compiler:4.6.1'
compile "com.github.bumptech.glide:okhttp3-integration:4.5.0"
compile 'com.github.bumptech.glide:annotations:4.6.1'
封裝圖片加載類
目前只提供了簡單的封裝颂砸,當然你也可以根據(jù)項目需求繼續(xù)進行拓展
/**
* Created by moment on 2018/2/6.
*/
class ImageLoad {
open fun load(context: WeakReference<Context>, url: String?, image: ImageView?) {
if (image == null) return
// 具體圖片加載實現(xiàn)可以使用第三方框架加載,也可以自己實現(xiàn)其垄,
這里提供Glide4.0的使用示例:
var requestOptions = RequestOptions().centerCrop()
.placeholder(R.drawable.default_banner)
.error(R.drawable.default_banner)
.transform(CenterCrop())
.format(DecodeFormat.PREFER_RGB_565)
.priority(Priority.LOW)
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
Glide.with(context.get()!!.applicationContext)
.load(url)
.apply(requestOptions)
.into(object : DrawableImageViewTarget(image) {
})
}
open fun load(context: WeakReference<Context>, url: String?, image: ImageView?, transformation: BitmapTransformation) {
if (image == null) return
// 具體圖片加載邏輯
}
open fun load(holder: Int, context: WeakReference<Context>, url: String, image: ImageView?, width: Int, height: Int) {
if (image == null) return
// 具體圖片加載邏輯
}
open fun loadCircle(context: WeakReference<Context>, url: String?, image: ImageView?, width_height: Int) {
if (image == null) return
// 具體圖片加載邏輯
}
open fun loadRound(context: WeakReference<Context>, url: String, image: ImageView?, width: Int, height: Int, round: Int) {
if (image == null) return
// 具體圖片加載邏輯
}
open fun clearCache(context: WeakReference<Context>) {
// 強制清楚緩存,可以為內(nèi)存緩存也可以為硬盤緩存
Glide使用示例:
Glide.get(context.get()!!.applicationContext).clearMemory()
System.gc()
}
}
使用說明
// 加載圓形頭像
ImageLoad().loadCircle(WeakReference(mContext), remark.user_info.portrait, viewHolder.civ_avatar,40)
// 加載正常圖片
ImageLoad().load(WeakReference(mContext), news.image_1, holder.imageView, width, height)
// 加載圓角圖片
ImageLoad().loadRound(WeakReference(mContext), briefCard["icon"].toString(), holder.image, 5)
在列表加載圖片時會使應用的內(nèi)存上升卤橄,但Glide提供給我們一個API來減少在列表加載時會損耗不必要的內(nèi)存的方法绿满,以recyclerview 為例:
recyclerview.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
when (newState) {
2 -> { // SCROLL_STATE_FLING
Glide.with(activity.applicationContext).pauseRequests()
}
0 -> { // SCROLL_STATE_IDLE
Glide.with(activity.applicationContext).resumeRequests()
}
1 -> { // SCROLL_STATE_TOUCH_SCROLL
Glide.with(activity.applicationContext).resumeRequests()
}
}
}
})
在列表滑動過程中我們可以調(diào)用pauseRequests方法來是圖片暫停加載,當滑動結(jié)束后再調(diào)用resumeRequests來恢復加載.當然要想降低應用內(nèi)存開銷的話也可以調(diào)用ImageLoad().clearCache(WeakReference(this@MainActivity.applicationContext))來清空Glide的內(nèi)存緩存
具體使用方法及使用細節(jié)詳見仿開眼視頻Android客戶端 歡迎大家多多star.