获取 AnimationDrawable对象之后,接下来就可用把 AnimationDrawable显示出来,—–习惯上把AnimationDrawable设为ImageView的背景即可.
需要注意的是 AnimationDrawable代表的动画默认是不播放的.必须在程序中启动动画播放才可以.AnimationDrawable 提供了两个方法() : 开始停止动画.
- start( ): 开始播放动画.
- stop( ): 停止播放动画.
package com.test.frameanimation;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
/**
* 帧动画 AnimationDrawable
*/
public class MainActivity extends AppCompatActivity {
Button play, stop;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.btn_start);
stop = (Button) findViewById(R.id.btn_stop);
image = (ImageView) findViewById(R.id.id_image);
//获取 AnimationDrawable 对象
final AnimationDrawable anim = (AnimationDrawable) image.getBackground();
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
anim.start();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
anim.stop();
}
});
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.test.frameanimation.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始动画"/>
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止动画"/>
</LinearLayout>
<ImageView
android:layout_gravity="center"
android:id="@+id/id_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/fat_panda"
android:scaleType="fitXY"
/>
</LinearLayout>
动画资源文件
<?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/fat_po_f01" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f02" android:duration="60"/>
<item android:drawable="@drawable/fat_po_f03" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f04" android:duration="60"/>
<item android:drawable="@drawable/fat_po_f05" android:duration="60"/>
<item android:drawable="@drawable/fat_po_f06" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f07" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f08" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f09" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f10" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f11" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f12" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f13" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f14" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f15" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f16" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f17" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f18" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f19" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f20" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f21" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f22" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f23" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f24" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f25" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f26" android:duration="160"/>
<item android:drawable="@drawable/fat_po_f27" android:duration="160"/>
</animation-list>