Android手勢識別簡單封裝類

來源:互聯網
上載者:User

   在做一個項目時用到了簡單的手勢功能(向左,向右滑切屏),於是簡單地封裝了下

  import android.content.Context;

  import android.util.DisplayMetrics;

  import android.view.GestureDetector;

  import android.view.GestureDetector.OnGestureListener;

  import android.view.MotionEvent;

  public class GestureHelper implements OnGestureListener {

  private GestureDetector gesture_detector;

  private int screen_width;

  private OnFlingListener listener_onfling;

  public static abstract class OnFlingListener {

  public abstract void OnFlingLeft();

  public abstract void OnFlingRight();

  }

  public GestureHelper(Context context) {

  DisplayMetrics dm = context.getResources().getDisplayMetrics();

  screen_width = dm.widthPixels;

  gesture_detector = new GestureDetector(context, this);

  }

  public void setOnFlingListener(OnFlingListener listener) {

  listener_onfling = listener;

  }

  public boolean onTouchEvent(MotionEvent event) {

  return gesture_detector.onTouchEvent(event);

  }

  @Override

  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

  // 觸發條件 :

  // X軸的座標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒

  final int FLING_MIN_DISTANCE = (int) (screen_width / 3.0f), FLING_MIN_VELOCITY = 200;

  if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {

  listener_onfling.OnFlingLeft();

  } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {

  listener_onfling.OnFlingRight();

  }

  return true;

  }

  @Override

  public boolean onDown(MotionEvent e) {

  return false;

  }

  @Override

  public void onLongPress(MotionEvent e) {

  }

  @Override

  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,

  float distanceY) {

  return false;

  }

  @Override

  public void onShowPress(MotionEvent e) {

  }

  @Override

  public boolean onSingleTapUp(MotionEvent e) {

  return false;

  }

  }

  使用方法如下:

  public class TestActivity extends Activity {

  private GestureHelper gh;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_start);

  gh = new GestureHelper(this);

  gh.setOnFlingListener(new OnFlingListener() {

  @Override

  public void OnFlingLeft() {

  //向左滑動

  }

  @Override

  public void OnFlingRight() {

  //向右滑動

  }

  });

  }

  @Override

  public boolean onTouchEvent(MotionEvent event) {

  return gh.onTouchEvent(event);

  }

  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.