Android frame animation

Frame animation

Overview

Frame animation refers to multiple still pictures being played frame after frame, just like playing a movie.

Common attribute methods

oneshot属性:表示是否只播放一次,true表示只会播放一次,false表示一直循环播放
druation:表示此帧持续的时间,整数,单位为毫秒
drawable:表示此帧动画所对应的图片资源

for example

In res/drawableunder a new anim_loading.xmlfile

<?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/a1"
          android:duration="80" />
    <item
          android:drawable="@drawable/a2"
          android:duration="80" />
    <item
          android:drawable="@drawable/a3"
          android:duration="80" />
    <item
          android:drawable="@drawable/a4"
          android:duration="80" />
    <item
          android:drawable="@drawable/a5"
          android:duration="80" />
    <item
          android:drawable="@drawable/a6"
          android:duration="80" />
    <item
          android:drawable="@drawable/a7"
          android:duration="80" />
    <item
          android:drawable="@drawable/a8"
          android:duration="80" />
    <item
          android:drawable="@drawable/a9"
          android:duration="80" />
    <item
          android:drawable="@drawable/a10"
          android:duration="80" />
    <item
          android:drawable="@drawable/a11"
          android:duration="80" />
    <item
          android:drawable="@drawable/a12"
          android:duration="80" />
    <item
          android:drawable="@drawable/a13"
          android:duration="80" />
    <item
          android:drawable="@drawable/a14"
          android:duration="80" />
    <item
          android:drawable="@drawable/a15"
          android:duration="80" />
    <item
          android:drawable="@drawable/a16"
          android:duration="80" />
</animation-list>

Set up the layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center_horizontal"
              android:orientation="vertical">

    <Button
            android:id="@+id/btnStart"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="开始动画" />

    <Button
            android:id="@+id/btnStop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="停止动画" />

    <ImageView
               android:id="@+id/imgAnim"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:layout_marginTop="10dp"
               android:src="@drawable/anim_loading" />

</LinearLayout>

Get frame animation, start and stop animation

//获取帧动画
anim = (AnimationDrawable) imgAnim.getDrawable();
//开始动画
anim.start();
//停止动画
anim.stop();

Code download

Guess you like

Origin blog.csdn.net/qq_14876133/article/details/113755631