Android開發中activity切換動畫的實現

 

(1)咱們在MainAcitvity中定義兩個textview,用於點擊觸發切換Activity事件,下面是佈局文件代碼。android

  <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/top_bg"
            android:orientation="horizontal" >
            <LinearLayout
                android:id="@+id/list_all"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1.0"
                android:background="@drawable/btn_bottom_bg"
                android:clickable="true"
                android:gravity="center"
                android:orientation="vertical" >
                <TextView
                    android:id="@+id/tv_all"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableLeft="@drawable/city"
                    android:drawablePadding="5.0dip"
                    android:duplicateParentState="true"
                    android:gravity="center"
                    android:text="@string/all"
                    android:textColor="@color/white"
                    android:textSize="13.5sp" />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/list_school"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1.0"
                android:background="@drawable/btn_bottom_bg"
                android:clickable="true"
                android:gravity="center"
                android:orientation="vertical" >
                <TextView
                    android:id="@+id/tv_school"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableLeft="@drawable/location"
                    android:drawablePadding="5.0dip"
                    android:duplicateParentState="true"
                    android:gravity="center"
                    android:text="@string/school"
                    android:textColor="@color/white"
                    android:textSize="13.5sp" />
            </LinearLayout>
        </LinearLayout>

(2)爲textView添加點擊事件。重寫overridePendingTransition方法,添加切換動畫的效果,第一個參數是進入的效果,第二個參數是退出的效果。ide

TextView tv_all = (TextView) this.findViewById(R.id.tv_all);
        tv_all.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(SecondActivity.this, MainActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.slide_in_left,  
                        R.anim.slide_out_right); 
            }
        });

(3)slide_in_left.xml進入的效果佈局

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >  
    <translate  
        android:duration="500"  
        android:fromXDelta="-50%p"  
        android:toXDelta="0" />  
    <alpha  
        android:duration="500"  
        android:fromAlpha="0.0"  
        android:toAlpha="1.0" />  
</set>  

()slide_out_right.xml退出的效果動畫

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate 
        android:duration="500" 
        android:fromXDelta="0" 
        android:toXDelta="50%p" />
    <alpha 
        android:duration="500" 
        android:fromAlpha="1.0" 
        android:toAlpha="0.0" />  
</set>  
相關文章
相關標籤/搜索