Android 动画之一 Drawable Animation —— 逐帧(Frame)动画

这几天打算把动画的基础都捋一下,就像我们已经知道的,动画大致有三类:(property动画,Tween动画、Frame动画),今天开始,我们就依次来看一下,他们都是怎么使用的吧^^。
今天先来看一下逐帧(Frame)动画的使用。因为它是最容易理解的动画。

Frame动画要求开发者把动画过程的每张静态图片都收集起来,然后用Andrid控制依次来显示这些静态图片,利用人眼的“视觉暂留”的原理,造成“动画”的效果,其实很简单的理解就是,逐帧动画的动画效果就和放电影的原理是完全相同的。

一、使用xml资源文件

1、逐帧动画

  即是通过播放预先排序好的图片来实现动态的画面,感觉像是放电影。

2、实现步骤:

1、
在工程里面导入要播放的图片。

2、

在工程res文件目录下新建一个anim文件夹,在里面新建一个start_animation.xml格式文件(注:从右击res新建),此文件用来定义动画播放图片的顺序及每一张图片显示停留时间。xml中在animation-list通过item 来添加每个帧。

例如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@mipmap/liminhao2" android:duration="40"></item>
    <item android:drawable="@mipmap/liminhao" android:duration="40"></item>
    <item android:drawable="@mipmap/gaoyuanyuan" android:duration="40"></item>
    <item android:drawable="@mipmap/guocaijie2" android:duration="40"></item>
    <item android:drawable="@mipmap/kero" android:duration="40"></item>

</animation-list>

注:此蓝色部分依次显示的图片,存放在drawable-mdpi文件下,一般1秒钟播放24张图片(帧)就感觉播放流畅了,即duration为40左右,默认单位为毫秒。
onShot控制动画是否循环播放,ture不循环,false循环。

3.布局文件:

布局文件中添加一ImageView控件,用来播放动画图片。将上面的anim中作为background添加到imageview中,具体布局如下:

<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"
    tools:context="com.example.day091801.FrameActivity"
    android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:id="@+id/buttonStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始播放"/>
    <Button
        android:id="@+id/buttonEnd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结束播放"/>

</LinearLayout>
    <ImageView
        android:id="@+id/imganimation"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@anim/start_animation"/>


</LinearLayout>

4、代码部分:

通过imageview的.getBackground()方法,获得背景,并通过start和stop方法开始或停止播放帧动画。

public class FrameActivity extends Activity implements View.OnClickListener{
    private Button mButtonStart;
    private Button mButtonEnd;
    private ImageView img;
    private   AnimationDrawable animationDrawable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame);
        mButtonStart = (Button) findViewById(R.id.buttonStart);
        mButtonEnd = (Button) findViewById(R.id.buttonEnd);
        img = (ImageView) findViewById(R.id.imganimation);
        mButtonStart.setOnClickListener(this);
        mButtonEnd.setOnClickListener(this);
        animationDrawable = (AnimationDrawable) img.getBackground();//获得animation,获得是Drawable,需要强制转型
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttonStart:
                animationDrawable.start();
                break;
            case R.id.buttonEnd:
                animationDrawable.stop();
                break;
            default:
                break;
        }
    }
}

注:主要功能是指定播放的资源图片。也可以在代码中使用// img.setBackgroundResource(R.anim.start_animation);

小结:这种应用在实际应用中应该不会用到,对于初学着来说,拿着玩下还是蛮有意思的,不仅增强了对Android学习的兴趣,同时也能加深对制造电影的一些了解,

效果演示:
这里写图片描述

二、完全使用java代码:

这里写图片描述

注意1中,必须先声明变量,这样在点击事件中才可以使用。
斜杠后的代码时设置其透明度。
第3步中是将设置好的animation添加到imageview中。且这个添加的位置只要在创建animation对象之后就可以,所以第2,3步是可以调换的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值