安卓学习笔记33:实现逐帧动画

1、逐帧动画

(一)逐帧动画概念

  • 逐帧动画(Frame-by-Frame Animation)是一种常见的动画形式,其原理是在“连续关键帧”中分解动画动做,也就是在时间轴的每帧上逐帧绘制不一样的内容,使其连续播放而成动画。 由于逐帧动画的帧序列内容不同,不但给制做增长了负担,并且最终输出的文件量也很大,但它的优点也很明显,逐帧动画具备很是大的灵活性,几乎能够表现任何想表现的内容,而它相似与电影的播放模式,很适合于表演细腻的动画。例如人物或动物急剧转身、 头发及衣服的飘动、走路、说话以及精致的3D效果等等。

(二)逐帧动画实现方式

  1. 利用动画资源文件实现逐帧动画
  2. 利用Thread和Handler来实现逐帧动画
  3. 利用Timer和TimerTask来实现逐帧动画

2、利用动画资源文件实现逐帧动画 - 功夫熊猫

(一)运行效果

(二)实现步骤

一、建立安卓应用【PandaAnimation01】

在这里插入图片描述
在这里插入图片描述

二、将图片素材拷贝到drawable目录

在这里插入图片描述

三、建立动画资源文件 - panda_animator.xml

  • 在drawable目录里建立panda_animator.xml
    在这里插入图片描述
<?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_f1" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f2" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f3" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f4" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f5" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f6" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f7" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f8" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f9" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f10" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f11" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f12" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f13" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f14" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f15" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f16" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f17" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f18" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f19" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f20" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f21" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f22" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f23" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f24" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f25" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f26" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f27" android:duration="60"/>
</animation-list>

四、主布局资源文件activity_main.xml

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" >

    <ImageView android:id="@+id/iv_panda" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/panda_animator" />

    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" >

        <Button android:id="@+id/btnPlay" android:layout_width="150dp" android:layout_height="wrap_content" android:onClick="doPlay" android:text="@string/play" />

        <Button android:id="@+id/btnStop" android:layout_width="150dp" android:layout_height="wrap_content" android:onClick="doStop" android:text="@string/stop" />
    </LinearLayout>
</LinearLayout>

本文同步分享在 博客“howard2005”(CSDN)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。android

相关文章
相关标签/搜索