当前位置:首页 > 编程笔记 > 正文
已解决

Android Gldie复用只取之前decode过的缓存resource,Kotlin

来自网友在路上 175875提问 提问时间:2023-11-06 14:05:30阅读次数: 75

最佳答案 问答题库758位专家为你答疑解惑

Android Gldie复用只取之前decode过的缓存resource,Kotlin

 

 

import android.graphics.Bitmap
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launchclass MainActivity : AppCompatActivity() {private val TAG = "Glide/${this::class.simpleName}"companion object {const val FAIL = -1const val SUCCESS = 1const val SIZE = 400}private val mCrop = CenterCrop()private val resId = R.mipmap.pic2private var mImageView: ImageView? = nullprivate val mChannel = Channel<Int>()override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val imageView = findViewById<ImageView>(R.id.image)mImageView = findViewById(R.id.image2)GlideApp.with(this).asBitmap().load(resId).transform(mCrop).override(SIZE, SIZE).addListener(object : RequestListener<Bitmap> {override fun onl oadFailed(e: GlideException?,model: Any?,target: Target<Bitmap>,isFirstResource: Boolean): Boolean {Log.d(TAG, "onLoadFailed")signal(FAIL)return false}override fun onResourceReady(resource: Bitmap,model: Any,target: Target<Bitmap>?,dataSource: DataSource,isFirstResource: Boolean): Boolean {Log.d(TAG, "onResourceReady")signal(SUCCESS)return false}}).into(imageView)waitReceive()}fun signal(s: Int) {lifecycleScope.launch(Dispatchers.IO) {mChannel.send(s)}}private fun waitReceive() {lifecycleScope.launch(Dispatchers.IO) {mChannel.receiveAsFlow().collect {Log.d(TAG, "collect $it")if (it == SUCCESS) {fetchCacheBitmap()}}}}private fun fetchCacheBitmap() {val bitmap = runCatching {GlideApp.with(this@MainActivity).asBitmap().load(resId).transform(mCrop).onlyRetrieveFromCache(true) //从内存或者glide decode好的resource里面取,不去原始decode.override(SIZE, SIZE).addListener(object : RequestListener<Bitmap> {override fun onl oadFailed(e: GlideException?,model: Any?,target: Target<Bitmap>,isFirstResource: Boolean): Boolean {Log.d(TAG, "cache onl oadFailed")return false}override fun onResourceReady(resource: Bitmap,model: Any,target: Target<Bitmap>?,dataSource: DataSource,isFirstResource: Boolean): Boolean {Log.d(TAG, "cache onResourceReady")return false}}).submit().get() //不要在这里加timeout值}.onFailure {Log.d(TAG, "取Glide缓存失败:${it.message}")}.onSuccess {Log.d(TAG, "取Glide缓存成功:${it.byteCount}")}.getOrNull()Log.d(TAG, "getOrNull=${bitmap?.byteCount}")if (bitmap != null) {lifecycleScope.launch(Dispatchers.Main) {//之前的缓存资源取到了。//使用之前的缓存,设置到新的ImageView里面。mImageView?.setImageBitmap(bitmap)}}}
}

 

 

e5c745ea179c4c039a14905391124164.png

可以看到,第一次因为是全新的加载,没有缓存,glide只能干脏活,把原始的图片文件decode成resource,花费200+毫秒,而之后,只从缓存(包括磁盘的resource半成品)中取,仅仅花费20+毫秒,时间开销是原先的约1/10,加载速度相当于快了约10倍。

 

 

 

 

 

Android Glide限定onlyRetrieveFromCache取内存缓存submit超时阻塞方式,Kotlin-CSDN博客文章浏览阅读1.4k次。文章浏览阅读638次。【代码】Android Paging 3,kotlin(1)在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬而未决:比如用户的头像,往往用户的头像是从服务器端读出的一个普通矩形图片,但是现在的设计一般要求在APP端的用户头像显示成圆形头像,那么此时虽然Glide可以加载,但加载出来的是一个矩形,如果要Glide_android 毛玻璃圆角。文章浏览阅读353次。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。https://blog.csdn.net/zhangphil/article/details/134051794

 

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"Android Gldie复用只取之前decode过的缓存resource,Kotlin":http://eshow365.cn/6-33680-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!