思路:先敲好两个Activity之间的跳转,调试成功,然后敲这个跳转的动画,调试成功,最后敲页面切换
在main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewFlipper">
<!-- 第一页 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/first"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/open"
android:onClick="openActivity"
/>
</LinearLayout>
<!-- 第二页 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#339900"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/second"
/>
</LinearLayout>
</ViewFlipper>"
</LinearLayout>
在string.xml中
<string name="first">Hello first!</string>
<string name="open">open</string>
<string name="second">Hello second!</string>
<string name="newone">I am new !</string>
在MainActivity.java中
private ViewFlipper viewFlipper;
private float startX;
private Animation in_lefttoright;
private Animation out_lefttoright;
private Animation in_righttoleft;
private Animation out_righttoleft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
in_lefttoright = AnimationUtils.loadAnimation(this, R.anim.enter_lefttoright);
out_lefttoright = AnimationUtils.loadAnimation(this,R.anim.out_lefttoright);
in_righttoleft = AnimationUtils.loadAnimation(this, R.anim.enter_righttoleft);
out_righttoleft = AnimationUtils.loadAnimation(this, R.anim.out_righttoleft);
viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
startX = event.getX();
}else if(event.getAction()==MotionEvent.ACTION_UP) {
float endX = event.getX();
if(endX > startX) {
viewFlipper.setInAnimation(in_lefttoright);
viewFlipper.setOutAnimation(out_lefttoright);
viewFlipper.showNext();
}else if(endX < startX) {
viewFlipper.setInAnimation(in_righttoleft);
viewFlipper.setOutAnimation(out_righttoleft);
viewFlipper.showPrevious();
}
return true;
}
return super.onTouchEvent(event);
}
public void openActivity(View v) {
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
this.overridePendingTransition(R.anim.enteralpha, R.anim.outalpha);
}
在AndroidManifest.xml中
</activity>
<activity android:name=".OtherActivity" android:label="新窗口"/>
在other.xml中
<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="#6600FF">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/newone"/>
</LinearLayout>
在OtherActivity.java中
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
在anim文件中
enter_lefttoright.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="500"
/>
</set>
enter_righttoleft.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="500"
/>
</set>
enteralpha.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="5000"
/>
</set>
out_lefttoright.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="500"
/>
</set>
out_righttoleft.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="500"
/>
</set>
outalpha.xml
android:shareInterpolator="false">
<alpha
android:fromAlpha="1"
android:toAlpha="0"
android:duration="5000"
/>
</set>
忘了注册 <activity android:name=".OtherActivity" android:label="新窗口"/>
详解参考:http://blog.csdn.net/lidew521/article/details/8946392