安卓的ImageView显示出绿色的图片

6

以下是原始图片:

enter image description here

此为使用ImageView渲染后的图片:

enter image description here

然而,在轮播图中,有时向后滑动图片会导致图片正确渲染,这更加奇怪...

在LG G3 (Android 5.1)和Genymotion (Android 4.4.4)上都观察到了这种行为。我正在使用Glide库加载图片,并使用ARGB_8888解码格式。

new GlideBuilder(this).setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);

我们来跟进GitHub的问题:https://github.com/bumptech/glide/issues/515 - Sam Judd
3个回答

8
这是一个已解决的问题305。以下是简要回顾:
此问题仅出现在JPEG格式的图像中(质量无关)。它似乎对RGB_565影响更明显,而不是ARGB_8888,因此您可能需要将DecodeFormat切换为ARGB_8888(清除应用程序数据以检查问题是否已解决)。但即使使用ARGB_8888,问题也可能出现,因此请使用以下解决方案之一:
  1. 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);
    
  2. 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);
    

谢谢,diskCacheStrategy(DiskCacheStrategy.SOURCE)在这里是关键。所有的DecodeFormat.PREFER_ARGB_8888 / GlideBuilder / GlideModule变体都无法真正强制使用24位颜色。 - Ryan

2

如果有人尝试了上面列出的所有方法,但都没有起作用(就像我的情况一样),还有另一种解决方法。由于绿色问题发生在转换期间,我们可以避免转换。

Glide.with(context)
     .load(url)
     .dontTransform()
     .into(holder.productImage);

1
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(). 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接