use of android.view.VelocityTracker in project android_frameworks_base by ParanoidAndroid.
the class ScrollView 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();
if (mFlingStrictSpan != null) {
mFlingStrictSpan.finish();
mFlingStrictSpan = null;
}
}
// Remember where the motion event started
mLastMotionY = (int) ev.getY();
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 y = (int) ev.getY(activePointerIndex);
int deltaY = mLastMotionY - y;
if (!mIsBeingDragged && Math.abs(deltaY) > mTouchSlop) {
final ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
mIsBeingDragged = true;
if (deltaY > 0) {
deltaY -= mTouchSlop;
} else {
deltaY += mTouchSlop;
}
}
if (mIsBeingDragged) {
// Scroll to follow the motion event
mLastMotionY = y;
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);
if (overScrollBy(0, deltaY, 0, mScrollY, 0, range, 0, mOverscrollDistance, true)) {
// Break our velocity if we hit a scroll barrier.
mVelocityTracker.clear();
}
onScrollChanged(mScrollX, mScrollY, oldX, oldY);
if (canOverscroll) {
final int pulledToY = oldY + deltaY;
if (pulledToY < 0) {
mEdgeGlowTop.onPull((float) deltaY / getHeight());
if (!mEdgeGlowBottom.isFinished()) {
mEdgeGlowBottom.onRelease();
}
} else if (pulledToY > range) {
mEdgeGlowBottom.onPull((float) deltaY / getHeight());
if (!mEdgeGlowTop.isFinished()) {
mEdgeGlowTop.onRelease();
}
}
if (mEdgeGlowTop != null && (!mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) {
postInvalidateOnAnimation();
}
}
}
break;
case MotionEvent.ACTION_UP:
if (mIsBeingDragged) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int initialVelocity = (int) velocityTracker.getYVelocity(mActivePointerId);
if (getChildCount() > 0) {
if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
fling(-initialVelocity);
} else {
if (mScroller.springBack(mScrollX, mScrollY, 0, 0, 0, getScrollRange())) {
postInvalidateOnAnimation();
}
}
}
mActivePointerId = INVALID_POINTER;
endDrag();
}
break;
case MotionEvent.ACTION_CANCEL:
if (mIsBeingDragged && getChildCount() > 0) {
if (mScroller.springBack(mScrollX, mScrollY, 0, 0, 0, getScrollRange())) {
postInvalidateOnAnimation();
}
mActivePointerId = INVALID_POINTER;
endDrag();
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
{
final int index = ev.getActionIndex();
mLastMotionY = (int) ev.getY(index);
mActivePointerId = ev.getPointerId(index);
break;
}
case MotionEvent.ACTION_POINTER_UP:
onSecondaryPointerUp(ev);
mLastMotionY = (int) ev.getY(ev.findPointerIndex(mActivePointerId));
break;
}
return true;
}
use of android.view.VelocityTracker in project android_frameworks_base by ParanoidAndroid.
the class VelocityTest method testInitialCondiditions.
@MediumTest
public void testInitialCondiditions() {
VelocityTracker vt = VelocityTracker.obtain();
assertNotNull(vt);
vt.recycle();
}
use of android.view.VelocityTracker in project android_frameworks_base by ParanoidAndroid.
the class VelocityTest method testDragWith2Points.
/**
* Test dragging with two points only
* (velocity must be an exact value)
*/
@MediumTest
public void testDragWith2Points() {
long t = System.currentTimeMillis();
VelocityTracker vt = VelocityTracker.obtain();
// 100px, 2 steps, 100ms => 1000px/s
drag(vt, 100, 200, 100, 200, 2, t, 100);
vt.computeCurrentVelocity(1000);
assertEquals(1000.0f, vt.getXVelocity());
assertEquals(1000.0f, vt.getYVelocity());
vt.recycle();
}
use of android.view.VelocityTracker in project android_frameworks_base by ParanoidAndroid.
the class VelocityTest method testDragDeceleration.
@MediumTest
public void testDragDeceleration() {
long t = System.currentTimeMillis();
VelocityTracker vt = VelocityTracker.obtain();
drag(vt, 100, 200, 100, 200, 15, t, 400, new DecelerateInterpolator());
vt.computeCurrentVelocity(1000);
assertLower(250.0f, vt.getXVelocity());
assertLower(250.0f, vt.getYVelocity());
vt.recycle();
}
use of android.view.VelocityTracker in project android_frameworks_base by ParanoidAndroid.
the class VelocityTest method testStabilityInNbPoints.
/**
* Velocity is independent of the number of points used during
* the same interval
*/
@MediumTest
public void testStabilityInNbPoints() {
long t = System.currentTimeMillis();
VelocityTracker vt = VelocityTracker.obtain();
// 10 steps over 400ms
drag(vt, 100, 200, 100, 200, 10, t, 400);
vt.computeCurrentVelocity(1);
float firstX = vt.getXVelocity();
float firstY = vt.getYVelocity();
vt.clear();
// 20 steps over 400ms
drag(vt, 100, 200, 100, 200, 20, t, 400);
vt.computeCurrentVelocity(1);
float secondX = vt.getXVelocity();
float secondY = vt.getYVelocity();
assertEqualFuzzy(firstX, secondX, 0.1f);
assertEqualFuzzy(firstY, secondY, 0.1f);
vt.recycle();
}
Aggregations