Android Glide preload LruCache DefaultLifecycleObserver pause/resumeRequests,Kotlin

Android Glide preload LruCache DefaultLifecycleObserver pause/resumeRequests,Kotlin

 

 

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

 

 

 

        val cache = android.util.LruCache<String, WeakReference<Bitmap>>(2000)

        val items = readAllImage(this)
        items.forEachIndexed { index, myData ->
            doPreload(myData, 50, cache)
        }

        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                Log.d(TAG, "onResume")

                GlideApp.with(this@MainActivity).resumeRequests()
            }

            override fun onStop(owner: LifecycleOwner) {
                super.onStop(owner)
                Log.d(TAG, "onStop")

                GlideApp.with(this@MainActivity).pauseAllRequests()
            }
        })

 

 

    private fun doPreload(
        myData: MyData,
        size: Int,
        cache: android.util.LruCache<String, WeakReference<Bitmap>>?
    ) {
        GlideApp.with(this)
            .asBitmap()
            .load(myData.path).addListener(object : RequestListener<Bitmap> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Bitmap>?,
                    isFirstResource: Boolean
                ): Boolean {

                    return false
                }

                override fun onResourceReady(
                    resource: Bitmap?,
                    model: Any?,
                    target: Target<Bitmap>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    if (resource != null) {
                        cache?.put(myData.path, WeakReference(resource))
                    }

                    Log.d(
                        "Glide-LruCache",
                        "path=${myData.path} byteCount=${
                            cache?.get(myData.path)?.get()?.byteCount
                        }$ ${cache?.size()}"
                    )

                    return false
                }

            })
            .centerCrop()
            .override(size)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .preload(size, size)
    }

 

 

    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()) {
            //图片路径 uri
            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) {

    }

 

 

f63c03cc27154340ab79b8af10d90764.png

 

 

 

viewLifecycleOwner.lifecycleScope life cycle, kotlin_zhangphil's blog-CSDN blogGoogle I/O 2017 Android official new architecture: LifecycleGoogle officially introduced some new Android architecture content in the 2017 I/O conference, one of which is Android Lifecycle . An important purpose of Lifecycle implementation is to realize the further decoupling of Android's logic control related to Activity and Fragment lifecycle. Google I/O Android official new architecture: Lifecycle_google i/oandroid_zhangphil's blog - CSDN blog. https://blog.csdn.net/zhangphil/article/details/130758486

Android Glide preloads preload, kotlin_zhangphil's blog-CSDN blog [Code] Android Paging 3, kotlin (1) In actual development, although Glide solves the problem of quickly loading pictures, there is still a problem that remains unresolved: such as the user's avatar , often the user's avatar is an ordinary rectangular picture read from the server, but the current design generally requires the user's avatar to be displayed as a circular avatar on the APP side, so although Glide can load it at this time, the loaded one is a rectangle. If you want Glide_android frosted glass rounded corners. "Android Image Loading and Caching Open Source Framework: Android Glide" Android Glide is an open source third-party framework for image loading and caching processing. _glide preload https://blog.csdn.net/zhangphil/article/details/131635804

Android Glide customizes the AppGlideModule, allowing Glide to load based on the customized GlideModule after the app starts, kotlin_zhangphil's blog-CSDN blog In actual development, although Glide solves the problem of quickly loading pictures, there is still a problem that remains unresolved: such as the user's Avatar, often the user's avatar is an ordinary rectangular picture read from the server, but the current design generally requires the user's avatar to be displayed as a circular avatar on the APP side, so although Glide can load it at this time, the loaded one is a rectangle , if you want Glide_android frosted glass rounded corners. "Android Image Loading and Caching Open Source Framework: Android Glide" Android Glide is an open source third-party framework for image loading and caching processing. https://blog.csdn.net/zhangphil/article/details/131592226

Android Glide preload CustomTarget bitmap into LruBitmapPool, kotlin_zhangphil's blog-CSDN blog [code] Android Paging 3, kotlin (1) In actual development, although Glide solves the problem of fast loading pictures, there is still a problem that remains unresolved: for example, users Usually, the user's avatar is an ordinary rectangular picture read from the server, but the current design generally requires the user's avatar to be displayed as a circular avatar on the APP side, so although Glide can load it at this time, what is loaded is a Rectangle, if you want Glide_android frosted glass with rounded corners. "Android Image Loading and Caching Open Source Framework: Android Glide" Android Glide is an open source third-party framework for image loading and caching processing. https://blog.csdn.net/zhangphil/article/details/131667687 kotlin forEachIndexed arrayListOf<String>_zhangphil's blog-CSDN blogPython for loop zip_python zip function for for loop_zhangphil's blog-CSDN blog. https://blog.csdn.net/zhangphil/article/details/131003571

 

Guess you like

Origin blog.csdn.net/zhangphil/article/details/132152925