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

  • 相关阅读:
    .net Core 配置Centos守护进程Supervisor
    .net Core 安装在linux上
    Mariadb Galera Cluster 搭建集群
    MariaDB 安装配置记录
    MariaDB 安装
    wso2 linux上部署说明
    RestFul 架构详解
    SpirngBoot之整合邮件服务
    SpringBoot之模板引擎
    SpringBoot之持久化框架
  • 原文地址:https://www.cnblogs.com/YangMT/p/9452612.html
Copyright © 2011-2022 走看看