嗨我试图在Android应用程序中设置一系列图像作为我的启动页面的背景设置框架动画,此时没有错误但是当我运行应用程序时它意外退出。 10个垃圾收集之后的第一个错误是E / dalvikvm-heap(276):1234272字节的外部分配对于此过程来说太大了。我有17个用于帧动画的图像,每个图像都是50k或更少,所以我不认为图像尺寸可能是问题。
我在主要活动中实现的java代码如下:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView frameanimation = (ImageView) findViewById(R.id.frame_animation);
AnimationDrawable frame_animation = (AnimationDrawable) frameanimation.getBackground();
frame_animation.setVisible(true, true);
frame_animation.start();
}
然后,我将此代码在我的XML布局文件中实现图像视图:
<ImageView
android:id="@+id/frame_animation"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/frame_animation"
android:background="@drawable/frame_animation"/>
最后我的动画drawable在我的drawable文件夹中保存为'frame_animation.xml'。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/homebckgrnda1" android:duration="200" />
<item android:drawable="@drawable/homebckgrnda2" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda3" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda4" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda5" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda6" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda7" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda8" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda9" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda10" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda11" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda12" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda13" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda14" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda15" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda16" android:duration="50" />
<item android:drawable="@drawable/homebckgrnda17" android:duration="50" />
</animation-list>
运行应用程序时获得的完整LogCat日志如下所示:
04-16 13:51:33.703: D/dalvikvm(276): GC_EXTERNAL_ALLOC freed 906 objects / 64688 bytes in 69ms
04-16 13:51:33.983: D/dalvikvm(276): GC_EXTERNAL_ALLOC freed 208 objects / 9952 bytes in 66ms
04-16 13:51:34.203: D/dalvikvm(276): GC_EXTERNAL_ALLOC freed 54 objects / 2112 bytes in 51ms
04-16 13:51:34.443: D/dalvikvm(276): GC_EXTERNAL_ALLOC freed 13 objects / 536 bytes in 52ms
04-16 13:51:34.683: D/dalvikvm(276): GC_EXTERNAL_ALLOC freed 13 objects / 512 bytes in 54ms
04-16 13:51:35.133: D/dalvikvm(276): GC_EXTERNAL_ALLOC freed 20 objects / 824 bytes in 52ms
04-16 13:51:35.593: D/dalvikvm(276): GC_EXTERNAL_ALLOC freed 26 objects / 1216 bytes in 70ms
04-16 13:51:36.023: D/dalvikvm(276): GC_EXTERNAL_ALLOC freed 24 objects / 1008 bytes in 51ms
04-16 13:51:36.394: D/dalvikvm(276): GC_EXTERNAL_ALLOC freed 26 objects / 1128 bytes in 55ms
04-16 13:51:36.803: D/dalvikvm(276): GC_EXTERNAL_ALLOC freed 24 objects / 984 bytes in 50ms
04-16 13:51:37.143: E/dalvikvm-heap(276): 1234272-byte external allocation too large for this process.
04-16 13:51:37.143: E/GraphicsJNI(276): VM won't let us allocate 1234272 bytes
04-16 13:51:37.153: D/AndroidRuntime(276): Shutting down VM
04-16 13:51:37.153: W/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-16 13:51:37.203: E/AndroidRuntime(276): FATAL EXCEPTION: main
04-16 13:51:37.203: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.BadmintonTrain/com.android.BadmintonTrain.BadmintonTrainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class android.widget.ImageView
04-16 13:51:37.203: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-16 13:51:37.203: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 13:51:37.203: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
04-16 13:51:37.203: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-16 13:51:37.203: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-16 13:51:37.203: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
04-16 13:51:37.203: E/AndroidRuntime(276): Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class android.widget.ImageView
04-16 13:51:37.203: E/AndroidRuntime(276): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
04-16 13:51:37.203: E/AndroidRuntime(276): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-16 13:51:37.203: E/AndroidRuntime(276): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.app.Activity.setContentView(Activity.java:1647)
04-16 13:51:37.203: E/AndroidRuntime(276): at com.android.BadmintonTrain.BadmintonTrainActivity.onCreate(BadmintonTrainActivity.java:21)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-16 13:51:37.203: E/AndroidRuntime(276): ... 11 more
04-16 13:51:37.203: E/AndroidRuntime(276): Caused by: java.lang.reflect.InvocationTargetException
04-16 13:51:37.203: E/AndroidRuntime(276): at android.widget.ImageView.<init>(ImageView.java:108)
04-16 13:51:37.203: E/AndroidRuntime(276): at java.lang.reflect.Constructor.constructNative(Native Method)
04-16 13:51:37.203: E/AndroidRuntime(276): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
04-16 13:51:37.203: E/AndroidRuntime(276): ... 22 more
04-16 13:51:37.203: E/AndroidRuntime(276): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
04-16 13:51:37.203: E/AndroidRuntime(276): at android.graphics.Bitmap.nativeCreate(Native Method)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.content.res.Resources.loadDrawable(Resources.java:1709)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.content.res.Resources.getDrawable(Resources.java:581)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:265)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.content.res.Resources.loadDrawable(Resources.java:1694)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.view.View.<init>(View.java:1885)
04-16 13:51:37.203: E/AndroidRuntime(276): at android.widget.ImageView.<init>(ImageView.java:112)
非常感谢任何帮助,我无法弄清楚导致问题的原因。
由于
答案 0 :(得分:2)
您正在使用的图像可能对于动画而言太大。它们有多大?在动画中,Android将所有图像加载到内存中并解压缩它们意味着每个像素将占用4个字节。所以50k意味着你的图像是111px x 111px。从错误来看,每帧约为480 x 640,这真的很大。尝试使用较小的图像。