ImageView 什么都不显示

ImageView shows nothing

我有一个 Bitmap (1538 x 2000) 并将其设置为 ImageViewsrc 但是当我 运行 应用程序没有显示时(但是 activity 背景颜色),我阅读了有关位图尺寸限制的内容,但如果问题是限制,为什么我可以在同一设备上通过图库查看此位图?

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/iv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:src="@drawable/pic3"/>

 </FrameLayout>

任何帮助将不胜感激

android 中的图像大小有限制。通常为 2048(最大尺寸),但您可以通过以下方法获取设备的具体值:Canvas.getMaximumBitmapWidth(), Canvas.getMaximumBitmapHeight()

您的图像大小似乎还可以,但可能是在错误的可绘制文件夹中(因此 android 以更高的分辨率读取它)。你可以把图片放到drawable-nodpi (more info: http://developer.android.com/guide/practices/screens_support.html) 文件夹,确保不会up/downscaled,但我个人认为使用较低的分辨率会更好,也许在多个文件夹中(或矢量资产,如果图像在 SVG 中可用)

你可以试试这个。

将图片放入assets文件夹

src->main->assets

如果文件夹不存在,请创建它。

将图像放在那里后,在 java 中使用此代码(以编程方式设置图像)。

Drawable d = null;
    try {
        d = Drawable.createFromStream(getAssets().open("YOUR_IMAGE_NAME"), null);
    } catch (IOException e) {
        e.printStackTrace();
    }
    imageView.setImageDrawable(d);

确保在使用图像名称时也添加了扩展名。例如:background.jpg 或 backgroung.png.

这应该有效。

嗯,我认为问题是我滥用 drawable 文件夹而不考虑 android 行为,我将图像放在 drawable 文件夹中,android 认为它是适用于 mdpi 设备。

我的 logcat 之前指过这个:

OpenGLRenderer: Bitmap too large to be uploaded into a texture (2307x3000, max=2048x2048)

如您所见,原始尺寸为 (1538x2000),logcat 报告为 (2307x3000),这意味着 android 缩放 (1.5x) 我的图像以适合我的 hdpi 设备当我使用@Prakhar 资产加载它时- scarlett speedstr 说,它按照我的预期工作,没有 android 干扰。

我也尝试了更多,将图像放在 drawable-hdpi 中,它没有任何比例也能正常工作。

感谢您提供有用的回答和评论。