AnimationDrawable代表一个动画,Android 既支持传统的逐帧动画(类 似于电影方式,一张图片、一张图片地切换),也支持通过平移、变换计算出来的补间动画。
下面以补间动画为例来介绍如何定义 AnimationDrawable 资源,定义补间动画的 XML 资 源文件以<set.../>元素作为根元素,该元素内可以指定如下 4 个元素:
  • alpha:设置透明度的改变。
  • scale:设置图片进行缩放改变。
  • translate:设置图片进行位移变换。
  • rotate:设置图片进行旋转。
定义动画的 XML 资源应该放在/res/anmi 路径下,当使用 ADT 创建一个 Android 应用时, 默认不会包含该路径,开发者需要自行创建该路径。
 
定义补间动画的思路很简单:设置一张图片的开始状态(包括透明度、位置、缩放比、 旋转度)、并设置该图片的结束状态(包括透明度、位置、缩放比、旋转度),再设置动画的 持续时间,Android 系统会使用动画效果把这张图片从开始状态变换到结束状态。
 
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/java"
/>
<Button
android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始动画"
/>
</LinearLayout>
my_anim.xml
图片将会变宽为原来的1.4倍,同时高度变为原来的0.6倍,向上向右移动。
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:duration="5000">
<!-- 定义缩放变换 -->
<scale android:fromXScale="1.0"
android:toXScale="1.4"
android:fromYScale="1.0"
android:toYScale="0.6"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:duration="2000"
/>
<!-- 定义位移变换 -->
<translate android:fromXDelta="10"
android:toXDelta="130"
android:fromYDelta="30"
android:toYDelta="-80"
android:duration="2000"
/>
</set>
AnimationDrawable.java
package org.crazyit.res;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView; /**
* Description:
* <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class AnimationDrawable extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView image = (ImageView)findViewById(R.id.image);
// 加载动画资源
final Animation anim = AnimationUtils.loadAnimation(this,
R.anim.my_anim);
// 设置动画结束后保留结束状态
anim.setFillAfter(true);
Button bn = (Button) findViewById(R.id.bn);
bn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 开始动画
image.startAnimation(anim);
}
});
}
}
 
 
 
 
 
 
 
 

AnimationDrawable 资源的更多相关文章

  1. android学习笔记35——AnimationDrawable资源

    AnimationDrawable资源 AnimationDrawable,代表一个动画. android既支持传统的逐帧动画(类似于电影方式,一张图片一张图片的切换),也支持通过平移.变换计算出来的 ...

  2. 使用(Drawable)资源———AnimationDrawable资源

    AnimationDrawable代表一个动画. 下面以补间动画为例来介绍如何定义AnimationDrawable资源,定义补间动画的XML资源文件已<set.../>元素作为根元素,该 ...

  3. Android中的Drawable资源

    在Android应用中,常常会用到Drawable资源,比如图片资源等,在Android开发中我们是用Drawable类来Drawable类型资源的. Drawable资源一般存储在应用程序目录的\r ...

  4. Android学习15--使用(Drawable)资源

    1.图片资源 图片资源是最简单的Drawable资源.仅仅要把*.png.*.jpg*..gif等格式的图片放入/res/drawable-XXX文件夹下,Android SDK就会在编译应用自己主动 ...

  5. android中常见的Drawable资源有哪些?

    Drawable资源是安卓应用中最常见的一种资源,比如图片等,因此,对于初学者而言,必须掌握drawable资源相关应用. 今天在网上刚好看到了一篇介绍android Drawable资源的文章,分享 ...

  6. Android应用资源

    Java刚開始学习的人直接在Java源代码使用"hello" 和123 类型的字符串和整型.但时间长了就会忘记当初定义的原因,有经验的或许会定义字符串常量ResultSet.TYP ...

  7. android应用的资源

    应用资源可以分为两大类: 1.无法直接访问的原生资源,保存在asset目录下. 2.可以通过R资源清单类访问的资源,保存在res目录下. 资源的类型以及存储方式: android要求在res目录下用不 ...

  8. 疯狂Android讲义 - 学习笔记(五)

    第五章 Android使用统一的Intent对象来封装“启动意图”,不管是启动Activity.Service组件.或者BroadcastReceiver等,提供了一致的编程模型.Intent设计有点 ...

  9. Android开发学习清单

    目录: 第1章 Android应用与开发环境1.1 Android的发展和历史1.1.1 Android的发展和简介1.1.2 Android平台架构及特性1.2 搭建Android开发环境1.2.1 ...

随机推荐

  1. Android中的RelativeLayout

    安卓布局之一,RelativeLayout.又称之为相对布局.对于一个界面每个人都有不同的实现.我比较喜欢使用RelativeLayou.原因是,相对布局不会出现过多的嵌套,在现在硬件不断发展的今天, ...

  2. motan源码分析十:流量切换

    motan提供了流量切换的功能,可以实现把一个group的流量切换到另一个group(一个或多个服务都可以).大家可以使用tomcat部署motan的管理工具,并设置几个组,例如可以参考demo代码: ...

  3. Deme_遥感控制物体移动(涉及遮罩,小摄像机跟随)

    using UnityEngine; using System.Collections; using UnityEngine.EventSystems; public class JoyControl ...

  4. [转] JavaScript中的字符串操作

    一.概述    字符串在JavaScript中几乎无处不在,在你处理用户的输入数据的时候,在读取或设置DOM对象的属性时,在操作cookie时,当然还有更 多....JavaScript的核心部分提供 ...

  5. codevs2622数字序列( 连续子序列最大和O(n)算法)

    /* 算法描述:维护一个s[p]表示累加和 并且更新最大值ans 如果s[p]<0 则从p+1重新累加 证明:设某个区间的起点和终点分别为s t 分两种情况 1.t<p:设s2表示1到s的 ...

  6. AndroidStudio SDK版本下载

    错误描述: pkg: /data/local/tmp/com.example.myapplication Failure [INSTALL_FAILED_OLDER_SDK] 出现这个错误,研究了半天 ...

  7. Datables wrning(table id='example'):Cannot reinitialise DataTable.

    出现的问题如下所示: Datables wrning(table id='example' Datables object for this table,please pass eithser no ...

  8. MySQL 序列使用

    MySQL 序列使用 MySQL序列是一组整数:1, 2, 3, ...,由于一张数据表只能有一个字段自增主键, 如果你想实现其他字段也实现自动增加,就可以使用MySQL序列来实现. 本章我们将介绍如 ...

  9. SQL window身份登陆 SQL server不能登陆

    用window方式登陆然后,在SQL Server Management Studio 中新建查询,执行下面代码一. ALTER LOGIN sa ENABLE GO ALTER LOGIN sa W ...

  10. [转]mysql 的日志的启动与查看

    mysql有以下几种日志:错误日志:  -log-err查询日志:  -log慢查询日志: -log-slow-queries更新日志:    -log-update二进制日志:-log-bin 日志 ...