Android: Canvas: 试图绘制太大

Android: Canvas: trying to draw too large

我正在 android 上使用一个应用程序,并且,每当我调试我制作的闪屏时,都会出现此错误:

java.lang.RuntimeException: Canvas: trying to draw too large(165019200bytes) bitmap.

但是,我找不到应该在哪里调整它的大小,因为我的布局看起来很正常。如果你能帮我解决这个问题就太好了。我是 android 上的新手。我附上 xml 代码、class 和完整的错误消息。提前致谢。

Class SplashScreen.java

    package com.example.taxscore;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
//import android.view.View;

public class SplashScreen extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        //metodo para la funcion de splashscreen, con duracion de 5s
        new Handler().postDelayed(new Runnable (){
            @Override
            public void run(){

                Intent intent = new Intent (SplashScreen.this, MainActivity.class);
                startActivity(intent);
                finish();
            }

        },5000);

    }
}

布局SplashScreen.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SplashScreen"
    android:background="#ffffff"
    >

    <ImageView
        android:layout_width="308dp"
        android:layout_height="190dp"
        android:background="#00FFFFFF"
        android:scaleType="centerInside"
        android:src="@drawable/tslogo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

完整错误信息:

11/28 23:17:43: Launching 'app' on Pixel.
$ adb shell am start -n "com.example.taxscore/com.example.taxscore.SplashScreen" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 5080 on device 'Pixel [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/art: Not late-enabling -Xcheck:jni (already on)
W/art: Unexpected CPU variant for X86 using defaults: x86
W/System: ClassLoader referenced unknown path: /data/app/com.example.taxscore-1/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
        at void androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener(android.view.View, androidx.core.view.OnApplyWindowInsetsListener) (ViewCompat.java:2421)
        at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:779)
        at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:659)
        at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:552)
        at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:161)
        at void com.example.taxscore.SplashScreen.onCreate(android.os.Bundle) (SplashScreen.java:14)
        at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6679)
        at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1118)
        at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2618)
        at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2726)
        at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
        at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1477)
        at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
        at void android.os.Looper.loop() (Looper.java:154)
I/art:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6119)
        at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
        at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:886)
        at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:776)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.example.taxscore-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.taxscore-1/lib/x86, /system/lib, /vendor/lib]]
        at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
        at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
        at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
        at void androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener(android.view.View, androidx.core.view.OnApplyWindowInsetsListener) (ViewCompat.java:2421)
        at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:779)
        at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:659)
        at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:552)
        at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:161)
        at void com.example.taxscore.SplashScreen.onCreate(android.os.Bundle) (SplashScreen.java:14)
        at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6679)
        at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1118)
        at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2618)
        at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2726)
I/art:     at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
        at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1477)
        at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
        at void android.os.Looper.loop() (Looper.java:154)
        at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6119)
        at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
        at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:886)
        at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:776)
I/art: Background sticky concurrent mark sweep GC freed 10367(580KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 17MB/17MB, paused 2.229ms total 326.727ms
I/art: Background partial concurrent mark sweep GC freed 218(16KB) AllocSpace objects, 0(0B) LOS objects, 18% free, 17MB/21MB, paused 87.710ms total 120.663ms
D/: HostConnection::get() New Host Connection established 0xa9fda780, tid 5080
D/: HostConnection::get() New Host Connection established 0xb282de80, tid 5107
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xb64850c0: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xb64850c0: ver 2 0 (tinfo 0xb64837b0)
D/EGL_emulation: eglMakeCurrent: 0xb64850c0: ver 2 0 (tinfo 0xb64837b0)
I/art: Background sticky concurrent mark sweep GC freed 698(150KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 174MB/174MB, paused 112.642ms total 207.242ms
I/ViewConfigCompat: Could not find method getScaledScrollFactor() on ViewConfiguration
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.taxscore, PID: 5080
    java.lang.RuntimeException: Canvas: trying to draw too large(165019200bytes) bitmap.
        at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260)
        at android.graphics.Canvas.drawBitmap(Canvas.java:1415)
        at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:528)
        at android.widget.ImageView.onDraw(ImageView.java:1316)
        at android.view.View.draw(View.java:17185)
        at android.view.View.updateDisplayListIfDirty(View.java:16167)
        at android.view.View.draw(View.java:16951)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
        at androidx.constraintlayout.widget.ConstraintLayout.dispatchDraw(ConstraintLayout.java:2023)
        at android.view.View.updateDisplayListIfDirty(View.java:16162)
        at android.view.View.draw(View.java:16951)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
        at android.view.View.updateDisplayListIfDirty(View.java:16162)
        at android.view.View.draw(View.java:16951)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
        at android.view.View.updateDisplayListIfDirty(View.java:16162)
        at android.view.View.draw(View.java:16951)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
        at android.view.View.updateDisplayListIfDirty(View.java:16162)
        at android.view.View.draw(View.java:16951)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
        at android.view.View.updateDisplayListIfDirty(View.java:16162)
        at android.view.View.draw(View.java:16951)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
        at android.view.View.draw(View.java:17188)
        at com.android.internal.policy.DecorView.draw(DecorView.java:753)
        at android.view.View.updateDisplayListIfDirty(View.java:16167)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:648)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:654)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:762)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2800)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2608)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2215)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
        at android.view.Choreographer.doCallbacks(Choreographer.java:686)
        at android.view.Choreographer.doFrame(Choreographer.java:621)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Process 5080 terminated.

您应该阅读完整的错误:试图绘制太大的位图。您要绘制的图像似乎太大了。请注意,即使图像很小如 png 或 jpeg 文件,也需要将其转换(解压缩)为可能变得非常大的位图。 例如。一个 1000x1000 像素的全白图像将变成一个很小的 ​​png 文件,但作为位图它将占用 400 万字节(1000 * 1000 * 4),因为每个像素 4 个字节(RGB 和 Alpha)。