以下是原始图片:
此为使用ImageView渲染后的图片:
然而,在轮播图中,有时向后滑动图片会导致图片正确渲染,这更加奇怪...
在LG G3 (Android 5.1)和Genymotion (Android 4.4.4)上都观察到了这种行为。我正在使用Glide库加载图片,并使用ARGB_8888
解码格式。
new GlideBuilder(this).setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
以下是原始图片:
此为使用ImageView渲染后的图片:
然而,在轮播图中,有时向后滑动图片会导致图片正确渲染,这更加奇怪...
在LG G3 (Android 5.1)和Genymotion (Android 4.4.4)上都观察到了这种行为。我正在使用Glide库加载图片,并使用ARGB_8888
解码格式。
new GlideBuilder(this).setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
RGB_565
影响更明显,而不是ARGB_8888
,因此您可能需要将DecodeFormat切换为ARGB_8888
(清除应用程序数据以检查问题是否已解决)。但即使使用ARGB_8888
,问题也可能出现,因此请使用以下解决方案之一:
Use DiskCacheStrategy.NONE
(for local images) or DiskCacheStrategy.SOURCE
(for remote images) to prevent Glide from re-compressing the images:
Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView);
Use asBitmap() and a custom BitmapEncoder to always compress affected images as PNGs:
Glide.with(this)
.fromResource()
.asBitmap()
.encoder(new BitmapEncoder(Bitmap.CompressFormat.PNG,100))
.load(R.drawable.testimg)
.into(imageView);
如果有人尝试了上面列出的所有方法,但都没有起作用(就像我的情况一样),还有另一种解决方法。由于绿色问题发生在转换期间,我们可以避免转换。
Glide.with(context)
.load(url)
.dontTransform()
.into(holder.productImage);
This issue may happen on few devices not all like one plus 3 or 3T and some LG devices when fetching imageUrl from server to your android project.
public static void loadImageWith(Context context, String imageUrl, ImageView imageView) {
Glide.with(context)
.load(imageUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.dontTransform()
.placeholder(R.drawable.empty_photo)
.into(imageView);
}
centerCrop() may create issue, so avoid to use centerCrop().