设为首页 加入收藏

TOP

Android 高仿QQ HD mini左右滑动菜单栏效果(二)
2014-11-24 07:20:51 来源: 作者: 【 】 浏览:1
Tags:Android 高仿 mini 左右 滑动 菜单 效果
nged.
*/
mIsBeingDragged = !mScroller.isFinished();
break;
}
case MotionEvent.ACTION_MOVE: {
/*
* mIsBeingDragged == false, otherwise the shortcut would have
* caught it. Check whether the user has moved far enough from his
* original down touch.
*/


/*
* Locally do absolute value. mLastMotionY is set to the y value of
* the down event.
*/
final int activePointerId = mActivePointerId;
if (activePointerId == INVALID_POINTER) {
// If we don't have a valid id, the touch down wasn't on
// content.
break;
}


final int pointerIndex = ev.findPointerIndex(activePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
final int xDiff = (int) Math.abs(x - mLastMotionX);
final int yDiff = (int) Math.abs(y - mLastMotionY);
if (xDiff > mTouchSlop && yDiff < xDiff) {
mIsBeingDragged = true;
}
break;
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
mIsBeingDragged = false;
mActivePointerId = INVALID_POINTER;
scrollToScreen();
break;
}
return mIsBeingDragged;
}


@Override
public boolean onTouchEvent(MotionEvent event) {


// Log.i(TAG, "onTouchEvent ---->>>>>" + event.getAction());
if (event.getAction() == MotionEvent.ACTION_DOWN
&& !inChild((int) event.getX(), (int) event.getY())) {
// Don't handle edge touches immediately -- they may actually belong
// to one of our
// descendants.
return false;
}


switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
return true; // 本VIEW消化掉


case MotionEvent.ACTION_MOVE:
final int activePointerIndex = event
.findPointerIndex(mActivePointerId);


final float x = event.getX(activePointerIndex);
final float y = event.getY(activePointerIndex);


final int distanceX = (int) /* Math.abs */-(x - mLastMotionX);


// 在滑动过程中,就需要显示新的brotherView,不然显示的还是之前的brotherView,最后松开手时会突然变称新brotherView,影响体验
if (distanceX < 0 && getScrollX() < 0 && leftLayout != null) {
setBrotherVisibility(LEFT);
} else if (distanceX > 0 && getScrollX() > 0 && rightLayout != null) {
setBrotherVisibility(RIGHT);
} else {
setBrotherVisibility(MIDDLE);
}


scrollBy((int) distanceX, 0);


mLastMotionX = x;
mLastMotionY = y;
break;


case MotionEvent.ACTION_UP:
mIsBeingDragged = false;
mActivePointerId = INVALID_POINTER;
scrollToScreen();
break;


default:
return super.onTouchEvent(event);
}
return mIsBeingDragged;


}


@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
super.onScrollChanged(l, t, oldl, oldt);
}


private void scrollToScreen() {


int scrollDistance = 0;


if (Math.abs(getScrollX()) > getWidth() / 2)
scrollDistance = (getScrollX() > 0) getWidth() - menuWidth
- getScrollX() : -(getWidth() - menuWidth - Math
.abs(getScrollX()));
else
scrollDistance = -getScrollX();


int distance = scrollDistance + getScrollX();
Log.i(TAG, " distance = " + distance);
if (distance > 0) {
mCurState = RIGHT;
} else if (distance < 0) {
mCurState = LEFT;
} else {
mCurState = MIDDLE;
}
mScroller.startScroll(getScrollX(), 0, scrollDistance, 0,
Math.abs(scrollDistance) * 2);
invalidate();


}


public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (Math.abs(velocityX) > ViewConfiguration.get(context)
.getScaledMinimumFlingVelocity()) {
fling = true;
scrollToScreen();
}


return fling;
}


private boolean inChild(int x, int y) {
if (getChildCount() > 0) {
final int scrollX = mScroller.getCurrX();
final View child = getChildAt(0);


return !(scrollX + x < 0 || scrollX + x > getWidth() || y < 0 || y > getHeight());


}
return false;
}


/**
* 设置当前显示的view
*
* @param whichpg
*/
public void

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇用 Qt4 Designer 配合程序设计 下一篇C++ 命名管道 IPC

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·python数据分析岗的 (2025-12-25 10:02:21)
·python做数据分析需 (2025-12-25 10:02:19)
·成为一个优秀的pytho (2025-12-25 10:02:16)
·Java后端面试实习自 (2025-12-25 09:24:21)
·Java LTS版本有哪些 (2025-12-25 09:24:18)