private float downX, downY;
private final float SLIDE_ANGLE = 45;
private boolean mIsSlideHorizontally;//是否是水平滑动
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = ev.getX();
downY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
float moveX = ev.getX();
float moveY = ev.getY();
float xDiff = Math.abs(moveX - downX);
float yDiff = Math.abs(moveY - downY);
double squareRoot = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
//滑动的角度
int yAngle = Math.round((float) (Math.asin(yDiff / squareRoot) / Math.PI * 180));
int xAngle = Math.round((float) (Math.asin(xDiff / squareRoot) / Math.PI * 180));
boolean isMeetSlidingYAngle = yAngle > SLIDE_ANGLE;//滑动角度是否大于45du
boolean isMeetSlidingXAngle = xAngle > SLIDE_ANGLE;//滑动角度是否大于45du
boolean isSlideUp = moveY < downY && isMeetSlidingYAngle;
boolean isSlideDown = moveY > downY && isMeetSlidingYAngle;
boolean isSlideLeft = moveX < downX && isMeetSlidingXAngle;
boolean isSlideRight = moveX > downX && isMeetSlidingXAngle;
if (isSlideUp) {
Log.d("chenhaocc", "向上滑动");
mIsSlideHorizontally = false;
} else if (isSlideDown) {
Log.d("chenhaocc", "向下滑动");
mIsSlideHorizontally = false;
} else if (isSlideLeft) {
Log.d("chenhaocc", "向左边滑动");
mIsSlideHorizontally = true;
} else if (isSlideRight) {
mIsSlideHorizontally = true;
Log.d("chenhaocc", "向右边滑动");
}
}
return super.dispatchTouchEvent(ev);
}
android 判断手指滑动方向
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- /** 添加平移手势 @param view*/ (void)addTapGestureWithView:(UIV...