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

  • 相关阅读:
    httpcontext in asp.net unit test
    initialize or clean up your unittest within .net unit test
    Load a script file in sencha, supports both asynchronous and synchronous approaches
    classes system in sencha touch
    ASP.NET MVC got 405 error on HTTP DELETE request
    how to run demo city bars using sencha architect
    sencha touch mvc
    sencha touch json store
    sencha touch jsonp
    51Nod 1344:走格子(贪心)
  • 原文地址:https://www.cnblogs.com/YangMT/p/9452612.html
Copyright © 2011-2022 走看看