展会信息港展会大全

Android ApiDemos示例解析(3): App->Activity->Animation
来源:互联网   发布日期:2016-01-14 10:51:41   浏览:1228次  

导读:App->Activity->Animation 示例用于演示不同Activity切换时动态效果。例子中定义了两种动画效果,渐变Fade In, 新出现的Activity由浅入深逐渐显示,放大...

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

[html]

<alpha xmlns:android=”http://schemas.android.com/apk/res/android”

android:interpolator=”@android:anim/accelerate_interpolator”

android:fromAlpha=”0.0″ android:toAlpha=”1.0″

android:duration=”@android:integer/config_longAnimTime” />

<alpha xmlns:android=”http://schemas.android.com/apk/res/android”

android:interpolator=”@android:anim/accelerate_interpolator”

android:fromAlpha=”0.0″ android:toAlpha=”1.0″

android:duration=”@android:integer/config_longAnimTime” />

zoom_center.xml

[html]

<set xmlns:android=”http://schemas.android.com/apk/res/android”

android:interpolator=”@android:anim/decelerate_interpolator”>

<scale android:fromXScale=”2.0″ android:toXScale=”1.0″

android:fromYScale=”2.0″ android:toYScale=”1.0″

android:pivotX=”50%p” android:pivotY=”50%p”

android:duration=”@android:integer/config_mediumAnimTime” />

</set>

<set xmlns:android=”http://schemas.android.com/apk/res/android”

android:interpolator=”@android:anim/decelerate_interpolator”>

<scale android:fromXScale=”2.0″ android:toXScale=”1.0″

android:fromYScale=”2.0″ android:toYScale=”1.0″

android:pivotX=”50%p” android:pivotY=”50%p”

android:duration=”@android:integer/config_mediumAnimTime” />

</set>

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

[html]

<?xml version=”1.0″ encoding=”utf-8″?>

<set xmlns:android=”http://schemas.android.com/apk/res/android”

android:interpolator=”@[package:]anim/interpolator_resource”

android:shareInterpolator=[ ” true ” false “>

<alpha

android:fromAlpha=”float”

android:toAlpha=”float” />

<scale

android:fromXScale=”float”

android:toXScale=”float”

android:fromYScale=”float”

android:toYScale=”float”

android:pivotX=”float”

android:pivotY=”float” />

<translate

android:fromXDelta=”float”

android:toXDelta=”float”

android:fromYDelta=”float”

android:toYDelta=”float” />

<rotate

android:fromDegrees=”float”

android:toDegrees=”float”

android:pivotX=”float”

android:pivotY=”float” />

<set> …

</set>

</set>

<?xml version=”1.0″ encoding=”utf-8″?>

<set xmlns:android=”http://schemas.android.com/apk/res/android”

android:interpolator=”@[package:]anim/interpolator_resource”

android:shareInterpolator=[ ” true ” false “>

<alpha

android:fromAlpha=”float”

android:toAlpha=”float” />

<scale

android:fromXScale=”float”

android:toXScale=”float”

android:fromYScale=”float”

android:toYScale=”float”

android:pivotX=”float”

android:pivotY=”float” />

<translate

android:fromXDelta=”float”

android:toXDelta=”float”

android:fromYDelta=”float”

android:toYDelta=”float” />

<rotate

android:fromDegrees=”float”

android:toDegrees=”float”

android:pivotX=”float”

android:pivotY=”float” />

<set> …

</set>

</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按钮的事件处理代码:

[java]

private OnClickListener mFadeListener = new OnClickListener() {

public void onClick(View v) {

// Request the next activity transition (here starting a new one).

startActivity(new Intent(Animation.this, Controls1.class));

// Supply a custom animation.This one will just fade the new

// activity on top.Note that we need to also supply an animation

// (here just doing nothing for the same amount of time) for the

// old activity to prevent it from going away too soon.

overridePendingTransition(R.anim.fade, R.anim.hold);

}

};</p>

<p>private OnClickListener mZoomListener = new OnClickListener() {

public void onClick(View v) {

// Request the next activity transition (here starting a new one).

startActivity(new Intent(Animation.this, Controls1.class));

// This is a more complicated animation, involving transformations

// on both this (exit) and the new (enter) activity.Note how for

// the duration of the animation we force the exiting activity

// to be Z-ordered on top (even though it really isn't) to achieve

// the effect we want.

overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);

}

};

private OnClickListener mFadeListener = new OnClickListener() {

public void onClick(View v) {

// Request the next activity transition (here starting a new one).

startActivity(new Intent(Animation.this, Controls1.class));

// Supply a custom animation.This one will just fade the new

// activity on top.Note that we need to also supply an animation

// (here just doing nothing for the same amount of time) for the

// old activity to prevent it from going away too soon.

overridePendingTransition(R.anim.fade, R.anim.hold);

}

};</p>

<p>private OnClickListener mZoomListener = new OnClickListener() {

public void onClick(View v) {

// Request the next activity transition (here starting a new one).

startActivity(new Intent(Animation.this, Controls1.class));

// This is a more complicated animation, involving transformations

// on both this (exit) and the new (enter) activity.Note how for

// the duration of the animation we force the exiting activity

// to be Z-ordered on top (even though it really isn't) to achieve

// the effect we want.

overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);

}

};

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

作者:mapdigit

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港