0

我想为一些图像制作动画。有人能告诉我为什么第一段代码不起作用,而第二段代码起作用吗?如果我必须使用第二个,我如何将动画停止到 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...

代码 2(也进入 "onCreate()" ):

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 ?
4

1 回答 1

0

你可以试试这个..这段代码可以正常工作

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(); 停止动画

于 2012-10-27T11:39:29.430 回答