【问题标题】:Image View doesn't show clear image defined in xml图像视图不显示 xml 中定义的清晰图像
【发布时间】:2016-06-03 19:37:32
【问题描述】:

在我的代码中,我想为我的 imageview 设置一个默认图像。我所有的图像都是 174px X 174px。所以,我希望默认图像大小相同。这是我用于图像视图的 xml

//....code

<ImageView
    android:id="@+id/ivCover"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:contentDescription="Default Image"
    android:src="@drawable/default_cover" />


//....some other code

现在,我的 default_cover.jpg 也是 174px X 174px。但它显示了一个较小的图像。我都试过了

android:scaleType="centerInside"
android:scaleType="center"
android:scaleType="matrix"

即使它们中的大多数对我来说毫无意义,但我仍然尝试了所有它们并且它们都不起作用。然后我通过指定指定图像的高度和宽度

android:layout_width="174px"
android:layout_height="174px"

(我知道使用 dp 比 px 更好,但因为这个图像大小是固定的,我只是在测试它)。所以它显示了

[默认图片]

当使用

从外部 url 加载时
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url_source_for_image).getContent());
ivCover.setImageBitmap(bitmap);

想要的图片

[]

这两张图片在这里看起来可能差别不大,但在我的安卓设备上差别非常明显。请不要说我没有尝试其他解决方案.. 这是so本身的链接列表..

ImageView fit without stretching the image

Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?

ImageView one dimension to fit free space and second evaluate to keep aspect ration

Resizing ImageView to fit to aspect ratio

Maximum width and height for ImageView in Android

Image in ImageView is stretched - Android

Android: How to prevent image from being scaled in ImageView or ImageButton?

【问题讨论】:

  • 您的位图在哪个可绘制文件夹中?如果你在 drawable-ldpi 中放置一个 50x50 的位图,在 drawable-hdpi 中放置一个 174x174 的位图,Android 将根据设备的分辨率而不是你指定的尺寸来决定显示哪一个。如果你想在所有设备上显示一个特定的位图,不管分辨率如何,把它放在 res\drawable-nodpi 中。
  • 图像在 res\drawable-xxhdpi 中,因为我认为 android 会巧妙地缩小图像大小以显示在低分辨率设备中。
  • 对,所以它可能会将图像缩小到较低的分辨率,然后再将其放大到适合您指定的尺寸。如果你把它放在 drawable-nodpi 中,它会起作用吗?
  • 如果我将高度和宽度设置为 wrap_content,那么它只会显示较小的图像,而当我指定这些属性 = 174px 时,它会再次显示那些扭曲的像素。
  • 哦,是的!有效。实际上我之前复制了那个图像。这次..我移动了它,现在可以正常工作了。我想知道我以前从未遇到过这种无 dpi 的东西。非常感谢。

标签: android android-imageview android-image


【解决方案1】:

听起来答案是使用drawable-nodpi。为了将来参考,如果您在 ldpi/hdpi/etc 和 no-dpi 中都有相同的命名图像,我认为 no-dpi 将被忽略。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-26
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 2011-08-16
    相关资源
    最近更新 更多