Fresco 在RecyclerView中的優(yōu)化

參考地址:

https://code.luasoftware.com/tutorials/android/android-fresco-optimize-performance-for-recyclerview/

RecylerView

Consider RecyclerView.hasFixedSize

RecyclerView can perform several optimizations if it can know in advance that RecyclerView’s size is not affected by the adapter contents. RecyclerView can still change its size based on other factors (e.g. its parent’s size) but this size calculation cannot depend on the size of its children or contents of its adapter (except the number of items in the adapter).

If your use of RecyclerView falls into this category, set this to true. It will allow RecyclerView to avoid invalidating the whole layout when its adapter contents change.

class LocalAdapter() : RecyclerView.Adapter<LocalAdapter.ViewHolder>() {
 private var items = ...
?
 init {
 setHasStableIds(true)
 }
?
 override fun getItemId(position: Int): Long {
 // return items[position].id
 return items[position].hashCode().toLong()
 }
}

Fresco

Enable Fresco downsampling

val config = ImagePipelineConfig.newBuilder(context)
 .setDownsampleEnabled(true)
 .build()
Fresco.initialize(context, config)

Use ResizeOptions

// I am using GridLayout with 3 columns
val estimatedImageSize = Resources.getSystem().displayMetrics.widthPixels / layoutManager.spanCount
val adapter = LocalAdapter(viewModel, estimatedImageSize)</pre>
class LocalAdapter(val estimatedImageSize: Int) : RecyclerView.Adapter<LocalAdapter.ViewHolder>() {
?
 private var resizeOptions: ResizeOptions? = null
 private var items = ...
?
 override fun onBindViewHolder(holder: ViewHolder, position: Int) {
 val item = items[position]
?
 // try to get exact size at runtime when DraweeView is rendered
 val localResizeOptions = if (resizeOptions == null) {
 holder.draweeView.doOnLayout {
 resizeOptions = ResizeOptions(holder.draweeView.width, holder.draweeView.height)
 }
 ResizeOptions(estimatedImageSize, estimatedImageSize)
 }
 else resizeOptions
?
 val request =
 ImageRequestBuilder.newBuilderWithSource(item.uri)
 .setResizeOptions(localResizeOptions)
 .build()
 holder.draweeView.setImageRequest(request)
 }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容