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

mak*_*kki 13 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();
        }
});
Run Code Online (Sandbox Code Playgroud)

小智 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);
}
Run Code Online (Sandbox Code Playgroud)

尝试一下,绝对应该工作.


mak*_*kki 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()
Run Code Online (Sandbox Code Playgroud)