Android图形图像之逐帧动画

前言


    逐帧动画最初我是在Flash上面接触到的。它是通过在很短的时间内,快速的播放多张连续的图片,从而给人一种动画的感觉,其实,使用摄像器材也是如此,摄像器材通过在每秒,甚至每毫秒的快速采样,实质上就是拍照片,在播放视频的时候,屏幕不停的绘制采样的照片,由于播放的速度超过了我们肉眼的反应时间。我们就感觉动了起来。

演示



这里写图片描述

代码实现


    逐帧动画首先需要定义好XML资源文件,同时设置duration属性,控制每一帧的持续时间。
    图案我是使用PhotoShop制作的,你也可以使用你喜欢的图片。
逐帧动画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/ni"
        android:duration="500"/>
    <item
        android:drawable="@drawable/hao"
        android:duration="500"/>
    <item
        android:drawable="@drawable/huan"
        android:duration="500"/>
    <item
        android:drawable="@drawable/yin"
        android:duration="500"/>
    <item
        android:drawable="@drawable/lai"
        android:duration="500"/>
    <item
        android:drawable="@drawable/dao"
        android:duration="500"/>
    <item
        android:drawable="@drawable/wo"
        android:duration="500"/>
    <item
        android:drawable="@drawable/de"
        android:duration="500"/>
    <item
        android:drawable="@drawable/bo"
        android:duration="500"/>
    <item
        android:drawable="@drawable/ke"
        android:duration="500"/>

</animation-list>

主界面XML

<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" >

    <ImageView
        android:id="@+id/anim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dip"
        android:background="@anim/action" />

    <ImageView
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/anim"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dip"
        android:background="@drawable/stop"
        android:clickable="true" />

</RelativeLayout>

JAVA代码

public class MainActivity extends Activity {
    private ImageView btn;
    private boolean isPlay = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView anim = (ImageView) findViewById(R.id.anim);
        final AnimationDrawable animation = (AnimationDrawable) anim
                .getBackground();
        btn = (ImageView) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {
                if (isPlay) {
                    animation.start();
                    btn.setBackground(getResources().getDrawable(
                            R.drawable.play));
                    isPlay = false;
                } else {
                    animation.stop();
                    btn.setBackground(getResources().getDrawable(
                            R.drawable.stop));
                    isPlay = true;
                }

            }
        });

    }

}

总结


    如果动画稍长,逐帧动画要求的素材太多了,如果没有专门的美工,那简直是疯了的节奏啊,还好,Android提供了一种补间动画,几张图片就能构成一个动画效果。如果喜欢,请期待我的下一篇博文。


点击下载Demo

——————————————————————————————-

欢迎转载,尊重作者劳动成果,转载请务必注明出处
技术因分享饱含生机

——————————————————————————————-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值