(1)我们在MainAcitvity中定义两个textview,用于点击触发切换Activity事件,下面是布局文件代码。
<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方法,添加切换动画的效果,第一个参数是进入的效果,第二个参数是退出的效果。
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>