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

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

逐帧动画是一种常见的动画形式(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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有头发的猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值