Android animation mechanism one - frame-by-frame animation

Frame Animation, also known as Drawable Animation, is the simplest and most intuitive type of animation, mainly using the visual persistence effect of the human eye.

Implementation principle: a series of constantly changing pictures, according to a certain order and dwell time, are constantly switched to achieve an animation effect

Implementation method: divided into XML resource file method and code method

XML file method

This is the most common way, first put the picture of each frame into the resource file directory. Then create a new animation XML file in the resource file directory, use the <animation-list> tag in this file to define the sequence of animation frames, use the <item> tag to define each frame of the animation, and specify the duration of the frame in it Time and other attributes, the case code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">

    <item
        android:drawable="@drawable/waiting0"
        android:duration="150"></item>
    <item
        android:drawable="@drawable/waiting1"
        android:duration="150"></item>
    <item
        android:drawable="@drawable/waiting2"
        android:duration="150"></item>
    <item
        android:drawable="@drawable/waiting3"
        android:duration="150"></item>

</animation-list>

Among them, android.onshot is used to control whether the animation is played in a loop. If it is false, it means looped playback. The android.duration in the <item> tag is used to specify the duration of each frame.

The code used in Activity is as follows:

public class FrameAnimationActivity extends AppCompatActivity {

    @BindView(R.id.frameAnimation_test_iv)
    ImageView frameAnimation_test_iv;

    AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_frameanimetion);
        ButterKnife.bind(this);

        frameAnimation_test_iv.setImageResource(R.drawable.test_frameanimation);
        animationDrawable = (AnimationDrawable) frameAnimation_test_iv.getDrawable();
    }

    @Override
    protected void onResume() {
        super.onResume();
        animationDrawable.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        animationDrawable.stop();
    }
}

code method

It is also relatively simple to define frame-by-frame animation in the code, but the code that is not commonly used is as follows:

animationDrawable = new AnimationDrawable();
        for(int i=0;i<4;i++){
            int id = getResources().getIdentifier("waiting"+i,"drawable",getPackageName());
            Drawable drawable = getResources().getDrawable(id);
            animationDrawable.addFrame(drawable,150);
        }
        animationDrawable.setOneShot (false);

        frameAnimation_test_iv.setImageResource(R.drawable.test_frameanimation);
        animationDrawable = (AnimationDrawable) frameAnimation_test_iv.getDrawable();

The method of starting and ending animation is the same as above


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324967618&siteId=291194637