0
点赞
收藏
分享

微信扫一扫

android开发使用Glide加载Bitmap的方法

九月的栩 2022-05-16 阅读 72
方法一:不推荐,会出现闪烁
fun loadBitmapImage(target: ImageView, bitmap: Bitmap) {
val baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos)
val bytes: ByteArray = baos.toByteArray()
val requestOptions = RequestOptions().centerCrop()
Glide.with(target.context)
.setDefaultRequestOptions(requestOptions)
.load(bytes)
.into(target)
baos.closeQuietly()
}
方法二:推荐
fun loadBitmapImage(target: ImageView, bitmap: Bitmap) {
val drawable = BitmapDrawable(target.resources, bitmap)
val requestOptions = RequestOptions().centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.error(drawable)  //放在出错位置
.placeholder(drawable)  //放在占位符位置
Glide.with(target.context)
.setDefaultRequestOptions(requestOptions)
.load("https://${System.currentTimeMillis()}")  //随便给个不可用的url
.into(target)
}





举报

相关推荐

0 条评论