Android Glide preload CustomTarget bitmap into LruBitmapPool,kotlin

Android Glide preload CustomTarget bitmap into LruBitmapPool,kotlin

    implementation 'com.github.bumptech.glide:glide:4.15.1'
    kapt 'com.github.bumptech.glide:compiler:4.14.2'
    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" />
    <uses-permission android:name="android.permission.INTERNET"/>
    private fun loadBitmaps() {
        CoroutineScope(Dispatchers.IO).launch {
            val items = readAllImage(applicationContext)
            withContext(Dispatchers.Main) {
                items.forEachIndexed { index, myData ->
                    loadBitmapIntoCustomTarget(index, File(myData.path))
                }
            }
        }
    }

    private val target = object : CustomTarget<Bitmap>() {
        override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
            MyModule.debug("CustomTarget-onResourceReady")
        }

        override fun onLoadCleared(placeholder: Drawable?) {

        }
    }

    private fun loadBitmapIntoCustomTarget(index: Int, file: File) {
        GlideApp.with(this)
            .asBitmap()
            .load(file.path)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .into(target)

        MyModule.debug("${index} loadBitmapIntoCustomTarget")
    }





    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)
import android.content.Context
import android.util.Log
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool
import com.bumptech.glide.load.engine.cache.LruResourceCache
import com.bumptech.glide.module.AppGlideModule

@GlideModule
class MyModule : AppGlideModule() {
    companion object {
        const val TAG = "GlideModule-Debug"
        val mLruResourceCache = LruResourceCache(1024 * 1024 * 9999)
        val mLruBitmapPool = LruBitmapPool(1024 * 1024 * 9999)

        fun debug(msg: String) {
            Log.d(
                TAG,
                "${mLruResourceCache.currentSize}-${mLruBitmapPool.currentSize} $msg"
            )
        }
    }

    override fun applyOptions(context: Context, builder: GlideBuilder) {
        super.applyOptions(context, builder)

        /**
        根据屏幕大小自动获取合适的缓存大小
        MemorySizeCalculator calculator=new MemorySizeCalculator.Builder(context)
        //默认值是2
        .setMemoryCacheScreens(2)
        .build();
        //使用Lru缓存策略
        builder.setMemoryCache(new LruResourceCache(calculator.getMemoryCacheSize()))
         */

        builder.setMemoryCache(mLruResourceCache)
        builder.setBitmapPool(mLruBitmapPool)
        builder.setLogLevel(Log.DEBUG)
        Log.d(TAG, "自定义配置")
    }

    override fun isManifestParsingEnabled(): Boolean {
        return false
    }
}
扫描二维码关注公众号,回复: 15679474 查看本文章

Android Glide预加载preload ,kotlin_zhangphil的博客-CSDN博客【代码】Android Paging 3,kotlin(1)在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬而未决:比如用户的头像,往往用户的头像是从服务器端读出的一个普通矩形图片,但是现在的设计一般要求在APP端的用户头像显示成圆形头像,那么此时虽然Glide可以加载,但加载出来的是一个矩形,如果要Glide_android 毛玻璃圆角。《Android图片加载与缓存开源框架:Android Glide》Android Glide是一个开源的图片加载和缓存处理的第三方框架。https://blog.csdn.net/zhangphil/article/details/131635804Android Glide预加载RecyclerViewPreloader,ViewPreloadSizeProvider,kotlin_zhangphil的博客-CSDN博客【代码】Android Paging 3,kotlin(1)在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬而未决:比如用户的头像,往往用户的头像是从服务器端读出的一个普通矩形图片,但是现在的设计一般要求在APP端的用户头像显示成圆形头像,那么此时虽然Glide可以加载,但加载出来的是一个矩形,如果要Glide_android 毛玻璃圆角。《Android图片加载与缓存开源框架:Android Glide》Android Glide是一个开源的图片加载和缓存处理的第三方框架。https://blog.csdn.net/zhangphil/article/details/131597104Android 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/131592226

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/131667687
今日推荐