2011.10.28注:若是須要控件停在動畫後的位置,須要設置android:fillAfter屬性爲true,在set節點中。默認在動畫 結束後回到動畫前位置。設置android:fillAfter後,咱們看到了控件留在了動畫後的位置,其實也只是看到在那個位置,真實位置仍是在原來動 畫前那裏,你會發現Button不能被點擊,就是這個緣由。因此咱們能夠在動畫結束後,手動把控件移動到動畫結束後的位置。這就須要根結點爲 AbsoluteLayout,由於LinearLayout不能經過x,y座標定位。具體方法:把佈局換成AbsoluteLayout,使用 Animation的setAnimationListener設置動畫播放事件,在onAnimationEnd方法中,使用控件的 setLayoutParams方法,設置動畫後的位置。 java
5月15日注:overridePendingTransition只支持android 2.0以上版本
Android的動畫效果分爲兩種,一種是tweened animation(補間動畫),第二種是frame by frame animation。通常咱們用的是第一種。補間動畫又分爲AlphaAnimation,透明度轉換 RotateAnimation,旋轉轉換 ScaleAnimation,縮放轉換 TranslateAnimation 位置轉換(移動)。
動畫效果在anim目錄下的xml文件中定義,在程序中用AnimationUtils.loadAnimation(Context context,int ResourcesId)載入成Animation對象,在須要顯示動畫效果時,執行須要動畫的View的startAnimation方法,傳入 Animation,便可。切換Activity也能夠應用動畫效果,在startActivity方法後,執行 overridePendingTransition方法,兩個參數分別是切換前的動畫效果,切換後的動畫效果,下面的例子中傳入的是兩個alpha動 畫,以實現切換Activity時淡出淡入,漸隱漸現效果。
下面貼出代碼:
兩個Activity的佈局文件 main.xml: android
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/ll" android:background="@drawable/white" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="translate動畫效果" /> <TextView android:id="@+id/tv2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="scale動畫效果" /> <TextView android:id="@+id/tv3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="alpha動畫效果" /> <TextView android:id="@+id/tv4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="rotate動畫效果" /> <Button android:id="@+id/bt3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="動畫演示" /> <Button android:id="@+id/bt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="切換" /> </LinearLayout>
activity2.xml: app
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/ll2" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Activity2" /> <Button android:id="@+id/bt2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="返回main" /> </LinearLayout>
動畫效果XML文件,所有存放在anim目錄下:
a1.xml 淡出效果 ide
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" /> </set> <!-- fromAlpha:開始時透明度 toAlpha:結束時透明度 duration:動畫持續時間 -->a2.xml 淡入效果:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" /> </set>rotate.xml 旋轉效果:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="300" android:toDegrees="-360" android:pivotX="10%" android:pivotY="100%" android:duration="10000" /> </set> <!-- fromDegrees開始時的角度 toDegrees動畫結束時角度 pivotX,pivotY不太清楚,看效果應該是定義旋轉的圓心的 -->scale.xml 縮放效果:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:interpolator= "@android:anim/decelerate_interpolator" android:fromXScale="0.0" android:toXScale="1.5" android:fromYScale="0.0" android:toYScale="1.5" android:pivotX="50%" android:pivotY="50%" android:startOffset="0" android:duration="10000" android:repeatCount="1" android:repeatMode="reverse" /> </set> <!-- interpolator指定動畫插入器,常見的有加速減速插入器accelerate_decelerate_interpolator, 加速插入器accelerate_interpolator,減速插入器decelerate_interpolator。 fromXScale,fromYScale,動畫開始前X,Y的縮放,0.0爲不顯示,1.0爲正常大小 toXScale,toYScale,動畫最終縮放的倍數,1.0爲正常大小,大於1.0放大 pivotX,pivotY動畫起始位置,相對於屏幕的百分比,兩個都爲50%表示動畫從屏幕中間開始 startOffset,動畫屢次執行的間隔時間,若是隻執行一次,執行前會暫停這段時間,單位毫秒 duration,一次動畫效果消耗的時間,單位毫秒,值越小動畫速度越快 repeatCount,動畫重複的計數,動畫將會執行該值+1次 repeatMode,動畫重複的模式,reverse爲反向,當第偶次執行時,動畫方向會相反。restart爲從新執行,方向不變 -->translate.xml 移動效果:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="320" android:toXDelta="0" android:fromYDelta="480" android:toYDelta="0" android:duration="10000" /> </set> <!-- fromXDelta,fromYDelta起始時X,Y座標,屏幕右下角的座標是X:320,Y:480 toXDelta,toYDelta動畫結束時X,Y的座標 -->下面是程序代碼,main.java:
package com.pocketdigi.animation; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.TextView; public class main extends Activity { /** Called when the activity is first created. */ TextView tv,tv2,tv3,tv4; Button bt3; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button bt=(Button)findViewById(R.id.bt); tv=(TextView)findViewById(R.id.tv); tv2=(TextView)findViewById(R.id.tv2); tv3=(TextView)findViewById(R.id.tv3); tv4=(TextView)findViewById(R.id.tv4); bt3=(Button)findViewById(R.id.bt3); bt.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(main.this,activity2.class); startActivity(intent); overridePendingTransition(R.anim.a2,R.anim.a1); //淡出淡入動畫效果 } }); bt3.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Animation translate=AnimationUtils.loadAnimation(main.this, R.anim.translate); Animation scale=AnimationUtils.loadAnimation(main.this, R.anim.scale); Animation rotate=AnimationUtils.loadAnimation(main.this, R.anim.rotate); Animation alpha=AnimationUtils.loadAnimation(main.this, R.anim.a1); //載入XML文件成Animation對象 tv.startAnimation(translate); tv2.startAnimation(scale); tv3.startAnimation(alpha); tv4.startAnimation(rotate); //應用動畫 }}); } }activity2.java:
package com.pocketdigi.animation; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class activity2 extends Activity { Button bt2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2); bt2=(Button)findViewById(R.id.bt2); bt2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(activity2.this,main.class); startActivity(intent); overridePendingTransition(R.anim.a2,R.anim.a1); } }); } }
注:動畫切換Activity只有在新啓動Activity纔有效,若是Activity已經啓動,而且intent加了FLAG_ACTIVITY_REORDER_TO_FRONT,這樣不會新啓動Activity,也就沒有動畫效果。 佈局