方法一:不推薦,會出現閃爍
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) }