一、Animations介绍

Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。

二、Animations的分类

Animations从总体上可以分为两大类:

1.Tweened Animations:该类Animations提供了旋转、移动、伸展和淡出等效果。Alpha——淡入淡出,Scale——缩放效果,Rotate——旋转,Translate——移动效果。

2.Frame-by-frame Animations:这一类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示。

三、Animations的使用方法(代码中使用)

对象之间的关系:

使用TweenedAnimations的步骤:

1.创建一个AnimationSet对象(Animation子类);

2.增加需要创建相应的Animation对象;

3.更加项目的需求,为Animation对象设置相应的数据;

4.将Animatin对象添加到AnimationSet对象当中;

5.使用控件对象开始执行AnimationSet。

四、具体实现

1、main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<LinearLayout

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<Button

android:id="@+id/rotateButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="旋转" />

<Button

android:id="@+id/scaleButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="缩放" />

<Button

android:id="@+id/alphaButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="淡入淡出" />

<Button

android:id="@+id/translateButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="移动" />

</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<ImageView

android:id="@+id/image"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:src="@drawable/an" />

</LinearLayout>

</LinearLayout>

2、.java文件

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

import android.view.animation.AlphaAnimation;

import android.view.animation.Animation;

importandroid.view.animation.AnimationSet;

importandroid.view.animation.RotateAnimation;

importandroid.view.animation.ScaleAnimation;

import android.view.animation.TranslateAnimation;

importandroid.widget.Button;

importandroid.widget.ImageView;

public class Animation1Activity extends Activity {

private Button rotateButton = null;

private Button scaleButton = null;

private Button alphaButton = null;

private Button translateButton = null;

private ImageView image = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

rotateButton = (Button)findViewById(R.id.rotateButton);

scaleButton = (Button)findViewById(R.id.scaleButton);

alphaButton = (Button)findViewById(R.id.alphaButton);

translateButton = (Button)findViewById(R.id.translateButton);

image = (ImageView)findViewById(R.id.image);

rotateButton.setOnClickListener(newRotateButtonListener());

scaleButton.setOnClickListener(newScaleButtonListener());

alphaButton.setOnClickListener(newAlphaButtonListener());

translateButton.setOnClickListener(

new TranslateButtonListener());

}

class AlphaButtonListener implementsOnClickListener{

public void onClick(View v) {

//创建一个AnimationSet对象,参数为Boolean型,

           //true表示使用Animationinterpolatorfalse则是使用自己的

AnimationSet animationSet = new AnimationSet(true);

//创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明

AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);

//设置动画执行的时间

alphaAnimation.setDuration(500);

//alphaAnimation对象添加到AnimationSet当中

animationSet.addAnimation(alphaAnimation);

//使用ImageViewstartAnimation方法执行动画

image.startAnimation(animationSet);

}

}

class RotateButtonListener implementsOnClickListener{

public void onClick(View v) {

AnimationSet animationSet = new AnimationSet(true);

//参数1:从哪个旋转角度开始

           //参数2:转到什么角度

           //4个参数用于设置围绕着旋转的圆的圆心在哪里

           //参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标

           //参数4x轴的值,0.5f表明是以自身这个控件的一半长度为x

           //参数5:确定y轴坐标的类型

           //参数6y轴的值,0.5f表明是以自身这个控件的一半长度为x

RotateAnimation rotateAnimation = new RotateAnimation(0, 360,

Animation.RELATIVE_TO_SELF,0.5f,

Animation.RELATIVE_TO_SELF,0.5f);

rotateAnimation.setDuration(1000);

animationSet.addAnimation(rotateAnimation);

image.startAnimation(animationSet);

}

}

class ScaleButtonListener implementsOnClickListener{

public void onClick(View v) {

AnimationSet animationSet = new AnimationSet(true);

//参数1x轴的初始值

           //参数2x轴收缩后的值

           //参数3y轴的初始值

           //参数4y轴收缩后的值

           //参数5:确定x轴坐标的类型

           //参数6x轴的值,0.5f表明是以自身这个控件的一半长度为x

           //参数7:确定y轴坐标的类型

           //参数8y轴的值,0.5f表明是以自身这个控件的一半长度为x

ScaleAnimation scaleAnimation = new ScaleAnimation(

0, 0.1f,0,0.1f,

Animation.RELATIVE_TO_SELF,0.5f,

Animation.RELATIVE_TO_SELF,0.5f);

scaleAnimation.setDuration(1000);

animationSet.addAnimation(scaleAnimation);

image.startAnimation(animationSet);

}

}

