PNG 未显示在 ImageView 中

PNG not showing in ImageView

我有一个大的 .png 文件,我想在启动画面中显示它,但图像没有显示。 xml:

<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.infaplic.lpi.activities.SplashActivity">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView2"/>

和Activity的代码:

public class SplashActivity extends Activity {

ImageView imagen;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    imagen=(ImageView)findViewById(R.id.imageView2);
    imagen.setImageDrawable(getResources().getDrawable(R.drawable.pantalla_arranque));
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            Intent mainIntent = new Intent(SplashActivity.this, NextActivity.class);
            SplashActivity.this.startActivity(mainIntent);
            SplashActivity.this.finish();
        }
    }, 1000);
}
}

这样一来,图片就不显示了。我试过 android:src: 在 xml 中,但它不起作用。

图片很大。在将它放入 ImageView 之前我需要调整它的大小吗?如果不是,为什么图片不显示?

谢谢。

编辑:PNG 为 1080x1920

在此下方添加。请阅读用于将图像边界缩放到 ImageView

中此 view.ImageView.ScaleType 边界的选项

使用这个

imagen.setImageResource(R.drawable.pantalla_arranque);

然后

<ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView2"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"/>  

android:adjustViewBounds :如果您希望 ImageView 调整其边界以保持其可绘制对象的纵横比,请将此设置为 true。

必须是 boolean 值,"true" 或 "false"。
请检查演示 ImageView scaleType Samples 。希望对你有帮助

将您的 ImageView 更改为:

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView2"
android:scaleType="fitXY"
android:adjustViewBounds="false"/>

如果它不起作用,请尝试将其添加到您的 manifest(在您的 Activity) android:hardwareAccelerated="false"

编辑

我一直在寻找答案,但我认为最好自己尝试一下......我创建了一个示例项目,我在其中使用了 ImageView 1080x1920 并且我一直在测试,因为我'我发现我把它显示了 3000 毫秒...如果你不重新调整它,1000 毫秒就不会显示。

顺便说一下,getDrawable() 已弃用,我建议您使用:

imagen.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.imagenprueva));

XML

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
    >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView2"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:background="@drawable/imagen"
        />
    </RelativeLayout>

这是完整的源代码希望对您有所帮助Source code

既然你说你的图片很大,我猜它太大了。我的一个应用程序中有一个错误,我在那里生成了一个大于 2048 像素的图像。有些设备不支持这么大的图片我猜你运行遇到了类似的问题。

如果那是一些背景图片,减小图片尺寸,背景图片不是那么重要,屏幕上的每个像素都必须拥有一个图像像素。

在我的例子中,我使用的是文件名为 "go-back-filename.png" 的图像。 xml 文件就像

 <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/go-back-icon"/>

在我将文件名更改为 "go_back_icon" 后,它工作正常。 希望这对某人有所帮助。

在您的 .xml 文件中 添加
xmlns:app="http://schemas.android.com/apk/res-auto"

然后在ImageView中添加这行代码:

app:srcCompat="@drawable/books"

不是

tools:srcCompat="@drawable/books"