use of android.view.VelocityTracker in project android_frameworks_base by DirtyUnicorns.
the class HorizontalScrollView method onTouchEvent.
@Override
public boolean onTouchEvent(MotionEvent ev) {
initVelocityTrackerIfNotExists();
mVelocityTracker.addMovement(ev);
final int action = ev.getAction();
switch(action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
{
if (getChildCount() == 0) {
return false;
}
if ((mIsBeingDragged = !mScroller.isFinished())) {
final ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
}
/*
* If being flinged and user touches, stop the fling. isFinished
* will be false if being flinged.
*/
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
// Remember where the motion event started
mLastMotionX = (int) ev.getX();
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE:
final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
if (activePointerIndex == -1) {
Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
break;
}
final int x = (int) ev.getX(activePointerIndex);
int deltaX = mLastMotionX - x;
if (!mIsBeingDragged && Math.abs(deltaX) > mTouchSlop) {
final ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
mIsBeingDragged = true;
if (deltaX > 0) {
deltaX -= mTouchSlop;
} else {
deltaX += mTouchSlop;
}
}
if (mIsBeingDragged) {
// Scroll to follow the motion event
mLastMotionX = x;
final int oldX = mScrollX;
final int oldY = mScrollY;
final int range = getScrollRange();
final int overscrollMode = getOverScrollMode();
final boolean canOverscroll = overscrollMode == OVER_SCROLL_ALWAYS || (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);
// calls onScrollChanged if applicable.
if (overScrollBy(deltaX, 0, mScrollX, 0, range, 0, mOverscrollDistance, 0, true)) {
// Break our velocity if we hit a scroll barrier.
mVelocityTracker.clear();
}
if (canOverscroll) {
final int pulledToX = oldX + deltaX;
if (pulledToX < 0) {
mEdgeGlowLeft.onPull((float) deltaX / getWidth(), 1.f - ev.getY(activePointerIndex) / getHeight());
if (!mEdgeGlowRight.isFinished()) {
mEdgeGlowRight.onRelease();
}
} else if (pulledToX > range) {
mEdgeGlowRight.onPull((float) deltaX / getWidth(), ev.getY(activePointerIndex) / getHeight());
if (!mEdgeGlowLeft.isFinished()) {
mEdgeGlowLeft.onRelease();
}
}
if (mEdgeGlowLeft != null && (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished())) {
postInvalidateOnAnimation();
}
}
}
break;
case MotionEvent.ACTION_UP:
if (mIsBeingDragged) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int initialVelocity = (int) velocityTracker.getXVelocity(mActivePointerId);
if (getChildCount() > 0) {
if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
fling(-initialVelocity);
} else {
if (mScroller.springBack(mScrollX, mScrollY, 0, getScrollRange(), 0, 0)) {
postInvalidateOnAnimation();
}
}
}
mActivePointerId = INVALID_POINTER;
mIsBeingDragged = false;
recycleVelocityTracker();
if (mEdgeGlowLeft != null) {
mEdgeGlowLeft.onRelease();
mEdgeGlowRight.onRelease();
}
}
break;
case MotionEvent.ACTION_CANCEL:
if (mIsBeingDragged && getChildCount() > 0) {
if (mScroller.springBack(mScrollX, mScrollY, 0, getScrollRange(), 0, 0)) {
postInvalidateOnAnimation();
}
mActivePointerId = INVALID_POINTER;
mIsBeingDragged = false;
recycleVelocityTracker();
if (mEdgeGlowLeft != null) {
mEdgeGlowLeft.onRelease();
mEdgeGlowRight.onRelease();
}
}
break;
case MotionEvent.ACTION_POINTER_UP:
onSecondaryPointerUp(ev);
break;
}
return true;
}
use of android.view.VelocityTracker in project Klyph by jonathangerbaud.
the class ScrollableViewGroup method onTouchEvent.
public boolean onTouchEvent(MotionEvent motionevent) {
int i;
float f = 0;
i = motionevent.getAction();
if (mFlingable) {
if (mVelocityTracker == null) {
VelocityTracker velocitytracker = VelocityTracker.obtain();
mVelocityTracker = velocitytracker;
}
mVelocityTracker.addMovement(motionevent);
}
if (mIsBeingDragged) {
switch(i) {
case 1:
{
return true;
}
case 2:
{
VelocityTracker velocitytracker1;
if (i == 3) {
f = 1.401298E-45F;
} else {
f = 0F;
}
mIsBeingDragged = false;
if (f != 0 || !mFlingable || getChildCount() <= 0) {
int j3 = mScrollDirection;
onScrollFinished(j3);
if (mFlingable && mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
mReceivedDown = false;
} else {
velocitytracker1 = mVelocityTracker;
velocitytracker1.computeCurrentVelocity(1000, mScrollDirection);
if (mVertical) {
f = mVelocityTracker.getYVelocity();
} else {
f = mVelocityTracker.getXVelocity();
}
if (f > mMinimumVelocity || f >= -mMinimumVelocity) {
float f7 = -f;
mFlingVelocity = f7;
if (mVertical) {
mScroller.fling(getScrollX(), getScrollY(), 0, (int) -f, 0, 0, mLimits[0], mLimits[1]);
} else {
mScroller.fling(getScrollX(), getScrollY(), (int) -f, 0, mLimits[0], mLimits[1], 0, 0);
}
invalidate();
} else {
onScrollFinished(mScrollDirection);
if (mFlingable && mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
mReceivedDown = false;
}
}
return true;
}
case 3:
{
float f1 = 0;
if (mVertical) {
f = 1;
}
f1 = mLastPosition[(int) f];
updatePosition(motionevent);
float f2 = mLastPosition[(int) f];
float f3 = f1 - f2;
if (f3 < -1F) {
mScrollDirection = -1;
} else if (f3 > 1F) {
mScrollDirection = 1;
}
int k = (int) f3;
scrollTo(getScroll() + k);
return true;
}
}
} else {
boolean b = false;
if (shouldStartDrag(motionevent)) {
b = true;
} else if (i == 1 && mReceivedDown) {
mReceivedDown = false;
b = performClick();
} else {
b = true;
}
return b;
}
return false;
}
use of android.view.VelocityTracker in project SmartAndroidSource by jaychou2012.
the class DragGridViewPager method onTouchEvent.
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
// descendants.
return false;
}
if (mPageCount <= 0) {
// Nothing to present or scroll; nothing to touch.
return false;
}
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(ev);
final int action = ev.getAction();
boolean needsInvalidate = false;
switch(action & MotionEventCompat.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
{
mScroller.abortAnimation();
// Remember where the motion event started
mLastMotionX = mInitialMotionX = ev.getX();
mLastMotionY = mInitialMotionY = ev.getY();
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
DEBUG_LOG("Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged + " mIsUnableToDrag=" + mIsUnableToDrag);
if (!mIsBeingDragged && mScrollState == SCROLL_STATE_IDLE) {
mLastPosition = getPositionByXY((int) mLastMotionX, (int) mLastMotionY);
} else {
mLastPosition = -1;
}
if (mLastPosition >= 0) {
mLastDownTime = System.currentTimeMillis();
} else {
mLastDownTime = Long.MAX_VALUE;
}
DEBUG_LOG("Down at mLastPosition=" + mLastPosition);
mLastDragged = -1;
break;
}
case MotionEvent.ACTION_MOVE:
{
final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float y = MotionEventCompat.getY(ev, pointerIndex);
if (mLastDragged >= 0) {
// change draw location of dragged visual
final View v = getChildAt(mLastDragged);
final int l = getScrollX() + (int) x - v.getWidth() / 2;
final int t = getScrollY() + (int) y - v.getHeight() / 2;
v.layout(l, t, l + v.getWidth(), t + v.getHeight());
// check for new target hover
if (mScrollState == SCROLL_STATE_IDLE) {
final int target = getTargetByXY((int) x, (int) y);
if (target != -1 && mLastTarget != target) {
animateGap(target);
mLastTarget = target;
DEBUG_LOG("Moved to mLastTarget=" + mLastTarget);
}
// edge holding
final int edge = getEdgeByXY((int) x, (int) y);
if (mLastEdge == -1) {
if (edge != mLastEdge) {
mLastEdge = edge;
mLastEdgeTime = System.currentTimeMillis();
}
} else {
if (edge != mLastEdge) {
mLastEdge = -1;
} else {
if ((System.currentTimeMillis() - mLastEdgeTime) >= EDGE_HOLD_DURATION) {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
triggerSwipe(edge);
mLastEdge = -1;
}
}
}
}
} else if (!mIsBeingDragged) {
final float xDiff = Math.abs(x - mLastMotionX);
final float yDiff = Math.abs(y - mLastMotionY);
DEBUG_LOG("Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
if (xDiff > mTouchSlop && xDiff > yDiff) {
DEBUG_LOG("Starting drag!");
mIsBeingDragged = true;
requestParentDisallowInterceptTouchEvent(true);
mLastMotionX = x - mInitialMotionX > 0 ? mInitialMotionX + mTouchSlop : mInitialMotionX - mTouchSlop;
mLastMotionY = y;
setScrollState(SCROLL_STATE_DRAGGING);
setScrollingCacheEnabled(true);
}
}
// Not else! Note that mIsBeingDragged can be set above.
if (mIsBeingDragged) {
// Scroll to follow the motion event
needsInvalidate |= performDrag(x);
} else if (mLastPosition >= 0) {
final int currentPosition = getPositionByXY((int) x, (int) y);
DEBUG_LOG("Moved to currentPosition=" + currentPosition);
if (currentPosition == mLastPosition) {
if ((System.currentTimeMillis() - mLastDownTime) >= LONG_CLICK_DURATION) {
if (onItemLongClick(currentPosition)) {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
mLastDragged = mLastPosition;
requestParentDisallowInterceptTouchEvent(true);
mLastTarget = -1;
animateDragged();
mLastPosition = -1;
}
mLastDownTime = Long.MAX_VALUE;
}
} else {
mLastPosition = -1;
}
}
break;
}
case MotionEvent.ACTION_UP:
{
DEBUG_LOG("Touch up!!!");
final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float y = MotionEventCompat.getY(ev, pointerIndex);
if (mLastDragged >= 0) {
rearrange();
} else if (mIsBeingDragged) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
final int width = getWidth();
final int scrollX = getScrollX();
final int currentPage = scrollX / width;
final int offsetPixels = scrollX - currentPage * width;
final float pageOffset = (float) offsetPixels / (float) width;
final int totalDelta = (int) (x - mInitialMotionX);
int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
setCurrentItemInternal(nextPage, true, true, initialVelocity);
mActivePointerId = INVALID_POINTER;
endDrag();
} else if (mLastPosition >= 0) {
final int currentPosition = getPositionByXY((int) x, (int) y);
DEBUG_LOG("Touch up!!! currentPosition=" + currentPosition);
if (currentPosition == mLastPosition) {
onItemClick(currentPosition);
}
}
break;
}
case MotionEvent.ACTION_CANCEL:
DEBUG_LOG("Touch cancel!!!");
if (mLastDragged >= 0) {
rearrange();
} else if (mIsBeingDragged) {
scrollToItem(mCurItem, true, 0, false);
mActivePointerId = INVALID_POINTER;
endDrag();
}
break;
case MotionEventCompat.ACTION_POINTER_DOWN:
{
final int index = MotionEventCompat.getActionIndex(ev);
final float x = MotionEventCompat.getX(ev, index);
mLastMotionX = x;
mActivePointerId = MotionEventCompat.getPointerId(ev, index);
break;
}
case MotionEventCompat.ACTION_POINTER_UP:
onSecondaryPointerUp(ev);
mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
break;
}
if (needsInvalidate) {
ViewCompat.postInvalidateOnAnimation(this);
}
return true;
}
use of android.view.VelocityTracker in project SmartAndroidSource by jaychou2012.
the class CustomViewAbove method onTouchEvent.
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!mEnabled)
return false;
if (!mIsBeingDragged && !thisTouchAllowed(ev))
return false;
// if (!mIsBeingDragged && !mQuickReturn)
// return false;
final int action = ev.getAction();
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(ev);
switch(action & MotionEventCompat.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
/*
* If being flinged and user touches, stop the fling. isFinished
* will be false if being flinged.
*/
completeScroll();
// Remember where the motion event started
int index = MotionEventCompat.getActionIndex(ev);
mActivePointerId = MotionEventCompat.getPointerId(ev, index);
mLastMotionX = mInitialMotionX = ev.getX();
break;
case MotionEvent.ACTION_MOVE:
if (!mIsBeingDragged) {
determineDrag(ev);
if (mIsUnableToDrag)
return false;
}
if (mIsBeingDragged) {
// Scroll to follow the motion event
final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
if (mActivePointerId == INVALID_POINTER)
break;
final float x = MotionEventCompat.getX(ev, activePointerIndex);
final float deltaX = mLastMotionX - x;
mLastMotionX = x;
float oldScrollX = getScrollX();
float scrollX = oldScrollX + deltaX;
final float leftBound = getLeftBound();
final float rightBound = getRightBound();
if (scrollX < leftBound) {
scrollX = leftBound;
} else if (scrollX > rightBound) {
scrollX = rightBound;
}
// Don't lose the rounded component
mLastMotionX += scrollX - (int) scrollX;
scrollTo((int) scrollX, getScrollY());
pageScrolled((int) scrollX);
}
break;
case MotionEvent.ACTION_UP:
if (mIsBeingDragged) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
final int scrollX = getScrollX();
final float pageOffset = (float) (scrollX - getDestScrollX(mCurItem)) / getBehindWidth();
final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
if (mActivePointerId != INVALID_POINTER) {
final float x = MotionEventCompat.getX(ev, activePointerIndex);
final int totalDelta = (int) (x - mInitialMotionX);
int nextPage = determineTargetPage(pageOffset, initialVelocity, totalDelta);
setCurrentItemInternal(nextPage, true, true, initialVelocity);
} else {
setCurrentItemInternal(mCurItem, true, true, initialVelocity);
}
mActivePointerId = INVALID_POINTER;
endDrag();
} else if (mQuickReturn && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
// close the menu
setCurrentItem(1);
endDrag();
}
break;
case MotionEvent.ACTION_CANCEL:
if (mIsBeingDragged) {
setCurrentItemInternal(mCurItem, true, true);
mActivePointerId = INVALID_POINTER;
endDrag();
}
break;
case MotionEventCompat.ACTION_POINTER_DOWN:
{
final int indexx = MotionEventCompat.getActionIndex(ev);
mLastMotionX = MotionEventCompat.getX(ev, indexx);
mActivePointerId = MotionEventCompat.getPointerId(ev, indexx);
break;
}
case MotionEventCompat.ACTION_POINTER_UP:
onSecondaryPointerUp(ev);
int pointerIndex = getPointerIndex(ev, mActivePointerId);
if (mActivePointerId == INVALID_POINTER)
break;
mLastMotionX = MotionEventCompat.getX(ev, pointerIndex);
break;
}
return true;
}
use of android.view.VelocityTracker in project ADWLauncher2 by boombuler.
the class Workspace method onTouchEvent.
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mLauncher.isAllAppsVisible()) {
// Cancel any scrolling that is in progress.
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
snapToScreen(mCurrentScreen);
// We don't want the events. Let them fall through to the all apps view.
return false;
}
acquireVelocityTrackerAndAddMovement(ev);
final int action = ev.getAction();
switch(action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
/*
* If being flinged and user touches, stop the fling. isFinished
* will be false if being flinged.
*/
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
// Remember where the motion event started
mLastMotionX = ev.getX();
mActivePointerId = ev.getPointerId(0);
if (mTouchState == TOUCH_STATE_SCROLLING) {
enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
}
break;
case MotionEvent.ACTION_MOVE:
if (mTouchState == TOUCH_STATE_SCROLLING) {
// Scroll to follow the motion event
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float deltaX = mLastMotionX - x;
mLastMotionX = x;
if (deltaX < 0) {
if (mTouchX > 0) {
mTouchX += Math.max(-mTouchX, deltaX);
mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
invalidate();
} else if (Preferences.getInstance().getEndlessScrolling() && mTouchX > -getWidth()) {
mTouchX += deltaX;
mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
invalidate();
}
} else if (deltaX > 0) {
final float availableToScroll = getChildAt(getChildCount() - 1).getRight() - mTouchX - (Preferences.getInstance().getEndlessScrolling() ? 0 : getWidth());
if (availableToScroll > 0) {
mTouchX += Math.min(availableToScroll, deltaX);
mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
invalidate();
}
} else {
awakenScrollBars();
}
}
break;
case MotionEvent.ACTION_UP:
if (mTouchState == TOUCH_STATE_SCROLLING) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
final int velocityX = (int) velocityTracker.getXVelocity();
final int screenWidth = getWidth();
final int whichScreen = (int) Math.floor((getScrollX() + (screenWidth / 2.0)) / screenWidth);
final float scrolledPos = (float) getScrollX() / screenWidth;
if (velocityX > SNAP_VELOCITY && (mCurrentScreen > (Preferences.getInstance().getEndlessScrolling() ? -1 : 0))) {
// Fling hard enough to move left.
// Don't fling across more than one screen at a time.
final int bound = scrolledPos < whichScreen ? mCurrentScreen - 1 : mCurrentScreen;
snapToScreen(Math.min(whichScreen, bound), velocityX, true);
} else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - (Preferences.getInstance().getEndlessScrolling() ? 0 : 1)) {
// Fling hard enough to move right
// Don't fling across more than one screen at a time.
final int bound = scrolledPos > whichScreen ? mCurrentScreen + 1 : mCurrentScreen;
snapToScreen(Math.max(whichScreen, bound), velocityX, true);
} else {
snapToScreen(whichScreen, 0, true);
}
}
mTouchState = TOUCH_STATE_REST;
mActivePointerId = INVALID_POINTER;
releaseVelocityTracker();
break;
case MotionEvent.ACTION_CANCEL:
if (mTouchState == TOUCH_STATE_SCROLLING) {
final int screenWidth = getWidth();
final int whichScreen = (getScrollX() + (screenWidth / 2)) / screenWidth;
snapToScreen(whichScreen, 0, true);
}
mTouchState = TOUCH_STATE_REST;
mActivePointerId = INVALID_POINTER;
releaseVelocityTracker();
break;
case MotionEvent.ACTION_POINTER_UP:
onSecondaryPointerUp(ev);
break;
}
return true;
}
Aggregations