Android使用ViewFlipper做頁面切換,與手勢滑動切換的使用

來源:互聯網
上載者:User

 Android系統內建有一個多頁面管理的控制項:ViewFlipper.

它可以簡單實現子頁面的切換,,,

它只需使用addView方法添加幾個View,每個View對應的是一個頁面,即可完成對於多頁面的管理,,,

在android上實現手勢的識別也比較簡單,可以實現OnTouchListener和OnGuestureListener介面,

然後在OnTouch函數中註冊GestureDetector來判別手勢動作,

參考一位大牛的文章:

http://wang-peng1.iteye.com/blog/572886

GestureDetector.OnGestureListener:用來通知普通的手勢事件,該介面有如下六個回呼函數:
1.   onDown(MotionEvent e):down事件;
2.   onSingleTapUp(MotionEvent e):一次點擊up事件;
3.   onShowPress(MotionEvent e):down事件發生而move或則up還沒發生前觸發該事件;
4.   onLongPress(MotionEvent e):長按事件;
5.   onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY):滑動手勢事件;
6.   onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY):在螢幕上拖動事件。

主要判斷是在onFling()函數裡面,e1表示開始按下去的位置資訊,e2表示抬起時的位置資訊,因此可以通過它們在x軸上面 的距離差來是左滑還是右滑。。。

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,          float velocityY) {      // TODO Auto-generated method stub       if (e2.getX()-e1.getX() > 100) {          // fling right           showNextView();      } else if (e1.getX() - e2.getX() > 100) {          // fling left           showPreviousView();      }      return false;  }  

資源檔:

<?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">  <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="wrap_content">      <Button        android:id="@+id/btnPrev"        android:text="Previous"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />      <Button        android:id="@+id/btnNext"        android:text="Next"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />  </LinearLayout>  <ViewFlipper     android:id="@+id/vfFlingTest"     android:layout_width="fill_parent"     android:layout_height="fill_parent"></ViewFlipper>  </LinearLayout> 

代碼:

import android.app.Activity;  import android.os.Bundle;  import android.view.GestureDetector;  import android.view.KeyEvent;  import android.view.MotionEvent;  import android.view.View;  import android.view.GestureDetector.OnGestureListener;  import android.view.View.OnClickListener;  import android.view.View.OnTouchListener;  import android.view.ViewGroup.LayoutParams;  import android.view.animation.AnimationUtils;  import android.widget.Button;  import android.widget.EditText;  import android.widget.ImageView;  import android.widget.TextView;  import android.widget.ViewFlipper;  public class FlingTest extends Activity implements              OnTouchListener, OnGestureListener{      private Button btnPrev;      private Button btnNext;      private ViewFlipper vfFlingTest;            private TextView tvFlipper;      private EditText etFlipper;      private ImageView ivFlipper;            private GestureDetector mGestureDetector;            @Override      protected void onCreate(Bundle savedInstanceState) {          // TODO Auto-generated method stub           super.onCreate(savedInstanceState);          setContentView(R.layout.fling_test);                    btnPrev = (Button)findViewById(R.id.btnPrev);          btnNext = (Button)findViewById(R.id.btnNext);          vfFlingTest = (ViewFlipper)findViewById(R.id.vfFlingTest);                    initViews();                    vfFlingTest.addView(tvFlipper);          vfFlingTest.addView(etFlipper);          vfFlingTest.addView(ivFlipper);                    vfFlingTest.setOnTouchListener(this);          vfFlingTest.setLongClickable(true);          mGestureDetector = new GestureDetector(this);                    btnPrev.setOnClickListener(new OnClickListener() {                            public void onClick(View v) {                  // TODO Auto-generated method stub                   showPreviousView();              }          });                    btnNext.setOnClickListener(new OnClickListener() {                            public void onClick(View v) {                  // TODO Auto-generated method stub                   showNextView();              }          });      }            public void showPreviousView() {          vfFlingTest.setInAnimation(AnimationUtils.loadAnimation(                  this, R.anim.right_in));          vfFlingTest.setOutAnimation(AnimationUtils.loadAnimation(                  this, R.anim.left_out));          vfFlingTest.showPrevious();      }            public void showNextView() {          vfFlingTest.setInAnimation(AnimationUtils.loadAnimation(                  this, R.anim.left_in));          vfFlingTest.setOutAnimation(AnimationUtils.loadAnimation(                  this, R.anim.right_out));          vfFlingTest.showNext();      }            private void initViews() {          tvFlipper = new TextView(this);          tvFlipper.setText("this is a text view!");                    etFlipper = new EditText(this);          etFlipper.setText("this is a text view!");                    ivFlipper = new ImageView(this);          ivFlipper.setLayoutParams(new LayoutParams(                  LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));          ivFlipper.setImageResource(R.drawable.pic1);      }      @Override      protected void onDestroy() {          // TODO Auto-generated method stub           android.os.Process.killProcess(android.os.Process.myPid());          super.onDestroy();      }      @Override      public boolean onKeyDown(int keyCode, KeyEvent event) {          // TODO Auto-generated method stub           if (keyCode == KeyEvent.KEYCODE_BACK) {              finish();              return true;          }          return super.onKeyDown(keyCode, event);      }      public boolean onTouch(View view, MotionEvent event) {          // TODO Auto-generated method stub           return mGestureDetector.onTouchEvent(event);      }        public boolean onDown(MotionEvent arg0) {          // TODO Auto-generated method stub           return false;      }      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,              float velocityY) {          // TODO Auto-generated method stub           if (e2.getX()-e1.getX() > 100) {              // fling right               showNextView();          } else if (e1.getX() - e2.getX() > 100) {              // fling left               showPreviousView();          }          return false;      }      public void onLongPress(MotionEvent e) {          // TODO Auto-generated method stub                 }      public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,              float distanceY) {          // TODO Auto-generated method stub           return false;      }      public void onShowPress(MotionEvent e) {          // TODO Auto-generated method stub                 }      public boolean onSingleTapUp(MotionEvent e) {          // TODO Auto-generated method stub           return false;      }  }  

還有就是一個頁面切換的簡單動畫效果(right_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" />   </set> 

right_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" />   </set> 

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" />   </se

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" />   </set>  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.