gpt4 book ai didi

java - 布局背景图像的 OutOfMemory 异常

转载 作者:行者123 更新时间:2023-12-01 11:06:08 24 4
gpt4 key购买 nike

我是 Android 新手,关注 this在 Android 中创建 MEME 应用程序的教程一切都工作正常,直到我将图像添加到 fragment 的相对布局背景中。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/dp"
android:id="@+id/imageOut">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/topText"
android:id="@+id/topMemeText"
android:layout_marginTop="32dp"
android:textColor="#FFF"
android:gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/bottomText"
android:id="@+id/bottomMemeText"
android:layout_marginBottom="69dp"
android:textColor="#FFF"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

错误是

 Caused by: java.lang.OutOfMemoryError: Failed to allocate a 86687164 byte allocation with 1048576 free bytes and 63MB until OOM

我知道这个问题是由于图像大小造成的,我必须对其进行解码,因为我在网上和这里搜索了很多情况http://developer.android.com/training/displaying-bitmaps/load-bitmap.html但是我遇到的每种情况都使用 ImageView,但我有布局的背景图像而不是 ImageView,所以我如何从布局中获取 ImageView 对象以便我可以使用。

mImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

谢谢

最佳答案

我的猜测是图像大小的调整导致了您的问题。

尝试将图像添加到drawable-xxhdpi,看看是否有帮助。

如果没有尝试drawable-nodpi。

关于java - 布局背景图像的 OutOfMemory 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32931934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com