Android之逐帧动画

来源:互联网 发布:unity3d五子棋源代码 编辑:程序博客网 时间:2024/04/29 16:21
                                                                              逐帧动画
含义:逐帧动画是把动画过程的每张静态图片
都收集起来,然后由Android来控制依次
显示这些静态图片,然后利用人眼视觉暂
留的原理,给用户造成动画的错觉。
逐帧动画的动画原理与放电影的原理完全一样。
1.定义逐帧动画---/res/drawable目录中
  只要在<animation-list>元素中使用<item>子元素
定义动画的全部帧,并制定各帧的持续时间即可,
定义逐帧动画的语法格式:
  <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
                  android:oneshot="true|false">
<item android:drawable=""
android:duration="">
  </animation-list>
其中android:oneshot控制该动画是否循环播放。
    如果为true,动画将不会循环播放,
    否则该动画将会循环播放
    每一个item子元素添加一帧
 注意:Android完成支持在Java代码中创建逐帧动画,
 先创建AnimationDrawable对象,然后调用
 addFrame(Drawable frame,int duration)向该
 动画中添加帧,每调用一次addFrame方法,
 就相当于添加一个item元素。


 2.在android代码中使用逐帧动画的步骤
 a)将逐帧动画的文件设置给某个组件的背景图片
 b)获取AnimationDrawable动画对象
    AnimationDrawable anim=
    (AnimationDrawable)image.getBackground();
 c)开始播放动画,逐帧动画默认是不播放的
    anim.start()
 d)停止动画

   anim.stop();

3.代码编写:

先看效果图:


activity_main:

<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"     >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="开始动画"         android:onClick="mystart"/>     <ImageView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/img"        android:background="<span style="color:#ff0000;">@drawable/my_anim</span>"        android:layout_centerInParent="true"/>         </RelativeLayout>

布局文件中的my_anim是在res文件下新建了一个drawable文件,在drawable新建了个my_anim,代码如下:

<?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/brownmonsters02"       android:duration="1000"></item>   <item android:drawable="@drawable/brownmonsters03"       android:duration="500"></item>   <item android:drawable="@drawable/brownmonsters04"       android:duration="800"></item>   <item android:drawable="@drawable/brownmonsters05"       android:duration="600"></item>   <item android:drawable="@drawable/brownmonsters19"       android:duration="600"></item></animation-list>
MainActivity中就几行代码:

public void mystart(View view) {// TODO Auto-generated method stub//播放逐帧动画AnimationDrawable drawable=(AnimationDrawable) iv.getBackground();drawable.start();}



0 0
原创粉丝点击