Android Activty界面切換動畫
在介紹切換動畫效果前,先介紹下將使用到的Android SDK提供的工具類。
AlphaAnimation:控制動畫對象的透明度,淡入淡出效果實現。
TranslateAnimation:控制動畫對象的位置,實現對象位置的移動動畫。
Animation:動畫抽象類。
AnimationUtils:提供了動畫的一些常用方法。
通過XML方式定義動畫的形式。
更多的動畫說明文檔請看:http://android.toolib.net/guide/topics/resources/animation-resource.html
一、淡入淡出方式切換
1、建立Activity淡入動畫的XML描述enter_alpha.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1.0" //1表示完全不透明, 0表示完全透明。這里設置起始透明度 android:duration="5000" //動畫時間,5s android:toAlpha="0" //設置結束透明度 /> </set>
2、建立Activity淡出動畫的XML描述out_alpha.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0" android:duration="5000" android:toAlpha="1.0" /> </set>
上述的xml文件存放路徑,在res路徑下新建文件夾anim,存放在此文件夾下。
在JAVA中調用動畫資源方式:R.anmi.文件名
在XML中:@[package:]anim/文件名
3、設計主Activity界面main.xml
原型圖效果:
界面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" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="change" android:text="淡入淡出Activity" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="change2" android:text="滾動切換Activity" /> </LinearLayout>
打開MainActivity定義“淡入淡出Activity”按鈕的change事件:
public void change(View v){ Intent intent = new Intent(this, OtherActivity.class); startActivity(intent); overridePendingTransition(R.anim.out_alpha, R.anim.enter_alpha); }
4、設計第二個Activity界面other.xml,并添加Activity信息到AndroidManifest.xml
原型圖效果:
創建第二個Activity界面OtherActivity類:
package mr.jin.activity; import android.app.Activity; import android.os.Bundle; public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other); } }
添加Activity信息:
<activity android:name=".OtherActivity" android:label="otherActivity">
界面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:background="#0000ff" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="這是第二個Activity界面" /> </LinearLayout>
到這里,淡入淡出切換Activity已經完成。
二、滾動方式切換
在實現淡入淡出時,界面已經設計完成,這里只需要實現動畫部分。
1、Activity滾入XML動畫描述lefttoright.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="-100%p"http://動畫對象的起始X坐標 android:toXDelta="0"http://動畫對象的結束X坐標 android:fromYDelta="0"http://這里是橫向移動,所以Y坐標無需改變,始終是0 android:toYDelta="0" android:duration="5000"http://動畫時間5s /> </set>
2、Activity滾出XML動畫描述righttoleft.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="100%p" android:fromYDelta="0" android:toYDelta="0" android:duration="5000" /> </set>
3、MainActivity中定義“滾動切換Activity”按鈕事件
public void change2(View v){ Intent intent = new Intent(this, OtherActivity.class); startActivity(intent); overridePendingTransition(R.anim.lefttoright, R.anim.righttoleft); }
案例源碼下載地址: 點擊下載
本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!