建设网站的网站是什么,做网站 江门,网站推广基本预算,网店运营推广具体内容Android自定义AppGlideModule,DataFetcher ,ModelLoaderFactory,ModelLoader,Kotlin(1)
假设实现一个简单的功能#xff0c;对传入要加载的path路径增加一定的筛选、容错或“重定向”#xff0c;需要自定义一个模型#xff0c;基于这个模型#xff0c;让Glide自动匹配模型…Android自定义AppGlideModule,DataFetcher ,ModelLoaderFactory,ModelLoader,Kotlin(1)
假设实现一个简单的功能对传入要加载的path路径增加一定的筛选、容错或“重定向”需要自定义一个模型基于这个模型让Glide自动匹配模型展开加载。 plugins {id org.jetbrains.kotlin.kapt
} implementation com.github.bumptech.glide:glide:4.16.0kapt com.github.bumptech.glide:compiler:4.16.0 import android.content.Context
import android.util.Log
import com.bumptech.glide.Glide
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule
import java.io.InputStreamGlideModule
class MyGlideModule : AppGlideModule() {override fun applyOptions(context: Context, builder: GlideBuilder) {super.applyOptions(context, builder)builder.setLogLevel(Log.DEBUG)}override fun registerComponents(context: Context, glide: Glide, registry: Registry) {super.registerComponents(context, glide, registry)registry.append(VideoCover::class.java,InputStream::class.java,VideoCoverLoaderFactory())}
} class VideoCover {var path: String? nullconstructor(path: String) {this.path path}
} import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Bitmap.CompressFormat
import android.graphics.BitmapFactory
import android.util.Log
import com.bumptech.glide.Priority
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.data.DataFetcher
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStreamclass VideoCoverFetcher : DataFetcherInputStream {val TAG Glide/VideoCoverFetcherprivate var model: VideoCover? nullprivate val resId android.R.drawable.stat_notify_errorconstructor(model: VideoCover) {this.model model}override fun loadData(priority: Priority, callback: DataFetcher.DataCallbackin InputStream) {val bmp BitmapFactory.decodeResource(Resources.getSystem(), resId)Log.d(TAG, loadData ${bmp.byteCount})callback.onDataReady(ByteArrayInputStream(bitmapToByteArray(bmp)))}override fun cleanup() {Log.d(TAG, cleanup)}override fun cancel() {Log.d(TAG, cancel)}override fun getDataClass(): ClassInputStream {return InputStream::class.java}override fun getDataSource(): DataSource {return DataSource.LOCAL}private fun bitmapToByteArray(bitmap: Bitmap): ByteArray {val bos ByteArrayOutputStream()bitmap.compress(CompressFormat.PNG, 0, bos)return bos.toByteArray()}
} import android.util.Log
import com.bumptech.glide.load.model.ModelLoader
import com.bumptech.glide.load.model.ModelLoaderFactory
import com.bumptech.glide.load.model.MultiModelLoaderFactory
import java.io.InputStreamclass VideoCoverLoaderFactory : ModelLoaderFactoryVideoCover, InputStream {val TAG Glide/VideoCoverLoaderFactoryoverride fun build(multiFactory: MultiModelLoaderFactory): ModelLoaderVideoCover, InputStream {return VideoCoverModuleLoader()}override fun teardown() {Log.d(TAG, teardown)}
} import android.util.Log
import com.bumptech.glide.load.Options
import com.bumptech.glide.load.model.ModelLoader
import com.bumptech.glide.load.model.ModelLoader.LoadData
import com.bumptech.glide.signature.ObjectKey
import java.io.InputStreamclass VideoCoverModuleLoader : ModelLoaderVideoCover, InputStream {val TAG Glide/VideoCoverModuleLoaderoverride fun buildLoadData(model: VideoCover,width: Int,height: Int,options: Options): ModelLoader.LoadDataInputStream? {Log.d(TAG, buildLoadData)return LoadData(VideoCoverSignature(model.path!!), //简单时候可以考虑ObjectKey(model.path!!)VideoCoverFetcher(model))}override fun handles(model: VideoCover): Boolean {return true}
} import com.bumptech.glide.load.Key
import java.security.MessageDigestclass VideoCoverSignature() : Key {private var path: String? nullconstructor(path: String) : this() {this.path path}override fun updateDiskCacheKey(messageDigest: MessageDigest) {val ba: ByteArray path?.toByteArray()!!messageDigest.update(ba, 0, ba.size)}
} import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.signature.ObjectKeyclass MainActivity : AppCompatActivity() {val TAG Glide/MainActivityprivate var image: ImageView? nulloverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val path xxximage findViewByIdImageView(R.id.image)GlideApp.with(this).load(VideoCover(path)).diskCacheStrategy(DiskCacheStrategy.RESOURCE)//.signature(ObjectKey(path)).addListener(object : RequestListenerDrawable {override fun onLoadFailed(e: GlideException?,model: Any?,target: TargetDrawable,isFirstResource: Boolean): Boolean {Log.d(TAG, onLoadFailed)return false}override fun onResourceReady(resource: Drawable,model: Any,target: TargetDrawable?,dataSource: DataSource,isFirstResource: Boolean): Boolean {Log.d(TAG, onResourceReady)return false}}).override(500).into(image!!)}
} Android Glide自定义AppGlideModule让Glide在app启动后基于定制化GlideModule加载kotlin_glideapp-CSDN博客在实际的开发中虽然Glide解决了快速加载图片的问题但还有一个问题悬而未决比如用户的头像往往用户的头像是从服务器端读出的一个普通矩形图片但是现在的设计一般要求在APP端的用户头像显示成圆形头像那么此时虽然Glide可以加载但加载出来的是一个矩形如果要Glide_android 毛玻璃圆角。《Android图片加载与缓存开源框架Android Glide》Android Glide是一个开源的图片加载和缓存处理的第三方框架。_glideapphttps://blog.csdn.net/zhangphil/article/details/131592226
Android Glide preload CustomTarget bitmap into LruBitmapPoolkotlin-CSDN博客【代码】Android Paging 3,kotlin1在实际的开发中虽然Glide解决了快速加载图片的问题但还有一个问题悬而未决比如用户的头像往往用户的头像是从服务器端读出的一个普通矩形图片但是现在的设计一般要求在APP端的用户头像显示成圆形头像那么此时虽然Glide可以加载但加载出来的是一个矩形如果要Glide_android 毛玻璃圆角。《Android图片加载与缓存开源框架Android Glide》Android Glide是一个开源的图片加载和缓存处理的第三方框架。https://blog.csdn.net/zhangphil/article/details/131667687