Android setOnTouchListener识别滑动手势

 setOnTouchListener(new OnTouchListener() {
private float startX, startY, offsetX, offsetY; @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_UP:
offsetX = event.getX() - startX;
offsetY = event.getY() - startY;
if (Math.abs(offsetX) > Math.abs(offsetY)) {
if (offsetX < -5) { // left
swipeLeft();
} else if (offsetX > 5) { // right
swipeRight();
}
} else {
if (offsetY < -5) { // up
swipeUp();
} else if (offsetY > 5) { // down
swipeDown();
}
}
break;
default:
break;
}
return true;
}
});
上一篇:java 内部类定义在局部时需要注意的情况


下一篇:CentOS 6.5安装JDK、Tomcat、Mysql