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