我尝试了几种不同的方法,但 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"/>
最佳答案
对于那些仍然面临这个问题的人来说,对我来说,这是由于我所使用的 ImageView
的 android:tint
属性引起的使用 Glide 设置图像。
我正在显示一个虚拟占位符图像,其中 android:tint
作为灰色阴影,而实际图像从 url 加载。而这种色调就是导致问题的原因。
因此,请尝试完全删除 android:tint
属性,或者在使用 imageView.setImageTintList(null)
加载图像后以编程方式将色调设置为 null
希望对你有帮助
关于android - ImageView 显示灰色方 block 而不是图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47580907/