Android ApiDemos樣本解析(3): App->Activity->Animat

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.