我正在使用这种布局,这似乎会导致内存问题。我重新调整按钮的大小,使其与Program中的屏幕完全匹配。引入屏幕的一小部分动画似乎足以引起问题。图形不是很大,所以它必须是导致问题的大量按钮?
也许我最好创建一个大图像/按钮并根据他们点击的图像中的坐标激活选项/按钮。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/everything"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal|center_vertical"
android:background="@drawable/backrepeat"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/page"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<ImageButton
android:id="@+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@drawable/clear"
android:gravity="start"
android:src="@drawable/clear" />
<TextView
android:id="@+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:gravity="end"
android:text=" "
android:textColor="#ff0000"
android:textSize="120sp" />
</LinearLayout>
<ImageView
android:id="@+id/breakerimage"
android:layout_width="fill_parent"
android:layout_height="4dp"
android:background="#fff000"
android:src="@drawable/clear" />
<LinearLayout
android:id="@+id/line1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:src="@drawable/clear" >
<ImageButton
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/aa"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bb"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/cc"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dd"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ee"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ff"
android:src="@drawable/clear" />
</LinearLayout>
<LinearLayout
android:id="@+id/line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/gg"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/hh"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ii"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/jj"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/kk"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ll"
android:src="@drawable/clear" />
</LinearLayout>
<LinearLayout
android:id="@+id/line3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/btn13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/mm"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/nn"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/oo"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pp"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/qq"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rr"
android:src="@drawable/clear" />
</LinearLayout>
<LinearLayout
android:id="@+id/line4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/btn19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ss"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tt"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/uu"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/vv"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ww"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/xx"
android:src="@drawable/clear" />
</LinearLayout>
<LinearLayout
android:id="@+id/line5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/btn25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/yy"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/zz"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/clear64"
android:src="@drawable/clear" />
<ImageButton
android:id="@+id/btn30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/up"
android:scaleType="fitXY"
android:src="@drawable/clear" />
</LinearLayout>
</LinearLayout>
logcat:https://www.dropbox.com/s/3vii4qndafcxue4/log.txt?dl=0
详细
>09-21 10:31:26.590: I/dalvikvm-heap(19975): Clamp target GC heap from 132.504MB to 128.000MB
09-21 10:31:26.590: D/dalvikvm(19975): GC_FOR_ALLOC freed 7K, 3% free 126221K/129188K, paused 26ms, total 26ms
09-21 10:31:26.630: I/dalvikvm-heap(19975): Clamp target GC heap from 138.263MB to 128.000MB
09-21 10:31:26.630: D/dalvikvm(19975): GC_FOR_ALLOC freed 1K, 3% free 128022K/130996K, paused 27ms, total 27ms
09-21 10:31:26.630: I/dalvikvm-heap(19975): Forcing collection of SoftReferences for 921616-byte allocation
09-21 10:31:26.670: I/dalvikvm-heap(19975): Clamp target GC heap from 142.254MB to 128.000MB
09-21 10:31:26.670: D/dalvikvm(19975): GC_BEFORE_OOM freed 79K, 3% free 127943K/130996K, paused 38ms, total 38ms
09-21 10:31:26.670: E/dalvikvm-heap(19975): Out of memory on a 921616-byte allocation.
09-21 10:31:26.670: I/dalvikvm(19975): "main" prio=5 tid=1 RUNNABLE
09-21 10:31:26.670: I/dalvikvm(19975): | group="main" sCount=0 dsCount=0 obj=0x418d4ea0 self=0x417c88c0
09-21 10:31:26.670: I/dalvikvm(19975): | sysTid=19975 nice=0 sched=0/0 cgrp=apps handle=1074704724
09-21 10:31:26.670: I/dalvikvm(19975): | state=R schedstat=( 0 0 0 ) utm=264 stm=94 core=1
09-21 10:31:26.670: I/dalvikvm(19975): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-21 10:31:26.680: I/dalvikvm(19975): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
09-21 10:31:26.680: I/dalvikvm(19975): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
09-21 10:31:26.680: I/dalvikvm(19975): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
09-21 10:31:26.680: I/dalvikvm(19975): at android.content.res.Resources.loadDrawable(Resources.java:3056)
09-21 10:31:26.680: I/dalvikvm(19975): at android.content.res.Resources.getDrawable(Resources.java:1613)
09-21 10:31:26.680: I/dalvikvm(19975): at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
09-21 10:31:26.680: I/dalvikvm(19975): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:973)
09-21 10:31:26.680: I/dalvikvm(19975): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:913)
09-21 10:31:26.680: I/dalvikvm(19975): at android.content.res.Resources.loadDrawable(Resources.java:3038)
09-21 10:31:26.680: I/dalvikvm(19975): at android.content.res.Resources.getDrawable(Resources.java:1613)
09-21 10:31:26.680: I/dalvikvm(19975): at android.view.View.setBackgroundResource(View.java:16144)
09-21 10:31:26.680: I/dalvikvm(19975): at com.kissta.abcanimals.Gamescreen$16.onClick(Gamescreen.java:390)
09-21 10:31:26.680: I/dalvikvm(19975): at android.view.View.performClick(View.java:4633)
09-21 10:31:26.680: I/dalvikvm(19975): at android.view.View$PerformClick.run(View.java:19330)
09-21 10:31:26.680: I/dalvikvm(19975): at android.os.Handler.handleCallback(Handler.java:733)
09-21 10:31:26.680: I/dalvikvm(19975): at android.os.Handler.dispatchMessage(Handler.java:95)
09-21 10:31:26.680: I/dalvikvm(19975): at android.os.Looper.loop(Looper.java:157)
09-21 10:31:26.680: I/dalvikvm(19975): at android.app.ActivityThread.main(ActivityThread.java:5356)
09-21 10:31:26.680: I/dalvikvm(19975): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 10:31:26.680: I/dalvikvm(19975): at java.lang.reflect.Method.invoke(Method.java:515)
09-21 10:31:26.680: I/dalvikvm(19975): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-21 10:31:26.680: I/dalvikvm(19975): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-21 10:31:26.680: I/dalvikvm(19975): at dalvik.system.NativeStart.main(Native Method)
09-21 10:31:26.680: D/skia(19975): --- allocation failed for scaled bitmap
09-21 10:31:26.680: D/AndroidRuntime(19975): Shutting down VM
09-21 10:31:26.680: W/dalvikvm(19975): threadid=1: thread exiting with uncaught exception (group=0x418d3da0)
09-21 10:31:26.680: E/AndroidRuntime(19975): FATAL EXCEPTION: main
09-21 10:31:26.680: E/AndroidRuntime(19975): Process: com.kissta.abcanimals, PID: 19975
09-21 10:31:26.680: E/AndroidRuntime(19975): java.lang.OutOfMemoryError
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.content.res.Resources.loadDrawable(Resources.java:3056)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.content.res.Resources.getDrawable(Resources.java:1613)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:973)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:913)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.content.res.Resources.loadDrawable(Resources.java:3038)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.content.res.Resources.getDrawable(Resources.java:1613)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.view.View.setBackgroundResource(View.java:16144)
09-21 10:31:26.680: E/AndroidRuntime(19975): at com.kissta.abcanimals.Gamescreen$16.onClick(Gamescreen.java:390)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.view.View.performClick(View.java:4633)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.view.View$PerformClick.run(View.java:19330)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.os.Handler.handleCallback(Handler.java:733)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.os.Handler.dispatchMessage(Handler.java:95)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.os.Looper.loop(Looper.java:157)
09-21 10:31:26.680: E/AndroidRuntime(19975): at android.app.ActivityThread.main(ActivityThread.java:5356)
09-21 10:31:26.680: E/AndroidRuntime(19975): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 10:31:26.680: E/AndroidRuntime(19975): at java.lang.reflect.Method.invoke(Method.java:515)
09-21 10:31:26.680: E/AndroidRuntime(19975): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-21 10:31:26.680: E/AndroidRuntime(19975): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-21 10:31:26.680: E/AndroidRuntime(19975): at dalvik.system.NativeStart.main(Native Method)
aa.png,bb.png等只有64px乘64px,单个“左”图像是240px乘240px
答案 0 :(得分:1)
嗯,也许这不是你预期的优化内存使用的答案,因为你不发布你的任何代码(只有布局),但是这个hack会把你的内存堆增加到最大值。
将此配置android:largeHeap="true"
添加到清单中的应用程序上下文中。
答案 1 :(得分:0)
您的布局未导致OOM错误。布局看起来很好。
您可能在游戏/应用中分配了太多内存。检查您的代码,看看您的位图/绘图如何分配和使用,或在此处发布您的代码供我们查看。
如果要调试内存的分配方式,请点击以下链接: https://developer.android.com/tools/debugging/debugging-memory.html
这些抽屉是什么aa,bb,cc ....等
它们的大小是什么?
您在哪个文件夹中放置了它们?
您使用的是什么设备?
答案 2 :(得分:0)
似乎错误的原因是你的绘图。您应该尝试使用一些可以为您处理图像缓存的第三方库。一个例子是Picasso。这是一个非常好的图像库,易于使用。