class TranslateButtonListener implementsOnClickListener{

public void onClick(View v) {

AnimationSet animationSet = new AnimationSet(true);

//参数12x轴的开始位置

           //参数34y轴的开始位置

           //参数56x轴的结束位置

           //参数78x轴的结束位置

TranslateAnimation translateAnimation =

new TranslateAnimation(

Animation.RELATIVE_TO_SELF,0f,

Animation.RELATIVE_TO_SELF,0.5f,

Animation.RELATIVE_TO_SELF,0f,

Animation.RELATIVE_TO_SELF,0.5f);

translateAnimation.setDuration(1000);

animationSet.addAnimation(translateAnimation);

image.startAnimation(animationSet);

}

}

}

五、Tween Animations的通用方法

转自:链接

Android学习二_八:Animation的使用(一) (转)的更多相关文章

  1. 【转】 Pro Android学习笔记(八二):了解Package(1):包和进程

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在之前,我们已经学习了如何签发apk,见P ...

  2. Android进阶(二十八)上下文菜单ContextMenu使用案例

    上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...

  3. 【转】 Pro Android学习笔记(八八):了解Handler(2):什么是Handler

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 之前我们有一篇很好的博文<Andro ...

  4. 【转】 Pro Android学习笔记(八九):了解Handler(3):延迟执行小例子

    目录(?)[-] 小例子 Handler的处理 Activity的代码片段 后台线程和UI的互动 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://b ...

  5. 【转】 Pro Android学习笔记(八六):了解Package(5):使用lib

    目录(?)[-] 在项目中使用lib 源代码 了解一些机制 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowin ...

  6. 【转】 Pro Android学习笔记(八四):了解Package(3):包间数据共享

    目录(?)[-] 共享User ID的设置 共享资源例子 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowing ...

  7. 【转】 Pro Android学习笔记(八五):了解Package(4):lib项目

    目录(?)[-] 什么是lib项目 小例子 Lib的实现 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowing ...

  8. 【转】 Pro Android学习笔记(八十):服务(5):访问远程服务

    目录(?)[-] Client的AIDL文件 Client的代码 建立连接 请求服务 断开连接 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://bl ...

  9. Java开发学习(二十八)----拦截器(Interceptor)详细解析

    一.拦截器概念 讲解拦截器的概念之前,我们先看一张图: (1)浏览器发送一个请求会先到Tomcat的web服务器 (2)Tomcat服务器接收到请求以后,会去判断请求的是静态资源还是动态资源 (3)如 ...

随机推荐

  1. JavaScript Window对象

    1.Window对象的location属性引用的是Location对象,它表示该窗口中当前显示的文档的URL,并定义了方法来使窗口载入新的文档.Location对象的href属性是一个字符串,后者包含 ...

  2. FREE 开源 API 管理工具等

    最近学习API 管理工具,发现几个不错的东西,记录如下: 1.IBM 收购NODE 厂家  STRONGLOOP 有一产品LOOPBACK,开源,好! 2.apigee  api管理平台 也不错. 3 ...

  3. ios 在中国地区,24小时时间格式 系统设定下 获得12小时制时间的方法

    如题,在中国地区,24小时时间格式 系统设定下,如果单单使用 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 之后,无论用hh ...

  4. nginx和apache的一些比较

    1.两者所用的驱动模式不同. nginx使用的是epoll的非阻塞模式事件驱动. apache使用的是select的阻塞模式事件驱动. 2.fastcgi和cgi的区别 当用户请求web服务的时候,w ...

  5. 针对跑MySQL的Linux优化【转】

    本文来自:http://www.mysqlsupport.cn/linux-performance-tuning-tips-mysql/ 因为很多MySQL的生产环境都在Linux下,我决定指出一些L ...

  6. 如何让ListView的item不可点击

    原文链接:http://blog.csdn.net/zhangfei_jiayou/article/details/6972752 1. 如果是listView的id是使用系统默认的id,如下, 则可 ...

  7. BestCoder27 1001.Jump and Jump... (hdu 5162) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5162 题目意思:有 n 个 kid,每个 kid 有三个成绩 a, b, c.选最大的一个成绩作为这个 ...

  8. 【hadoop2.6.0】数据丢失问题解决

    想自己走一遍从代码到运行的流程,结果各种错,郁闷啊. 问题① http://localhost:50070/ 里面一进去就告诉我块丢了.... 解决: bin/hadoop fsck -delete ...

  9. python基础——map/reduce

    python基础——map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Pro ...

  10. 兼容iOS 10:配置获取隐私数据权限声明

    原文链接 iOS 10的一大变化是更强的隐私数据保护.在文档中是这么描述的: You must statically declare your app’s intended use of protec ...