天天看点

Android学习笔记--实现帧动画

1.在res-drawable下添加实现帧动画的图片,并在该目录下创建XXX.xml文件将要添加的图片文件加载下来。 android:oneshot="true"表示执行一次,false表示无限循环。android:duration="100"表示每一帧图片间隔时间。

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

    <item
        android:drawable="@drawable/captain1"
        android:duration="100"/>
    <item
        android:drawable="@drawable/captain2"
        android:duration="100"/>
    <item
        android:drawable="@drawable/captain3"
        android:duration="100"/>
    <item
        android:drawable="@drawable/captain4"
        android:duration="100"/>
    <item
        android:drawable="@drawable/captain5"
        android:duration="100"/>
</animation-list>
           

2.再布局文件中添加ImageView控件,将第一步新建的xxx.xml(注意不是添加的n张图片)文件作为他的背景。

<ImageView 
        android:id="@+id/iv_captain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/captain"
        />
           

3.在java文件中是使用AnimationDrable实现帧动画的效果。

ImageView im = (ImageView) this.findViewById(R.id.iv_captain);
			a = (AnimationDrawable) im.getBackground();
			a.start();