zoukankan      html  css  js  c++  java
  • <Android 基础(二十五)> Frame Animation

    简介

    Frame Animation, 逐帧动画,通过定义一系列的Drawable对象来实现动画效果,可以用来作为视图的背景。
    Frame Animation在代码中体现为AnimationDrawable对象,可以通过xml文件快创建,放在在/res/drawable/目录下,设置为视图背景后,调用start()方法即可执行逐帧动画。

    XML文件

    Tags:
    < animation-list > 作为父节点,代表Animation Drawable
    < item >作为子节点,代表逐帧动画内容,一张一张图片

    Attributes:

    属性 含义
    android:oneshot=”false true”
    android:variablePadding=”false true”
    android:visible=”false true”
    android:drawable=”@drawable/xxxxx” item图片资源
    android:duration=”xxxxx” drawable播放时间,单位ms

    Res:
    /res/drawable/{folder}

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/selected"
        android:oneshot="true"
        android:variablePadding="false"
        android:visible="true">
    
        <item android:drawable="@drawable/ic_action_add" android:duration="500"/>
        <item android:drawable="@drawable/ic_action_anchor" android:duration="500"/>
        <item android:drawable="@drawable/ic_action_alarm" android:duration="500"/>
        <item android:drawable="@drawable/ic_action_amazon" android:duration="500"/>
        <item android:drawable="@drawable/ic_action_ac" android:duration="500"/>
    
    </animation-list>

    Coding

    使用XML资源

    imageView.setBackgroundResource(R.drawable.frame_anim);//设置背景
    Drawable bgDrawable = imageView.getBackground();//获取背景
    if(bgDrawable instanceof AnimationDrawable) {
        ((AnimationDrawable) bgDrawable).start();//如果为AnimationDrawable则执行动画
    }

    纯代码实现

    ***
    imageView.setBackground(createAnimationDrawable());//设置背景
    Drawable bg = imageView.getBackground();
    if(bg instanceof AnimationDrawable) {
        ((AnimationDrawable) bg).start();//开始动画
    }
    
    ***
    private AnimationDrawable createAnimationDrawable() {
    
        AnimationDrawable animationDrawable = new AnimationDrawable();
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.ic_action_add), 500);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.ic_action_anchor), 500);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.ic_action_alarm), 500);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.ic_action_amazon), 500);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.ic_action_ac), 500);
        animationDrawable.setOneShot(false);
        animationDrawable.setVisible(true,true);
        return animationDrawable;
    }

    效果图

    这里写图片描述

  • 相关阅读:
    yii框架开启事务
    CI框架--事务
    Nginx负载均衡使用ip
    如果nginx启动失败,错误解决
    nginx使用ssl模块配置支持HTTPS访问【解决ssl错误】
    Nginx反向代理+负载均衡简单实现(https方式)
    nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37
    openssl生成ssl证书
    POJ 1955 Rubik's Cube
    CF卡技术详解——笔记
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6467164.html
Copyright © 2011-2022 走看看