【Android】帧动画(Drawable Animation)

来源:互联网 发布:南京市网络车问政平台 编辑:程序博客网 时间:2024/06/05 15:41

今天总结下帧动画
一.原理:
对几张图片按照顺序一张一张进行播放,视觉上感觉是连续播放的动画效果。
二.步骤:
1.在res/drawable放入几张图片
2.在res/drawable下新建一个drawable resource file,比如命名为gril_animation.xml,根节点选择animation-list,点击ok
这里写图片描述
3.编辑gril_animation.xml文件,以animation-list作为根节点,item作为子节点,每个item就是一帧,即一个drawable图片

图片资源是在百度上搜索帧动画找的,然后用了一个很笨的办法,一小张一小张截下来的,总之能用就行了,不要求太完美了@-@

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false"    android:variablePadding="true"    android:visible="true">    <item android:drawable="@drawable/frame_a" android:duration="100"/>    <item android:drawable="@drawable/frame_b" android:duration="100"/>    <item android:drawable="@drawable/frame_c" android:duration="100"/>    <item android:drawable="@drawable/frame_d" android:duration="100"/>    <item android:drawable="@drawable/frame_e" android:duration="100"/>    <item android:drawable="@drawable/frame_f" android:duration="100"/>    <item android:drawable="@drawable/frame_g" android:duration="100"/>    <item android:drawable="@drawable/frame_h" android:duration="100"/></animation-list>

上边代码中涉及到有几个android的属性
android:oneshot 值为true,整个动画只会播放一次,然后停止
android:variablePadding 值为true,允许drawable的padding值基于选中状态发生改变(后期再研究这个属性
android:visible 提供drawable的初始可见状态,默认值为false(此处不太懂,既然是false,为何也可见
android:drawable 每一帧所使用的drawable资源图片
android:duration 显示动画的某一帧的持续时间(单位ms)
4.在一个layout布局文件中写一个ImageView控件作为运行帧动画的载体,代码很简单

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.myanimation.MainActivity"><ImageView    android:id="@+id/img"    android:layout_width="match_parent"    android:layout_height="match_parent" /></RelativeLayout>

5.在Activity文件中编写实现帧动画播放,代码也很简单

package com.example.myanimation;import android.graphics.drawable.AnimationDrawable;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.MotionEvent;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    private AnimationDrawable animation;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //载入将会运行动画的ImageView        ImageView img = (ImageView) findViewById(R.id.img);        //把刚才我们编写的有8张图片的gril_animation文件设置为IamgeView的背景        img.setBackgroundResource(R.drawable.gril_animation);        //获取ImageView的背景信息,它已经被编译为一个AnimationDrawable对象。        animation = (AnimationDrawable) img.getBackground();    }    @Override    public boolean onTouchEvent(MotionEvent event) {    //当手指触发ACTION_DOWN事件时开始动画        if(event.getAction() == MotionEvent.ACTION_DOWN) {            animation.start();        }        return true;    }    @Override    protected void onDestroy() {        super.onDestroy();        //animation不为null时手动置为空以防不能被回收        if(animation != null) {            animation = null;        }    }}

三.重要api
AnimationDrawable AnimationDrawable类是Drawable animations的基础
*它的一个很重要的方法就是start()方法,用于开始播放动画
1.Public constructors(构造函数)
AnimationDrawable()

2.Public methods(public 函数)
(1)void addFrame (Drawable frame, int duration) 为动画添加一帧
frame:将要被添加的帧,这个值不能为null
duration:这个frame帧将要持续出现的时间

(2)int getDuration (int i) 索引为i的帧的持续毫秒值
Parameters
i: 索引值
Returns
int:返回指定索引值的动画帧的持续毫秒值

(3)Drawable getFrame (int index) 指定帧索引的drawable

(4)int getNumberOfFrames () 得到动画所含有的帧数量

(5) void inflate (Resources r,
XmlPullParser parser,
AttributeSet attrs,
Resources.Theme theme)

(6)boolean isOneShot ()   是否只播放一次(7)boolean isRunning ()   表示动画是否正在运行(8)Drawable mutate ()   返回值不能为null (9)void run ()   这种方法只存在于实现目的,不应该直接调用。调用start()替代。 (10)void setOneShot (boolean oneShot) 设置动画是不是只播放一次 (11)boolean setVisible (boolean visible, boolean restart) 设置这个帧动画是否可见 (12)void start () 从第一帧开始动画,必要时循环播放。如果动画正在播放,这个方法没有效果。 **注意**:不要在Activity的 onCreate(Bundle)方法中调用它,因为帧动画还没有完全附加到窗口,如果你想在不需要交互的情况下立即播放动画,您可能想要在您的Activity的onWindowFocusChanged(boolean)方法中调用它,当Android让你的窗口聚焦时这一功能就会被调用 不过我尝试了下把start()方法放到onCreate(Bundle)方法中貌似也没有影响(**后期研究这块**) (13)void stop ()在当前帧暂停动画,如果动画没有在播放此方法无效(14)void unscheduleSelf (Runnable what)不太懂3.Protected methods(缺省函数)void setConstantState (DrawableContainer.DrawableContainerState state)设置常量状态,参数state不能为null对以上api的简单应用
animation = new AnimationDrawable();//构造函数        animation.addFrame(getDrawable(R.drawable.frame_a), 100);        animation.addFrame(getDrawable(R.drawable.frame_b), 100);        animation.addFrame(getDrawable(R.drawable.frame_c), 100);        animation.addFrame(getDrawable(R.drawable.frame_d), 100);        animation.addFrame(getDrawable(R.drawable.frame_e), 100);        animation.addFrame(getDrawable(R.drawable.frame_f), 100);        animation.addFrame(getDrawable(R.drawable.frame_g), 100);        animation.addFrame(getDrawable(R.drawable.frame_h), 100);        int firstDuration = animation.getDuration(0);//得到第一帧的持续时间        Drawable secondDrawable = animation.getFrame(2);//得到第二帧的那张drawable图片        int framesNum = animation.getNumberOfFrames();//得到这个帧动画所含有的帧数量        boolean isOneShot = animation.isOneShot();//判断是否只播放一次        boolean isRunning = animation.isRunning();//动画是否正在运行        animation.setOneShot(true);//设置动画是不是只播放一次        if(animation.isRunning()) {            animation.stop();//在当前帧暂停动画,如果动画没有在播放此方法无效        }
阅读全文
0 0
原创粉丝点击