Fresco 在RecyclerView中的优化

参考地址:

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() {
 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)
class LocalAdapter(val estimatedImageSize: Int) : RecyclerView.Adapter() {
​
 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)
 }
}

你可能感兴趣的:(Fresco 在RecyclerView中的优化)