DynamicDrawable以编程方式没有animation-list xml

时间:2012-04-13 23:18:49

标签: android android-animation

我处于想要从动态图像列表中显示动画的情况。我想为此目的使用AnimationDrawable,但不幸的是我被卡住了...... :(我的代码是

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();
        }
});

2 个答案:

答案 0 :(得分:5)

所以Animation.addFrame(Drawable frame,int duration)采用了一个可以从Bitmap创建的drawable。

如果动态加载图像列表:

List<Bitmap> images;
int duration = 200;    

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

尝试这一点,绝对应该有用。

答案 1 :(得分:5)

我在循环之后做了animation.start()并从runnable中调用了函数并且它正常工作

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()