使用 ViewFlipper 可達成頁面切換的動態效果
layout
public class MyViewFlipperActivity extends Activity { private ViewFlipper vf; private float oldX; // @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // vf = (ViewFlipper) findViewById(R.id.viewFlipper1); } @Override public void onClick(View v) { vf.showNext(); } @Override public boolean onTouchEvent(MotionEvent touchevent) { // 滑動方式 switch (touchevent.getAction()) { case MotionEvent.ACTION_DOWN: oldX = touchevent.getX(); break; case MotionEvent.ACTION_UP: float currentX = touchevent.getX(); if (oldX < currentX) { vf.setInAnimation(inFromLeftAnimation(360)); vf.setOutAnimation(outToRightAnimation(360)); vf.showNext(); } if (oldX > currentX) { vf.setInAnimation(inFromRightAnimation(360)); vf.setOutAnimation(outToLeftAnimation(360)); vf.showPrevious(); } break; } return false; } // 前一頁 public Animation inFromRightAnimation(int duration) { Animation inFromRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); inFromRight.setDuration(duration); inFromRight.setInterpolator(new AccelerateInterpolator()); return inFromRight; } public Animation outToLeftAnimation(int duration) { Animation outtoLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); outtoLeft.setDuration(duration); outtoLeft.setInterpolator(new AccelerateInterpolator()); return outtoLeft; } // 下一頁 public Animation inFromLeftAnimation(int duration) { Animation inFromLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); inFromLeft.setDuration(duration); inFromLeft.setInterpolator(new AccelerateInterpolator()); return inFromLeft; } public Animation outToRightAnimation(int duration) { Animation outtoRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); outtoRight.setDuration(duration); outtoRight.setInterpolator(new AccelerateInterpolator()); return outtoRight; } }
layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/textView1"/> <ViewFlipper android:id="@+id/viewFlipper1" android:layout_width="match_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_height="wrap_content" android:id="@+id/panel1" android:text="頁面一" android:layout_width="fill_parent" android:layout_weight="1"> </TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_height="wrap_content" android:id="@+id/panel2" android:text="頁面二" android:layout_width="fill_parent" android:layout_weight="1" > </TextView> </LinearLayout> </ViewFlipper> </LinearLayout>