Activty介面切換動畫

來源:互聯網
上載者:User

在介紹切換動畫效果前,先介紹下將使用到的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"    >    <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="change"android:text="淡入淡出Activity" /><Buttonandroid: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 {@Overrideprotected 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"//動畫對象的起始X座標        android:toXDelta="0"//動畫對象的結束X座標        android:fromYDelta="0"//這裡是橫向移動,所以Y座標無需改變,始終是0        android:toYDelta="0"        android:duration="5000"//動畫時間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);    }

案例源碼:點擊下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.