2012-04-13 26 views
12

我處於我想從動態圖像列表中顯示動畫的情況。我想用AnimationDrawable用於此目的,但不幸的是我堅持... :(我的代碼是AnimationDrawable沒有動畫列表xml

ImageView globe = (ImageView) findViewById(R.id.globe); 

AnimationDrawable animation = new AnimationDrawable(); 
animation.setOneShot(true); 

Resources res = this.getResources(); 

while (from <= to) { 
    String name = "globe_" + String.format("%03d", from); 
    int globeId = res.getIdentifier(name, "drawable", 
      this.getPackageName()); 
    animation.addFrame(res.getDrawable(globeId), 200); 
    from++; 
} 

globe.setBackgroundDrawable(animation); 
globe.post(new Runnable(){ 
     @Override 
     public void run() { 
      animation.start(); 
     } 
}); 

回答

5

我只是做了animation.start() AFTE R上的循環,並呼籲從一個可運行的功能和它的工作

while (from <= to) { 
    String name = "globe_" + String.format("%03d", from); 
    int globeId = res.getIdentifier(name, "drawable", 
      this.getPackageName()); 

    animation.addFrame(res.getDrawable(globeId), 200); 
    from++; 
} 

globe.setBackgroundDrawable(animation); 

animation.start() 
5

所以Animation.addFrame(可繪製幀,INT持續時間)需要,你可以從創建繪製位圖

如果是動態加載圖片的列表:。

List<Bitmap> images; 
int duration = 200;  

for(Bitmap image: images){ 
    BitmapDrawable frame = new BitmapDrawable(image); 
    animation.addFrame(frame, duration); 
} 

嘗試了這一點,一定要努力

+0

感謝您的答覆,但我已經將通過animation.addFrame提拉(res.getDrawable(globeId),200);我也試過你的方式,但沒有運氣:( – makki 2012-04-14 06:22:10

+0

對不起,請看看這個linke,告訴我它是怎麼回事:http://androidforums.com/application-development/11620-programmatic-frame-frame - 動畫 - examle-animationdrawable.html – 2012-04-14 06:30:09