2011.10.28注:如果需要控件停在動畫后的位置,需要設置android:fillAfter屬性為true,在set節點中。默認在動畫結束后回到動畫前位置。設置android:fillAfter后,我們看到了控件留在了動畫后的位置,其實也只是看到在那個位置,真實位置還是在原來動畫前那里,你會發現Button不能被點擊,就是這個原因。所以我們可以在動畫結束后,手動把控件移動到動畫結束后的位置。這就需要根結點為AbsoluteLayout,因為LinearLayout不能通過x,y座標定位。具體方法:把布局換成AbsoluteLayout,使用Animation的setAnimationListener設置動畫播放事件,在onAnimationEnd方法中,使用控件的setLayoutParams方法,設置動畫后的位置。
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:
<?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:
<?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 淡出效果
<?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,也就沒有動畫效果。