Android Glide预加载RecyclerViewPreloader,ViewPreloadSizeProvider,kotlin
文章来源地址https://www.toymoban.com/news/detail-550724.html
implementation 'com.github.bumptech.glide:glide:4.15.1'
implementation ("com.github.bumptech.glide:recyclerview-integration:4.14.2") {
// Excludes the support library because it's already included by Glide.
transitive = false
}
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
import android.content.Context
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.ListPreloader
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader
import com.bumptech.glide.util.ViewPreloadSizeProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
class MainActivity : AppCompatActivity() {
companion object {
const val TAG = "fly"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var recyclerView: RecyclerView = findViewById(R.id.recycler_view)
recyclerView?.layoutManager = GridLayoutManager(this, 3).apply {
orientation = GridLayoutManager.VERTICAL
}
val adapter = MyAdapter()
recyclerView?.adapter = adapter
CoroutineScope(Dispatchers.IO).launch {
val items = readAllImage(applicationContext)
withContext(Dispatchers.Main) {
adapter.onChange(items)
}
}
var preloadSizeProvider = ViewPreloadSizeProvider<MyData>()
val preloadModelProvider = MyPreloadModelProvider(adapter, this)
val preloader = RecyclerViewPreloader(
this,
preloadModelProvider,
preloadSizeProvider,
100 //maxPreload预加载数量
)
recyclerView?.addOnScrollListener(preloader)
}
class MyPreloadModelProvider(
private val adapter: MyAdapter,
private val context: Context
) : ListPreloader.PreloadModelProvider<MyData> {
// 从当前屏幕结束的位置开始,再加载maxPreload条item,
// 不可见的item即为屏幕外的position位置
// 如果对于一个给定的位置不需要加载任何东西,getPreloadItems 可以返回一个空表。
override fun getPreloadItems(position: Int): MutableList<MyData> {
val data: MyData = adapter.getItems()[position]
Log.d(TAG, "getPreloadItems pos:$position index:${data.index}")
return mutableListOf(data)
}
override fun getPreloadRequestBuilder(item: MyData): RequestBuilder<*>? {
Log.d(TAG, "getPreloadRequestBuilder ${item.index}")
return Glide.with(context).load(File(item.path))
}
}
class MyAdapter : RecyclerView.Adapter<MyViewHolder>() {
private var items: ArrayList<MyData>? = null
fun onChange(items: ArrayList<MyData>?) {
this.items = items
notifyDataSetChanged()
}
fun getItems(): ArrayList<MyData> {
return items!!
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item, parent, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
Glide.with(holder.itemView.context).load(File(items?.get(position)?.path))
.into(holder.image)
}
override fun getItemCount(): Int {
return items?.size ?: 0
}
}
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val image: ImageView = itemView.findViewById(R.id.image)
}
private fun readAllImage(context: Context): ArrayList<MyData> {
val photos = ArrayList<MyData>()
//读取手机图片
val cursor = context.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
null
)
var index = 0
while (cursor!!.moveToNext()) {
//图片路径
val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA))
//图片名称
//val name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME))
//图片大小
//val size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE))
photos.add(MyData(path, index++))
}
cursor.close()
return photos
}
class MyData(var path: String, val index: Int)
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp" />
Android Glide自定义AppGlideModule,让Glide在app启动后基于定制化GlideModule加载,kotlin_zhangphil的博客-CSDN博客在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬而未决:比如用户的头像,往往用户的头像是从服务器端读出的一个普通矩形图片,但是现在的设计一般要求在APP端的用户头像显示成圆形头像,那么此时虽然Glide可以加载,但加载出来的是一个矩形,如果要Glide_android 毛玻璃圆角。《Android图片加载与缓存开源框架:Android Glide》Android Glide是一个开源的图片加载和缓存处理的第三方框架。https://blog.csdn.net/zhangphil/article/details/131592226Android load all photos into RecyclerView,pinch to zoom by ScaleGestureDetector,kotlin(4)_zhangphil的博客-CSDN博客Android RecyclerView的StaggeredGridLayoutManager实现交错排列的子元素分组先看实现的结果如图:设计背景:现在的产品对设计的需求越来越多样化,如附录文章2是典型的联系人分组RecyclerView,子元素排列到一个相同的组,但是有些时候,UI要求把这些元素不是垂直方向的,而是像本文开头的图中所示样式排列,这就需要用StaggeredGridLayoutMa。在处理大图的浏览查看动作过程中,往往还有其他额外的事情需要处理,典型的以微信。https://blog.csdn.net/zhangphil/article/details/131296499
Android RecyclerView AsyncListUtil手动刷新fillData,kotlin_zhangphil的博客-CSDN博客基于Android官方Paging Library的RecyclerView分页加载框架我之前写了一篇RecyclerView分页加载机制的文章,是基于Android官方的AsyncListUtil实现的,详情见附录文章1。基于Android官方Paging Library的RecyclerView分页加载框架我之前写了一篇RecyclerView分页加载机制的文章,是基于Android官方的AsyncListUtil实现的,详情见附录文章1。【代码】Android Paging 3,kotlin(1)https://blog.csdn.net/zhangphil/article/details/131519811文章来源:https://www.toymoban.com/news/detail-550724.html
到了这里,关于Android Glide预加载RecyclerViewPreloader,ViewPreloadSizeProvider,kotlin的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!