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. “too many open files" ----增大打开的文件数

     http://www.cnblogs.com/ibook360/archive/2012/05/11/2495405.html [root@localhost ~]# ab -n -c http:/ ...

  2. 关于cocostudio加载UI json CCUIHELPER未声明问题

    查看官方的文档,在文档的最后添加了如何加载项目.如下代码: UILayer* ul =UILayer::create(); ul->addWidget(CCUIHELPER->create ...

  3. MVC模式下的数据展示:EasyUI的datagrid

    我的数据库设计是一张老师表teacher,一张学生表student,一个教师对应多个学生,在学生一方建立外键; 还有一点想清楚,需要展示的数据是根据什么来的,是成功登陆的用户的id?还是直接展示所有的 ...

  4. HDU 5037 Frog(贪心)

    题意比较难懂,一只青蛙过河,它最多一次跳L米,现在河中有石头,距离不等,上帝可以往里加石头,青蛙非常聪明,它一定会选择跳的次数最少的路径.问怎么添加石头能让青蛙最多的次数.输出青蛙跳的最多的次数. 考 ...

  5. php创建读取 word.doc文档

    创建文档; <?php $html = "this is question"; for($i=1;$i<=3;$i++){ $word = new word(); $w ...

  6. HTML5 FileAPI读取实例---(一)

    在HTML5中,提供了一个关于文件操作的API,通过这个API,对于从web页面上访问本地文件系统的相关处理变得十分简单.到目前为止只有部分浏览器对它提供支持. 1.FileList对象和File对象 ...

  7. asp.net中Page.ClientScript.RegisterStartupScript用法小结(转)

    //ASP.NET后台页面跳转 Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<scri ...

  8. MemCachedClient数据写入的三个方法

    set方法 1 将数据保存到cache服务器,如果保存成功则返回true 2 如果cache服务器存在同样的key,则替换之 3 set有5个重载方法,key和value是必须的参数,还有过期时间,h ...

  9. 局部线性嵌入(LLE)原理总结

    局部线性嵌入(Locally Linear Embedding,以下简称LLE)也是非常重要的降维方法.和传统的PCA,LDA等关注样本方差的降维方法相比,LLE关注于降维时保持样本局部的线性特征,由 ...

  10. Hibernate 性能优化之懒加载

    针对数据库中的大数据,不希望特别早的加载到内存中,当用到它的时候才加载 懒加载分为:类的懒加载.集合的懒加载.单端关联的懒加载 类的懒加载    1.在默认情况下,类就是执行懒加载        2. ...