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();

  • 相关阅读:
    乘法九九表
    #include <time.h>
    【bzoj2060】[Usaco2010 Nov]Visiting Cows拜访奶牛 树形dp
    【codevs1380】没有上司的舞会 树形dp
    【bzoj1060】[ZJOI2007]时态同步 树形dp
    【bzoj2435】[NOI2011]道路修建 树形dp
    【bzoj3573】[HNOI2014]米特运输 树形dp
    【bzoj4027】[HEOI2015]兔子与樱花 树形dp+贪心
    【codevs1163】访问艺术馆 树形dp
    【bzoj1864】[ZJOI2006]三色二叉树 树形dp
  • 原文地址:https://www.cnblogs.com/YangMT/p/9452612.html
Copyright © 2011-2022 走看看