android逐帧动画-与这两个代码的区别

android逐帧动画-与这两个代码的区别,android,animation,frame,runnable,Android,Animation,Frame,Runnable,我想动画一些图像。 有人能告诉我为什么第一段代码不起作用,而第二段代码起作用吗? 如果我必须使用第二个,我如何停止动画进入runnable 编辑:第一段代码适用于android 4.x,但不适用于2.2(模拟器和设备) 代码1(进入“onCreate()”): ImageView boule = (ImageView)findViewById(R.id.boule); boule.setImageBitmap(null); boule.setBackgroundResource(R.anim

我想动画一些图像。 有人能告诉我为什么第一段代码不起作用,而第二段代码起作用吗? 如果我必须使用第二个,我如何停止动画进入runnable

编辑:第一段代码适用于android 4.x,但不适用于2.2(模拟器和设备)

代码1(进入“onCreate()”):

ImageView boule = (ImageView)findViewById(R.id.boule);  
boule.setImageBitmap(null);
boule.setBackgroundResource(R.anim.anime);
AnimationDrawable animation = (AnimationDrawable)boule.getBackground();
animation.start();
// does not animate anything...
ImageView boule = (ImageView)findViewById(R.id.boule);
boule.setImageBitmap(null);
boule.setBackgroundResource( R.anim.anime );
final AnimationDrawable animation = (AnimationDrawable) boule.getBackground();
boule.post(new Runnable() {
    public void run() {
        if ( animation != null ) animation.start();
        }
    });
// OK, it works, but how do I stop this ?
代码2(也输入到“onCreate()”):

ImageView boule = (ImageView)findViewById(R.id.boule);  
boule.setImageBitmap(null);
boule.setBackgroundResource(R.anim.anime);
AnimationDrawable animation = (AnimationDrawable)boule.getBackground();
animation.start();
// does not animate anything...
ImageView boule = (ImageView)findViewById(R.id.boule);
boule.setImageBitmap(null);
boule.setBackgroundResource( R.anim.anime );
final AnimationDrawable animation = (AnimationDrawable) boule.getBackground();
boule.post(new Runnable() {
    public void run() {
        if ( animation != null ) animation.start();
        }
    });
// OK, it works, but how do I stop this ?

你可以试试这个。。此代码工作正常

BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.jth);
BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.jthj);
int duration = 10;
final AnimationDrawable ad = new AnimationDrawable();
ad.setOneShot(false);
ad.addFrame(frame1, duration);
ad.addFrame(frame2, duration);
iv.setBackgroundDrawable(ad);
ad.setVisible(true, true);

放置ad.start();在按钮onClickListener和ad.stop()中;谢天谢地,谢天谢地,但这不管用。我已经将所有代码放在onCreate方法中(在用我自己的方法替换资源之后),然后在下面添加“ad.start()”,然后。。。什么也没发生。我只看到第一张图片。