AnimationDrawable OutOfMemoryError

时间:2016-12-21 19:07:15

标签: java android animationdrawable

当前版本: Android Studio 2.2.3

我将首先询问,您如何创建动画列表?当您单击res / drawable文件夹 new - >可绘制资源文件您无法将根元素从选择器更改为动画列表。所以我手动更改它。

没关系,我跟着developers.android的instructions here

以下是代码:

RES /抽拉/ anim.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/frame13" android:duration="200" />
<item android:drawable="@drawable/frame14" android:duration="200" />
<item android:drawable="@drawable/frame15" android:duration="200" />
<item android:drawable="@drawable/frame16" android:duration="200" />
<item android:drawable="@drawable/frame17" android:duration="200" />
<item android:drawable="@drawable/frame18" android:duration="200" />
<item android:drawable="@drawable/frame19" android:duration="200" />
<item android:drawable="@drawable/frame20" android:duration="200" />
<item android:drawable="@drawable/frame21" android:duration="200" />
<item android:drawable="@drawable/frame22" android:duration="200" />
<item android:drawable="@drawable/frame23" android:duration="200" />
</animation-list>

activity_main.xml中

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/imageView"
    android:src="@drawable/anim"/>

MainActivity.java

public class MainActivity extends AppCompatActivity {

ImageView imageView;
AnimationDrawable a;

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

    imageView = (ImageView) findViewById(R.id.imageView);
    imageView.setBackgroundResource(R.drawable.anim);
    a = (AnimationDrawable) imageView.getBackground();
}

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        a.start();
        return true;
    }
    return super.onTouchEvent(event);
}
}

你看错了什么?

我至少没有,当我运行它时,会弹出这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: animation.aggelonias.gr.animation, PID: 1146
              java.lang.OutOfMemoryError
                  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
                  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
                  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
                  at android.content.res.Resources.loadDrawable(Resources.java:3077)
                  at android.content.res.Resources.getDrawable(Resources.java:1624)
                  at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
                  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:990)
                  at android.graphics.drawable.Drawable.createFromXml(Drawable.java:930)
                  at android.content.res.Resources.loadDrawable(Resources.java:3059)
                  at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                  at android.widget.ImageView.<init>(ImageView.java:133)
                  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:1017)
                  at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1076)
                  at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
                  at animation.aggelonias.gr.animation.MainActivity.onCreate(MainActivity.java:17)
                  at android.app.Activity.performCreate(Activity.java:5451)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
                  at android.app.ActivityThread.access$900(ActivityThread.java:169)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5476)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                  at dalvik.system.NativeStart.main(Native Method)

在那个烂摊子的某个地方说, MainActivity.java:17 :这是setContentView(R.layout.activity_main);

谢谢!

2 个答案:

答案 0 :(得分:0)

也许您的图像文件frame13,14,...会非常大,或者您的图像类型不是标准图像类型,例如png,jpg。 检查您的图像文件。

尝试在清单中添加android:largeHeap =&#34; true&#34;,以查看是否可以解决此问题。

答案 1 :(得分:0)

如果你的动画,

AnimationDrawable会将所有图片加载到内存中 包含许多帧,可能会导致 Out Of Memory 。所以你应该尝试压缩你的位图。另一种方法是,您可以使用SurfaceView代替AnimationDrawable

View here