Android will be watching the animation (frame animation)

Frame animation (Drawable Animation)

  • Frame Animation: load a series of pictures resources, a display of a player.
  • Implementation steps
    1. Create an xml file in the res / drawable directory, the root is . And can be configured oneshot attribute indicates whether the animation play once again play live.
    2. Add a picture resources in the xml file, and set the picture to play long.
    3. ImageView set the background for the specified resource, and then takes it in the context of Activity and converted into AnimationDrawable and turn on animation.
  • Code demonstrates
    animation_drawable1.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <!--oneshot:控制播放次数 true:播放一次,false:重复不停的播放-->
    <!--duration:每张图片持续时长-->
    <item
        android:drawable="@mipmap/icon_num_1"
        android:duration="500" />
    <item
        android:drawable="@mipmap/icon_num_2"
        android:duration="500" />
    <item
        android:drawable="@mipmap/icon_num_3"
        android:duration="500" />
</animation-list>

AnimationActivity (Activity acquired animation frame specified set the ImageView)

public class AnimationActivity extends BaseActivity {

    private ImageView ivDemo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        ivDemo = findViewById(R.id.ivDemo);
    }

    //帧动画
    public void btDrawable(View view) {
        ivDemo.setBackgroundResource(R.drawable.animation_drawable1);
        AnimationDrawable animation = (AnimationDrawable) ivDemo.getBackground();
        animation.start();
    }
}

Guess you like

Origin www.cnblogs.com/io1024/p/11577743.html