SoFunction
Updated on 2025-04-12

Android programming to realize animation automatic playback function

This article describes the Android programming to implement the automatic playback function of animation. Share it for your reference, as follows:

private ImageView image;
private AnimationDrawable animDrawable = new AnimationDrawable();
@Override
public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.table_title);
    image = (ImageView) ();
    //Code implementation//   for(int i=0; i<16; i++){
//     int id = getResources().getIdentifier("load_"+(i+1), "drawable", getPackageName());
//     (getResources().getDrawable(id), 50);
//   }
//   (false);
//   (animDrawable);
    animDrawable = (AnimationDrawable) ();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    (hasFocus);
    ();
}

Execute () animation in onCreate is not executed. Don't understand
In addition, the above code has been tested with flaws. For example, in TabActivity onWindowFocusChanged will only be executed once

so

AnimationDrawable ad;
@Override
public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    ImageView iv = (ImageView) findViewById(.animation_view);
    ();
    ad = (AnimationDrawable) ();
   //Key Code   ().addOnPreDrawListener(opdl);
}
OnPreDrawListener opdl=new OnPreDrawListener(){
    @Override
    public boolean onPreDraw() {
        if(() == false){
        ();
        }
        return true; //Note that true must be returned    }
};

This method has been tried and it works. very good.

For more information about Android related content, please check out the topic of this site:Android development animation skills summary》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Android file operation skills summary》、《Android resource operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.