Android基础篇之逐帧动画(Frame by Frame)

来源:互联网 发布:手机数据恢复精灵 编辑:程序博客网 时间:2024/05/22 06:26

奔腾的小马效果图:
这里写图片描述

逐帧动画是一种常见的动画形式(Frame By Frame),通过多张图片连续播放实现的一种动画,类似于GIF,其原理是在“连续的关键帧”中分解动画动作,也就是在时间轴的每帧上逐帧绘制不同的内容,使其连续播放而成动画。 因为逐帧动画的帧序列内容不一样,不但给制作增加了负担而且最终输出的文件量也很大,但它的优势也很明显:逐帧动画具有非常大的灵活性,几乎可以表现任何想表现的内容,而它类似与电影的播放模式,很适合于表演细腻的动画。例如:人物或动物急剧转身、 头发及衣服的飘动、走路、说话以及精致的3D效果等等。

代码:
activity_main

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"    android:orientation="vertical">    <ImageView        android:background="@drawable/main"        android:id="@+id/iv_imageview"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"/>    <LinearLayout        android:layout_gravity="center_horizontal"        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:text="开始"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="start"/>        <Button            android:text="停止"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="stop"/>    </LinearLayout></LinearLayout>

动画main.xml

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 添加多个帧 -->    <item        android:drawable="@drawable/horse1"        android:duration="60"/>    <item        android:drawable="@drawable/horse2"        android:duration="60"/>    <item        android:drawable="@drawable/horse3"        android:duration="60"/>    <item        android:drawable="@drawable/horse4"        android:duration="60"/>    <item        android:drawable="@drawable/horse5"        android:duration="60"/>    <item        android:drawable="@drawable/horse6"        android:duration="60"/>    <item        android:drawable="@drawable/horse7"        android:duration="60"/>    <item        android:drawable="@drawable/horse8"        android:duration="60"/></animation-list>

MainActivity:

import android.os.Bundle;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.view.View;import android.widget.ImageView;public class MainActivity extends Activity {    AnimationDrawable ad;    ImageView iv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        iv=(ImageView) findViewById(R.id.iv_imageview);        ad =(AnimationDrawable) iv.getBackground();    }    public void start(View v)    {        //开始        ad.start();    }    public void stop(View v)    {        //停止        ad.stop();    }}
0 0