将ImageView添加到约束布局会导致崩溃

时间:2017-11-15 15:18:10

标签: java android android-layout

我正在尝试为我的应用创建一个启动画面,当我只在splashScreen的布局文件中显示一些文本(使用约束布局)时,它工作得很好。但是当我添加一个imageView时,应用程序崩溃了,我不明白为什么。图片的文件大小只有299kb,所以它不是内存问题或者其他什么问题?关于为什么会发生这种情况以及如何解决问题的任何想法?这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical"
    tools:context="com.spdesigns.funfacts.SplashScreen">

<ImageView
    android:id="@+id/logoImageView"
    android:layout_width="377dp"
    android:layout_height="293dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/logo" />

</android.support.constraint.ConstraintLayout>

SplashScreen.java

package com.spdesigns.funfacts;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SplashScreen extends AppCompatActivity {

private static final int Splash_Timer = 4000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent mainActivity = new Intent(SplashScreen.this,FunFactsActivity.class);
            startActivity(mainActivity);
            finish();
        }
    },Splash_Timer);
}
}

Logcat错误:

11-15 18:29:19.218 3486-3486/com.spdesigns.funfacts E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.spdesigns.funfacts, PID: 3486
                                                                  java.lang.OutOfMemoryError: Failed to allocate a 655089132 byte allocation with 3077156 free bytes and 379MB until OOM
                                                                      at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                      at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
                                                                      at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
                                                                      at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1155)
                                                                      at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720)
                                                                      at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
                                                                      at android.content.res.Resources.loadDrawable(Resources.java:854)
                                                                      at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
                                                                      at android.widget.ImageView.<init>(ImageView.java:157)
                                                                      at android.widget.ImageView.<init>(ImageView.java:145)
                                                                      at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
                                                                      at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
                                                                      at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
                                                                      at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
                                                                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
                                                                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                      at com.spdesigns.funfacts.SplashScreen.onCreate(SplashScreen.java:15)
                                                                      at android.app.Activity.performCreate(Activity.java:6662)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

1 个答案:

答案 0 :(得分:1)

问题是ImageView使用GL纹理,在大多数设备上限制为2048x2048。最大纹理尺寸因设备而异,我看到的尺寸小到约1500x1500,大到4096x4096。

为此,您必须为每个维度创建一个图像,以确保它可以在所有设备中使用。查看此主题以获取更多信息。

GL Maximum Textures Size Policy