Android:加载动画时出现OutOfMemory异常
我做的事情很简单,但我有很多框架。似乎是导致异常的帧数。我尝试过减小每帧的大小,但这似乎效果不大。这甚至是在动画开始之前。除了显示更少的帧之外,还能做什么?还有另一种方法吗?
改编自示例代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一些小的重构和代码清理、擦除模拟器中的数据并重新开始后,这些问题就消失了。在真机上测试,这部分代码运行得相当流畅。
所以,有时,只是开始新的锻炼。
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.