只要在recyclelerview中为base64图像调用notifydatasetchanged(),图像就会闪烁

问题描述 投票:0回答:1

我在我的项目中使用滑动来在回收器视图中显示图像。图像将从服务器下载,直到我将显示非常低分辨率的模糊图像。模糊图像是基础64编码图像,其将转换为byteArray以在滑行中显示。

我的问题是每当notifydatasetchanged()函数被称为基础64解码图像闪烁。如何避免这种奇怪的行为?

我在同一个回收站视图中将本地存储中的图像作为File加载,但是当notifydatasetchanged()调用时没有闪烁的问题。仅对于模糊图像(基本64解码位图)发生闪烁问题

我正在使用滑翔版:4.8.0

//converting string to byteArray
byte[] blurImg = getBlurImageBitmap(fileDataTable.get("blurimg") as String)

//function which converts the image string to byteArray
fun getBlurImageBitmap(blurImageString : String) : ByteArray {
    val decodedBytes = Base64.decode(blurImageString, android.util.Base64.DEFAULT)
    return decodedBytes
}

//loading the byteArray into glide
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    Glide.with(imageMessage.context)
         .load(chatMessage.fileData.blurImg)
         .transition(DrawableTransitionOptions.withCrossFade(1))
         .into(imageMessage)
}

我想避免滑翔时基础64图像的闪烁。

android android-recyclerview base64 android-glide
1个回答
0
投票

找到了闪烁图像的原因。

发现仅基础64(模糊图像)编码图像导致闪烁问题并且来自本地存储的图像不会闪烁。每次刷新数据时,Glide都会将base 64编码的字符串转换为drawable,因此会发生闪烁。虽然本地存储的位图是第一次处理并存储在LRU Cache中,但是当再次刷新数据时它将不会被加载。

在查看滑动内部回调时,发现只要将base 64字符串转换为drawable,它就会将null作为资源发送。

解决方案是提供一个占位符drawable与base 64解码drawable相同,以便滑动,这样只要它将null作为资源发送,它就会显示占位符drawable。

Drawable image = new BitmapDrawable(((ImageViewHolder) holder).imageView.getContext().getResources(), BitmapFactory.decodeByteArray(imgList.get(position), 0, imgList.get(position).length));

//Passing the converted drawable as placeholder
requestOptions.placeholder(image);


Glide.with(imageViewHolder.imageView.getContext())
               .load(imgList.get(position))  -> Passing the same base 64 string which was converted to drawable for placeholder
               .apply(requestOptions)
               .into(imageViewHolder.imageView);


So the image actually flickers but we have passed the same image as placeholder, so the flicker will not be visible to use
© www.soinside.com 2019 - 2024. All rights reserved.