android - ImageView 显示灰色方 block 而不是图像

标签 android imageview android-imageview android-glide

enter image description here

我尝试了几种不同的方法,但 ImageView 仍然只显示一个灰色的方 block 。我正在 API 17 上进行测试。

以下是我尝试过但未成功的选项:

  • Configured the ImageView in XML to:

  • fixed width and height; width and height to wrap_content; width to match_parent and height to wrap_content

  • android: scaleType to "fitXY" and "centerCrop"
  • android: adjustViewBounds to true | false;

尝试过的图像加载方法:

binding.captcha.setImageBitmap (BitmapFactory.decodeFile (imgFile2.getAbsolutePath ()));

也尝试过这个答案https://stackoverflow.com/a/35830800/1764042

尝试过 Glide :

    Glide.with(this)
    .load (Uri.fromFile (imgFile2))
    .skipMemoryCache (true)
    .diskCacheStrategy (DiskCacheStrategy.NONE)
    .override (150, 150)
    .centerCrop ()
    .into (binding.captcha);

下面的选项是唯一显示图像(在背景中)的选项,但是我想知道可能会发生什么情况来阻止我使用默认方法显示图像...

    Drawable drawableImage = new BitmapDrawable(BitmapFactory.decodeFile(imgFile2.getAbsolutePath()));
    binding.captcha.setBackgroundDrawable(drawableImage);
  • 注释:
  • imgFile2不为空,它是从SD卡加载的。
  • 如果我将其用作背景,则会显示 imgFile2,因此问题不在于该文件。
  • 这个问题不是简单地用一个库就能解决的,我已经在尝试 Glide...但是如果你知道我需要做什么才能让它与 glide 一起工作...
  • 如果重要的话,imageView 位于 fragment 内。

当前 xml:

        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_centerHorizontal="true"
            android:id="@+id/captcha"/>

最佳答案

对于那些仍然面临这个问题的人来说,对我来说,这是由于我所使用的 ImageViewandroid:tint 属性引起的使用 Glide 设置图像。

我正在显示一个虚拟占位符图像,其中 android:tint 作为灰色阴影,而实际图像从 url 加载。而这种色调就是导致问题的原因。

因此,请尝试完全删除 android:tint 属性,或者在使用 imageView.setImageTintList(null) 加载图像后以编程方式将色调设置为 null

希望对你有帮助

关于android - ImageView 显示灰色方 block 而不是图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47580907/

相关文章:

android - 无法在我的监听器和广播接收器中涉及接口(interface)方法空对象引用

android顺序补间动画

swift - 正确地为纵横比适合的图像加上字幕

android - 使用 Thumbnailutils 显示缩略图时出现问题

java - 我怎样才能指定这个 ImageView 必须显示得更小?

Android - 通过 wifi 使用 adb 连接到设备

android - ViewPagerIndicator 在 setCurrentItem() 之后没有更新

android - 我如何从我的可绘制对象中找到在 imageview 中设置的图像?

列表项中的 Android ListView ImageView 在滚动时隐藏

android - 如何在多个布局中重用 ImageView