OnGestureListener是辅助监控用户手势的工具,大家应该都实现过这个接口。
简单的说一下使用:一 、 Activity中实现GestureDetector.OnGestureListener辅助接口
二 、在onCreate中 new 出 GestureDetector detector= new GestureDetector(this, this);
三 、重写onTouchEvent(MotionEvent event)方法,加入detector.onTouchEvent(event);返回true;
接口中的onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)方法,我们可以看到这里有一个
distanceX,distanceY表示在X轴跟Y轴上面的滑动距离。
//当前滚动角度 假设标准距离100为360度
currentDegel += Math.sqrt(Math.pow(distanceX, 2.0) + Math.pow(distanceY, 2.0)) / 100 * 360;
//旋转角度
float degel = currentDegel % 360;
//滚动轴x坐标
float roX;
//滚动轴y坐标
float roY;
if (distanceX == 0) {
//延X轴滚动 滚动轴是Y
roX = 1;
roY = 0;
if (distanceY < 0) {
//角度决定旋转方向 负为顺时针
degel = -degel;
}
} else if (distanceY == 0) {
//延Y轴滚动 滚动轴是X
roX = 0;
roY = 1;
if (distanceX < 0) {
//角度决定旋转方向 负为顺时针
degel = -degel;
}
} else {
if (distanceX * distanceY > 0) {
//滑动方向为一三象限,滚动轴为二四象限
roX = -Math.abs(distanceY);
roY = Math.abs(distanceX);
if (distanceX < 0) {
//角度决定旋转方向 负为二向四滑
degel = -degel;
}
} else {
//滑动方向为二四象限,滚动轴为一三象限
roX = Math.abs(distanceY);
roY = Math.abs(distanceX);
if (distanceX < 0) {
//角度决定旋转方向 负为三向一滑
degel = -degel;
}
}
}
Log.i(TAG, "滚动轴: ["+roX+","+roY+"]");
movePic(degel,roX, roY);