Je fais les choses assez simplement mais j'ai beaucoup d'images. Il semble que ce soit la quantité d'images qui provoque l'exception. J'ai essayé de réduire la taille de chaque image, mais cela ne semble pas affecter beaucoup. C'est avant même que l'animation commence. À moins de montrer moins de cadres, que peut-on faire? Y a-t-il une autre approche?Android: Exception OutOfMemory lors du chargement de l'animation
Adapté à partir du code exemple:
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);
}
}
la mise en page:
<?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>
la liste d'animation:
<?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>
Pourquoi y a-t-il 2 classes pour une animation? – user432209
Les classes auxiliaires supplémentaires permettent de réduire la charge utile sur le thread principal. De nombreuses façons de s'y prendre. Il y a quelques discussions sur la bonne/meilleure façon de s'y prendre. – Jahmic