2012-10-27 24 views
0

我想動畫一些圖片。 有人能告訴我爲什麼這第一段代碼不起作用,但第二段有效嗎? 如果我必須使用第二個,我該如何停止動畫進入可運行?android frame by frame動畫 - 與這兩個代碼的區別

編輯:第一代碼適用於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 ? 

回答

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();爲停止行動

+0

謝謝,夏爾,但它不工作。我已經把所有的代碼放在了onCreate方法中(用我自己替換資源後),然後在下面添加「ad.start()」,並且......什麼也沒有發生。我只看到第一張圖片。 – Chrysotribax