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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

Android Gldie复用只取之前decode过的缓存resourceKotlin

 

 

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.launch


class MainActivity : AppCompatActivity() {
    private val TAG = "Glide/${this::class.simpleName}"

    companion object {
        const val FAIL = -1
        const val SUCCESS = 1
        const val SIZE = 400
    }

    private val mCrop = CenterCrop()
    private val resId = R.mipmap.pic2
    private var mImageView: ImageView? = null

    private 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 onLoadFailed(
                    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 onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Bitmap>,
                        isFirstResource: Boolean
                    ): Boolean {
                        Log.d(TAG, "cache onLoadFailed")
                        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,kotlin1在实际的开发中虽然Glide解决了快速加载图片的问题但还有一个问题悬而未决比如用户的头像往往用户的头像是从服务器端读出的一个普通矩形图片但是现在的设计一般要求在APP端的用户头像显示成圆形头像那么此时虽然Glide可以加载但加载出来的是一个矩形如果要Glide_android 毛玻璃圆角。文章浏览阅读353次。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。https://blog.csdn.net/zhangphil/article/details/134051794

 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: android