Android:加载动画时出现OutOfMemory异常

发布于 2024-11-09 01:00:39 字数 2672 浏览 0 评论 0原文

我做的事情很简单,但我有很多框架。似乎是导致异常的帧数。我尝试过减小每帧的大小,但这似乎效果不大。这甚至是在动画开始之前。除了显示更少的帧之外,还能做什么?还有另一种方法吗?

改编自示例代码:

public class XMLAnimation extends Activity
{
    class MyAnimationRoutine extends TimerTask
    {
        MyAnimationRoutine()
        {
        }

        @Override
        public void run()
        {
            ImageView img = (ImageView) findViewById(R.id.simple_anim);
            AnimationDrawable frameAnimation = (AnimationDrawable) img
                    .getBackground();

            frameAnimation.start();
        }
    }

    class MyAnimationRoutine2 extends TimerTask
    {
        MyAnimationRoutine2()
        {
        }

        @Override
        public void run()
        {
            ImageView img = (ImageView) findViewById(R.id.simple_anim);
            AnimationDrawable frameAnimation = (AnimationDrawable) img
                    .getBackground();

            frameAnimation.stop();
        }
    }

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        ImageView img = (ImageView) findViewById(R.id.simple_anim);
        img.setBackgroundResource(R.anim.simple_animation);

        MyAnimationRoutine mar = new MyAnimationRoutine();
        MyAnimationRoutine2 mar2 = new MyAnimationRoutine2();

        Timer t = new Timer(false);
        t.schedule(mar, 100);
        Timer t2 = new Timer(false);
        t2.schedule(mar2, 5000);
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
      <ImageView android:id="@+id/simple_anim"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:layout_centerHorizontal="true"
           /> 
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World, XMLAnimation"
    />
</LinearLayout>

动画列表:

<?xml version="1.0" encoding="utf-8"?>
 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false">
    <item android:drawable="@drawable/frame1" android:duration="50" />
    ...
    <item android:drawable="@drawable/frame40" android:duration="50" />
 </animation-list>

I'm doing things pretty simply but I have a lot of frames. It seems to be the amount of frames that causes the exception. I've tried to reduce the size of each frame, but that doesn't seem to effect much. This is even before the animation starts. Short of showing less frames, what can be done? Is there another approach?

Adapted from sample code:

public class XMLAnimation extends Activity
{
    class MyAnimationRoutine extends TimerTask
    {
        MyAnimationRoutine()
        {
        }

        @Override
        public void run()
        {
            ImageView img = (ImageView) findViewById(R.id.simple_anim);
            AnimationDrawable frameAnimation = (AnimationDrawable) img
                    .getBackground();

            frameAnimation.start();
        }
    }

    class MyAnimationRoutine2 extends TimerTask
    {
        MyAnimationRoutine2()
        {
        }

        @Override
        public void run()
        {
            ImageView img = (ImageView) findViewById(R.id.simple_anim);
            AnimationDrawable frameAnimation = (AnimationDrawable) img
                    .getBackground();

            frameAnimation.stop();
        }
    }

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        ImageView img = (ImageView) findViewById(R.id.simple_anim);
        img.setBackgroundResource(R.anim.simple_animation);

        MyAnimationRoutine mar = new MyAnimationRoutine();
        MyAnimationRoutine2 mar2 = new MyAnimationRoutine2();

        Timer t = new Timer(false);
        t.schedule(mar, 100);
        Timer t2 = new Timer(false);
        t2.schedule(mar2, 5000);
    }
}

the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
      <ImageView android:id="@+id/simple_anim"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:layout_centerHorizontal="true"
           /> 
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World, XMLAnimation"
    />
</LinearLayout>

the animation list:

<?xml version="1.0" encoding="utf-8"?>
 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false">
    <item android:drawable="@drawable/frame1" android:duration="50" />
    ...
    <item android:drawable="@drawable/frame40" android:duration="50" />
 </animation-list>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以畏孤独 2024-11-16 01:00:39

经过一些小的重构和代码清理、擦除模拟器中的数据并重新开始后,这些问题就消失了。在真机上测试,这部分代码运行得相当流畅。

所以,有时,只是开始新的锻炼。

After some minor refactoring and code clean-up, wiping data in the emulator, and starting fresh, these problems went away. Testing on the actual device, this part of code works quite smoothly.

So, sometimes, just starting fresh works out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文