zoukankan      html  css  js  c++  java
  • Android动画--逐帧动画(drawable animation)

    逐帧动画也叫darwable animation,是顺序播放一组事先定义好的动画,类似于播放电影。它利用的是眼睛的视觉暂留效应。有两种方式定义逐帧动画,分别是采用xml资源文件和代码实现。

    一、xml资源文件的方式:

    (1)首先在drawable中存储将要播放的图片

    (2)在drawable中新建一个xml文件,在这个文件中使用<animation-list>标签来定义动画帧序列。

    格式如下:

    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"

        android:oneshot="false">

        <item android:drawable="@drawable/hw_device_guid_omron9200_img_1"

            android:duration="500"/>

        <item android:drawable="@drawable/hw_device_guid_omron9200_img_2"

            android:duration="500"/>

        <item android:drawable="@drawable/hw_device_guid_omron9200_img_3"

            android:duration="500"/>

        <item android:drawable="@drawable/hw_device_guid_omron9200_img_4"

            android:duration="500"/>

        <item android:drawable="@drawable/hw_device_kangkang_ccpba01_measure_guide_1"

            android:duration="500"/>

        <item android:drawable="@drawable/hw_device_kangkang_ccpba01_measure_guide_2"

            android:duration="500"/>

        <item android:drawable="@drawable/hw_device_kangkang_ccpba01_measure_guide_3"

            android:duration="500"/>

        <item android:drawable="@drawable/hw_device_kangkang_ccpba01_measure_guide_4"

            android:duration="500"/>

    </animation-list>

    其中android:oneshot 用来控制动画是否循环播放,false表示动画循环播放,true代表动画不循环播放

    android:duration用来设置每一帧动画播放持续的时间

    (3)在代码中使用

    当我们在xml中设置完成,然后在需要展示帧动画的地方调用。

    代码格式如下:

    imageView.setBackgroundResource(R.drawable.drawable_anim);

           AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();

           animationDrawable.start();

    二、代码实现

    代码实现指的是不适用资源文件,在代码中定义实现。

    在代码中定义animationdrawable对象,并将其设置给对应的view

    代码实现如下:

    AnimationDrawable animationDrawable = new AnimationDrawable();

            for (int i = 1; i <= 4; i++) {

                int id = getResources().getIdentifier("hw_device_guid_omron9200_img_"+i,"drawable",getPackageName());

                Drawable drawable = getDrawable(id);

                animationDrawable.addFrame(drawable,1000);

            }

            imageView.setBackground(animationDrawable);

            animationDrawable.setOneShot(false);

            AnimationDrawable animationDrawableFromBG = (AnimationDrawable) imageView.getDrawable();

            animationDrawable.start();

  • 相关阅读:
    iOS真机调试 for Xcode 5
    iOS/iphone开发如何为苹果开发者帐号APPID续费
    unity3d中布娃娃系统
    U3D实现与iOS交互
    两种方法连接MySql数据库
    Unity3D Gamecenter 得分上传失败的处理
    Unity3.5 GameCenter基础教程(转载)
    判断数字正则表达式
    (转)SQL server 2005查询数据库表的数量和表的数据量
    C#操作PowerDesigner代码
  • 原文地址:https://www.cnblogs.com/YangMT/p/9452612.html
Copyright © 2011-2022 走看看