今天我们就来学习逐帧动画,废话少说直接上效果图如下:
帧动画的实现方式有两种:
一、在res/drawable文件夹下新建animation-list的XML实现帧动画
1、首先在res/drawable文件夹下添加img00-img24共25张图片
2、新建frame_anim.xml
- <?xml version="1.0" encoding="utf-8"?>
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="true" >
- <!-- animation-list 帧动画 -->
- <!-- android:oneshot的值为 false代表播放多次,true代表只播放一次 -->
- <!-- duration代表每张图片的播放时间 ,定义一个持续时间为50毫秒的动画帧 -->
- <item
- android:drawable="@drawable/img00"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img01"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img02"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img03"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img04"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img05"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img06"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img07"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img08"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img09"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img10"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img11"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img12"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img13"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img14"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img15"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img16"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img17"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img18"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img19"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img20"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img21"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img22"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img23"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img24"
- android:duration="50"/>
- </animation-list>
- <RelativeLayout 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"
- tools:context="com.havorld.frameanimation.MainActivity" >
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true" />
- <!-- android:background="@drawable/frame_anim" -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:orientation="horizontal"
- android:padding="10dp" >
- <Button
- android:id="@+id/start"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="播放" />
- <Button
- android:id="@+id/stop"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="停止" />
- </LinearLayout>
- </RelativeLayout>
4、在代码中获取并开启帧动画
- public class MainActivity extends Activity implements OnClickListener {
- private ImageView imageView;
- private AnimationDrawable animationDrawable;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imageView = (ImageView) findViewById(R.id.imageView);
- findViewById(R.id.start).setOnClickListener(this);
- findViewById(R.id.stop).setOnClickListener(this);
- setXml2FrameAnim1();
- // setXml2FrameAnim2();
- }
- /**
- * 通过XML添加帧动画方法一
- */
- private void setXml2FrameAnim1() {
- // 把动画资源设置为imageView的背景,也可直接在XML里面设置
- imageView.setBackgroundResource(R.drawable.frame_anim);
- animationDrawable = (AnimationDrawable) imageView.getBackground();
- }
- /**
- * 通过XML添加帧动画方法二
- */
- private void setXml2FrameAnim2() {
- // 通过逐帧动画的资源文件获得AnimationDrawable示例
- animationDrawable = (AnimationDrawable) getResources().getDrawable(
- R.drawable.frame_anim);
- imageView.setBackground(animationDrawable);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.start:
- if (animationDrawable != null && !animationDrawable.isRunning()) {
- animationDrawable.start();
- }
- break;
- case R.id.stop:
- if (animationDrawable != null && animationDrawable.isRunning()) {
- animationDrawable.stop();
- }
- break;
- default:
- break;
- }
- }
- }
二、通过代码实现帧动画
- /**
- * 通过代码添加帧动画方法
- */
- private void setSrc2FrameAnim() {
- animationDrawable = new AnimationDrawable();
- // 为AnimationDrawable添加动画帧
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img00), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img01), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img02), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img03), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img04), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img05), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img06), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img07), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img08), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img09), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img10), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img11), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img12), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img13), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img14), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img15), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img16), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img17), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img18), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img19), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img20), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img21), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img22), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img23), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img24), 50);
- // 设置为循环播放
- animationDrawable.setOneShot(false);
- imageView.setBackground(animationDrawable);
- }