欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android判断手势方向

程序员文章站 2024-03-24 20:51:04
...
import com.inventec.painter.ScreenUtil.Screen;

import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

public class GestureSlideExt {
 
 public static final int GESTURE_UP = 1;
 
 public static final int GESTURE_RIGHT = 2;
 
 public static final int GESTURE_DOWN = 3;
 
 public static final int GESTURE_LEFT = 4;
 private Context mContext;
 private Screen screen;
 private OnGestureResult onGestureResult;

 public GestureDetector Buile() {
  return new GestureDetector(mContext, onGestureListener);
 }

 public GestureSlideExt(Context context, OnGestureResult onGestureResult) {
  this.mContext = context;
  this.onGestureResult = onGestureResult;
  screen = ScreenUtil.getScreenPix(context);
 }

 public void doResult(int result) {
  if (onGestureResult != null) {
   onGestureResult.onGestureResult(result);
  }
 }

 public interface OnGestureResult {
  public void onGestureResult(int direction);
 }

 private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
   float x = e2.getX() - e1.getX();
   float y = e2.getY() - e1.getY();
   // 限制必须得划过屏幕的1/4才能算划过
   float x_limit = screen.widthPixels / 4;
   float y_limit = screen.heightPixels / 4;
   float x_abs = Math.abs(x);
   float y_abs = Math.abs(y);
   if (x_abs >= y_abs) {
    // gesture left or right
    if (x > x_limit || x < -x_limit) {
     if (x > 0) {
      // right
      doResult(GESTURE_RIGHT);
     } else if (x <= 0) {
      // left
      doResult(GESTURE_LEFT);
     }
    }
   } else {
    // gesture down or up
    if (y > y_limit || y < -y_limit) {
     if (y > 0) {
      // down
      doResult(GESTURE_DOWN);
     } else if (y <= 0) {
      // up
      doResult(GESTURE_UP);
     }
    }
   }
   Log.e("Tag", "判断结束");
   return true;
  }
 };
}

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class ScreenUtil {
 
 public static Screen getScreenPix(Context context) {
  DisplayMetrics dm = new DisplayMetrics();
  WindowManager windowManager = (WindowManager) context
    .getSystemService(Context.WINDOW_SERVICE);
  windowManager.getDefaultDisplay().getMetrics(dm);
  return new Screen(dm.widthPixels, dm.heightPixels);
 }

 public static class Screen {
  public int widthPixels;
  public int heightPixels;

  public Screen() {
  }

  public Screen(int widthPixels, int heightPixels) {
   this.widthPixels = widthPixels;
   this.heightPixels = heightPixels;
  }

  public String toString() {
   return "(" + widthPixels + "," + heightPixels + ")";
  }
 }
}
 

 

 

GestureDetector gestureDetector;

gestureDetector = new GestureSlideExt(PainterActivity.this, new GestureSlideExt.OnGestureResult() {
         
   @Override
   public void onGestureResult(int direction) {
    // TODO Auto-generated method stub
    switch(direction){
    case GestureSlideExt.GESTURE_UP:
     tv.setText("up");
     show("滑屏手势方向:GESTURE_UP(向上)");
     break;
    case GestureSlideExt.GESTURE_RIGHT:
     tv.setText("right");
     show("滑屏手势方向:GESTURE_RIGHT(向右)");
     break;
    case GestureSlideExt.GESTURE_DOWN:
     tv.setText("down");
     show("滑屏手势方向:GESTURE_DOWN(向下)");
     break;
    case GestureSlideExt.GESTURE_LEFT:
     tv.setText("left");
     show("滑屏手势方向:GESTURE_LEFT(向左)");
     break;
    }
   }
  }).Buile();


转载于:https://my.oschina.net/yuhanxun/blog/78647