Android ApiDemo示例解读3:App->Activity->Animation

移动开发 Android
SDK中的示例程序App->Activity->Animation演示了切换Activity时的动画效果。提供了两种动画效果,一种是Fade In渐变,后出现的Activity由浅入深逐渐显示;另一种是Zoom放大效果,后出现的Activity由小及大逐渐显示。

SDK中的示例程序App->Activity->Animation演示了切换Activity时的动画效果。提供了两种动画效果,一种是Fade In渐变,后出现的Activity由浅入深逐渐显示;另一种是Zoom放大效果,后出现的Activity由小及大逐渐显示。

Android 中 Animation 资源可以分为两种:

Tween Animation 对单个图像进行各种变换(缩放,平移,旋转等)来实现动画。

Frame Animation 由一组图像顺序显示显示动画。

Animation 中使用的是Tween Animation,使用的资源为R.anim.fade、R.anim.hold、R.anim.zoom_enter、R.anim.zoom_exit。

其中R.anim.fade、R.anim.zoom_enter分别为Fade In 和 Zoom动画资源。其定义为:

fade.xml

  1. <alpha xmlns:android=”http://schemas.android.com/apk/res/android”    
  2. android:interpolator=”@android:anim/accelerate_interpolator”    
  3. android:fromAlpha=”0.0″ android:toAlpha=”1.0″    
  4. android:duration=”@android:integer/config_longAnimTime” />   

zoom_center.xml

  1. <set xmlns:android=”http://schemas.android.com/apk/res/android”    
  2. android:interpolator=”@android:anim/decelerate_interpolator”>   
  3. <scale android:fromXScale=”2.0″ android:toXScale=”1.0″    
  4.  android:fromYScale=”2.0″ android:toYScale=”1.0″    
  5.  android:pivotX=”50%p” android:pivotY=”50%p”    
  6.  android:duration=”@android:integer/config_mediumAnimTime” />   
  7. </set>   

tween animation 资源定义的格式如下:

  1. <?xml version=”1.0″ encoding=”utf-8″?>   
  2.  <set xmlns:android=”http://schemas.android.com/apk/res/android”    
  3.  android:interpolator=”@[package:]anim/interpolator_resource”    
  4.  android:shareInterpolator=[ ” true ” false “>   
  5.  <alpha   
  6.  android:fromAlpha=”float”    
  7.  android:toAlpha=”float” />   
  8.  <scale   
  9.  android:fromXScale=”float”    
  10.  android:toXScale=”float”    
  11.  android:fromYScale=”float”    
  12.  android:toYScale=”float”    
  13.  android:pivotX=”float”    
  14.  android:pivotY=”float” />   
  15.  <translate   
  16.  android:fromXDelta=”float”    
  17.  android:toXDelta=”float”    
  18.  android:fromYDelta=”float”    
  19.  android:toYDelta=”float” />   
  20.  <rotate   
  21.  android:fromDegrees=”float”    
  22.  android:toDegrees=”float”    
  23.  android:pivotX=”float”    
  24.  android:pivotY=”float” />   
  25.  <set> …    
  26.  </set>   
  27.  </set>   

<set> 为其它animation类型<alpha>,<scale>,<translate>和<rotate>或其它<set>的容器。

android:interpolator 为Interpolator资源ID,Interpolator定义了动画的变化速率,动画的各帧的显示可以加速,减速,重复显示。

android:shareInterpolator 如果想为<set>中的各个子动画定义共享interpolator,shareInterpolator 则设为true。

<alpha> 定义Fade in 、Fade out 动画,其对应的Android类AlphaAnimation,参数由fromAlpha,toAlpha定义。

<scale>定义缩放动画,其对应的Android类为ScaleAnimation,参数由fromXScale、toXScale、 fromYScale、toYScale、pivotX、pivotY定义,pivotX、pivotY定义了缩放时的中心。

<translate>定义平移动画,其对应的Android类为TranslateAnimation,参数由fromXDelta、toXDelta、fromYDelta、toYDelta定义。

<rotate>定义选择动画,其对应的Android类RotateAnimation,参数由fromDegrees、toDegrees、pivotX、pivotY, pivotX、pivotY定义选择中心。

Animation中的Fade In和Zoom In按钮的事件处理代码:

  1. private OnClickListener mFadeListener = new OnClickListener() {    
  2.  public void onClick(View v) {    
  3.  // Request the next activity transition (here starting a new one).    
  4.  startActivity(new Intent(Animation.this, Controls1.class));    
  5.  // Supply a custom animation.  This one will just fade the new    
  6.  // activity on top.  Note that we need to also supply an animation    
  7.  // (here just doing nothing for the same amount of time) for the    
  8.  // old activity to prevent it from going away too soon.    
  9.  overridePendingTransition(R.anim.fade, R.anim.hold);    
  10.  }    
  11. };   
  12. private OnClickListener mZoomListener = new OnClickListener() {    
  13.  public void onClick(View v) {    
  14.  // Request the next activity transition (here starting a new one).    
  15.  startActivity(new Intent(Animation.this, Controls1.class));    
  16.  // This is a more complicated animation, involving transformations    
  17.  // on both this (exit) and the new (enter) activity.  Note how for    
  18.  // the duration of the animation we force the exiting activity    
  19.  // to be Z-ordered on top (even though it really isn't) to achieve    
  20.  // the effect we want.    
  21.  overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);    
  22.  }    
  23. };   

从代码可以看到Activity Animation到其它Activity Controls1 切换的动画使用overridePendingTransition 来定义,函数overridePendingTransition(int enterAnim, int exitAnim) 必须定义在StartActivity(Intent) 或是 Activity.finish()之后来定义两个Activity切换时的动画,enterAnim 为新Activity出现时动画效果,exitAnim则定义了当前Activity退出时动画效果。

责任编辑:闫佳明 来源: jizhuomi
相关推荐

2013-12-19 14:28:04

Android ApiAndroid开发Android SDK

2013-12-19 14:32:31

Android ApiAndroid开发Android SDK

2013-12-19 14:36:43

Android ApiAndroid开发Android SDK

2013-12-19 14:34:52

Android ApiAndroid开发Android SDK

2013-12-19 14:16:46

Android ApiAndroid开发Android SDK

2013-12-19 14:13:16

Android ApiAndroid开发Android SDK

2013-12-19 13:40:44

Android ApiAndroid开发Android SDK

2013-12-19 13:51:12

Android ApiAndroid开发Android SDK

2013-12-19 16:26:29

Android ApiAndroid开发Android SDK

2010-01-28 13:12:47

Android使用An

2010-02-02 14:22:50

Python示例

2014-05-27 14:16:08

AndroidActivitysingleTask

2019-07-19 10:44:34

移动应用APP

2010-02-01 11:22:09

C++虚函数

2012-02-17 17:07:30

Android安全Activity劫持

2015-10-20 15:54:16

android源码滑动关闭

2010-03-05 10:47:05

Python futu

2010-03-02 14:41:00

WCF行为控制

2013-01-08 13:33:07

Android开发Activity入门指南

2014-03-06 13:26:49

动画资源Animation R
点赞
收藏

51CTO技术栈公众号