屏幕切換指的是在同一個Activity內屏幕見的切換,最長見的情況就是在一個FrameLayout內有多個頁面,比如一個系統設置頁面;一個個性化設置頁面。
通過查看OPhone API文檔可以發現,有個android.widget.ViewAnimator類繼承至FrameLayout,ViewAnimator類的作用是爲FrameLayout裏面的View切換提供動畫效果。該類有如下幾個和動畫相關的函數:
- setOutAnimation: 設置View退出屏幕時候使用的動畫,參數setInAnimation函數一樣。
- showNext: 調用該函數來顯示FrameLayout裏面的下一個View。
- showPrevious:調用該函數來顯示FrameLayout裏面的上一個View。
- isFlipping: 用來判斷View切換是否正在進行
- setFilpInterval:設置View之間切換的時間間隔
- startFlipping:使用上面設置的時間間隔來開始切換所有的View,切換會循環進行
- stopFlipping: 停止View切換
- <?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">
- <ViewFlipper android:id="@+id/details"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:persistentDrawingCache="animation"
- android:flipInterval="1000"
- android:inAnimation="@anim/push_left_in"
- android:outAnimation="@anim/push_left_out"
- >
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button
- android:text="Next"
- android:id="@+id/Button_next1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </Button>
- <ImageView
- android:id="@+id/image1"
- android:src="@drawable/dell1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </ImageView>
- </LinearLayout>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button
- android:text="Next"
- android:id="@+id/Button_next2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </Button>
- <ImageView
- android:id="@+id/image2"
- android:src="@drawable/lg"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </ImageView>
- </LinearLayout>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button
- android:text="Next"
- android:id="@+id/Button_next3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </Button>
- <ImageView
- android:id="@+id/image3"
- android:src="@drawable/lenovo"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </ImageView>
- </LinearLayout>
- </ViewFlipper>
- </LinearLayout>
- res\anim\push_left_in.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta="100%p"
- android:toXDelta="0"
- android:duration="500"/>
- <alpha
- android:fromAlpha="0.0"
- android:toAlpha="1.0"
- android:duration="500" />
- </set>
- res\anim\push_left_out.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:duration="500"/>
- <alpha
- android:fromAlpha="1.0"
- android:toAlpha="0.0"
- android:duration="500" />
- </set>
- public class TestActivity extends Activity {
- private ViewFlipper mViewFlipper;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button buttonNext1 = (Button) findViewById(R.id.Button_next1);
- mViewFlipper = (ViewFlipper) findViewById(R.id.flipper);
- buttonNext1.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- //在layout中定義的屬性,也可以在代碼中指定
- // mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in);
- // mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out);
- // mViewFlipper.setPersistentDrawingCache(ViewGroup.PERSISTENT_ALL_CACHES);
- // mViewFlipper.setFlipInterval(1000);
- mViewFlipper.showNext();
- //調用下面的函數將會循環顯示mViewFlipper內的所有View。
- // mViewFlipper.startFlipping();
- }
- });
- Button buttonNext2 = (Button) findViewById(R.id.Button_next2);
- buttonNext2.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- mViewFlipper.showNext();
- }
- });
- Button buttonNext3 = (Button) findViewById(R.id.Button_next3);
- buttonNext3.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- mViewFlipper.showNext();
- }
- });
- }
- }
- class TestActivity extends Activity implements OnGestureListener , OnDoubleTapListener
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- Log.d(tag, "...onFling...");
- if(e1.getX() > e2.getX()) {//move to left
- mViewFlipper.showNext();
- }else if(e1.getX() < e2.getX()) {
- mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_right_in);
- mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_right_out);
- mViewFlipper.showPrevious();
- mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in);
- mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out);
- }else {
- return false;
- }
- return true;
- }
- public boolean onDoubleTap(MotionEvent e) {
- Log.d(tag, "...onDoubleTap...");
- if(mViewFlipper.isFlipping()) {
- mViewFlipper.stopFlipping();
- }else {
- mViewFlipper.startFlipping();
- }
- return true;
- }
- public GestureDetector(OnGestureListener listener) {
- this(null, listener, null);
- }
- public GestureDetector(Context context, OnGestureListener listener, Handler handler) {
- if (handler != null) {
- mHandler = new GestureHandler(handler);
- } else {
- mHandler = new GestureHandler();
- }
- mListener = listener;
- if (listener instanceof OnDoubleTapListener) {
- setOnDoubleTapListener((OnDoubleTapListener) listener);
- }
- init(context);