这在单手操作时很重要。
在MotionEvent.ACTION_DOWN,获取当前操作区域以及记录(x,y)坐标;
MotionEvent.ACTION_UP处判断是点击还是滑动事件;
以下代码将View三分,且在中部判断左右上下滑动事件。
private void onRealTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if(mListener.shouldHideBar()){
break;
}
mLastX = (int) event.getX();
mIsLoadNextPage = true;
if(event.getX() > viewWidth - viewWidth / 3 && event.getY() < viewHeight / 3){ //从上半部分翻页
mCurrStyle = STYLE.TOP_RIGHT;
setTouchPoint(event.getX(),event.getY());
} else if(event.getX() > viewWidth - viewWidth / 3 && event.getY() >= viewHeight - viewHeight / 3) { //从下半部分翻页
mCurrStyle = STYLE.BOTTOM_RIGHT;
setTouchPoint(event.getX(),event.getY());
} else if(event.getX() > viewWidth - viewWidth / 3) { //从中间翻页
mCurrStyle = STYLE.CENTER_RIGHT;
setTouchPoint(event.getX(),event.getY());
} else if (event.getX() < viewWidth / 3) {
mCurrStyle = STYLE.LEFT;
} else {
x1 = event.getX();
y1 = event.getY();
mCurrStyle = STYLE.CENTER;
}
break;
case MotionEvent.ACTION_MOVE:
if(mListener.shouldHideBar()){
break;
}
if (mCurrStyle != STYLE.LEFT && mCurrStyle != STYLE.CENTER) {
setTouchPoint(event.getX(),event.getY());
}
if (Math.abs(event.getX() - mLastX) >= 5) {
mIsLoadNextPage = event.getX() < mLastX;
}
mLastX = (int) event.getX();
break;
case MotionEvent.ACTION_UP:
if(mListener.shouldHideBar()){
mListener.showOrHideSettingBar();
break;
}
if (mCurrStyle == STYLE.LEFT) {
// 翻到上一页(动画)
startLastAnim();
} else if (mCurrStyle == STYLE.CENTER) {
// 弹出或隐藏菜单
x2 = event.getX();
y2 = event.getY();
if(y1 - y2 > 50) {//向上滑
pre();
} else if(y2 - y1 > 50) { //向下滑
next();
} else if(x1 - x2 > 50) { // 向左滑
next();
} else if(x2 - x1 > 50) { //向右滑
pre();
}else{
mListener.showOrHideSettingBar();
}
} else {
if (mIsLoadNextPage) {
startTurnAnim();
} else {
startCancelAnim();
}
}
break;
}
}
网友评论