Java Code Examples for android.view.VelocityTracker#obtain()

The following examples show how to use android.view.VelocityTracker#obtain() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LazyViewPager.java    From AndroidBase with Apache License 2.0 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager
 * with the touch scrolling of another view, while still letting the ViewPager
 * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.)
 * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call
 * {@link #endFakeDrag()} to complete the fake drag and fling as necessary.
 *
 * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag
 * is already in progress, this method will return false.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 *
 * @see #fakeDragBy(float)
 * @see #endFakeDrag()
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    mInitialMotionX = mLastMotionX = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example 2
Source File: DirectionalViewpager.java    From fangzhuishushenqi with Apache License 2.0 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * @return true if the fake drag began successfully, false if it could not be started.
 */
public boolean beginFakeDrag() {
    if (mIsBeingDragged) {
        return false;
    }
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    if (isHorizontal()) {
        mInitialMotionX = mLastMotionX = 0;
    } else {
        mInitialMotionY = mLastMotionY = 0;
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
    mFakeDragBeginTime = time;
    return true;
}
 
Example 3
Source File: imgSlider.java    From Android-Music-Player with MIT License 5 votes vote down vote up
public imgSlider(Context context, int width, int height) {
    super(context, width, height);
    setBackgroundColor(itemBack.Color0);
    Vx = VelocityTracker.obtain();

    btm = creatImg();
    addView(btm);

    top = creatImg();
    addView(top);

    FMTop = new FMView(getContext(),width,height);
    FMTop.setBackgroundColor(0x66000000);
    FMTop.setClickable(false);
    addView(FMTop);

    Ui.ef.playerEvent.addEvent(new EventCall(new int[]{playerEvents.SONG_CHANGED,playerEvents.PLAYLIST_CHANGED, Ui.ef.Event_onBind}){
        @Override
        public void onCall(int eventId) {
            if(eventId == playerEvents.SONG_CHANGED){
                songChanged();
            }

            if(eventId == Ui.ef.Event_onBind || playerEvents.PLAYLIST_CHANGED == eventId){
                if(Ui.ef.MusicPlayer.handler.list!= null){
                    songChanged();
                }
            }
        }
    });
}
 
Example 4
Source File: ItemTouchHelper.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("WeakerAccess") /* synthetic access */
void obtainVelocityTracker() {
    if (mVelocityTracker != null) {
        mVelocityTracker.recycle();
    }
    mVelocityTracker = VelocityTracker.obtain();
}
 
Example 5
Source File: RefreshLayout.java    From SimpleProject with MIT License 5 votes vote down vote up
private void initView(Context context) {
	setOrientation(VERTICAL);
	mRefreshStatus = REFRESH_STATUS_NONE;
	mScroller = new Scroller(context);
	mScreenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
	mVelocityTracker = VelocityTracker.obtain();
	ViewConfiguration configuration = ViewConfiguration.get(context);
	mMinVelocity = configuration.getScaledMinimumFlingVelocity();
}
 
Example 6
Source File: WheelView.java    From WheelView with Apache License 2.0 5 votes vote down vote up
private void startWheelDrag(MotionEvent event, float x, float y) {
    mIsDraggingWheel = true;
    mDraggedAngle = 0f;

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    }
    mVelocityTracker.addMovement(event);

    mAngularVelocity = 0f;
    mLastTouchAngle = mWheelBounds.angleToDegrees(x, y);
}
 
Example 7
Source File: DragController.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
/**
 * Used to create a new DragLayer from XML.
 *
 * @param context The application's context.
 */
public DragController(Launcher launcher) {
    Resources r = launcher.getResources();
    mLauncher = launcher;
    mHandler = new Handler();
    mScrollZone = r.getDimensionPixelSize(R.dimen.scroll_zone);
    mVelocityTracker = VelocityTracker.obtain();

    float density = r.getDisplayMetrics().density;
    mFlingToDeleteThresholdVelocity =
            (int) (r.getInteger(R.integer.config_flingToDeleteMinVelocity) * density);
}
 
Example 8
Source File: LWebView.java    From ELinkageScroll with Apache License 2.0 4 votes vote down vote up
private void initVelocityTrackerIfNotExists() {
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
}
 
Example 9
Source File: CupcakeGestureDetector.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            mVelocityTracker = VelocityTracker.obtain();
            if (null != mVelocityTracker) {
                mVelocityTracker.addMovement(ev);
            } else {
                Log.i(LOG_TAG, "Velocity tracker is null");
            }

            mLastTouchX = getActiveX(ev);
            mLastTouchY = getActiveY(ev);
            mIsDragging = false;
            break;
        }

        case MotionEvent.ACTION_MOVE: {
            final float x = getActiveX(ev);
            final float y = getActiveY(ev);
            final float dx = x - mLastTouchX, dy = y - mLastTouchY;

            if (!mIsDragging) {
                // Use Pythagoras to see if drag length is larger than
                // touch slop
                mIsDragging = FloatMath.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
            }

            if (mIsDragging) {
                mListener.onDrag(dx, dy);
                mLastTouchX = x;
                mLastTouchY = y;

                if (null != mVelocityTracker) {
                    mVelocityTracker.addMovement(ev);
                }
            }
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            // Recycle Velocity Tracker
            if (null != mVelocityTracker) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            break;
        }

        case MotionEvent.ACTION_UP: {
            if (mIsDragging) {
                if (null != mVelocityTracker) {
                    mLastTouchX = getActiveX(ev);
                    mLastTouchY = getActiveY(ev);

                    // Compute velocity within the last 1000ms
                    mVelocityTracker.addMovement(ev);
                    mVelocityTracker.computeCurrentVelocity(1000);

                    final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker
                            .getYVelocity();

                    // If the velocity is greater than minVelocity, call
                    // listener
                    if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) {
                        mListener.onFling(mLastTouchX, mLastTouchY, -vX,
                                -vY);
                    }
                }
            }

            // Recycle Velocity Tracker
            if (null != mVelocityTracker) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            break;
        }
    }

    return true;
}
 
Example 10
Source File: CupcakeGestureDetector.java    From zen4android with MIT License 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            mVelocityTracker = VelocityTracker.obtain();
            if (null != mVelocityTracker) {
                mVelocityTracker.addMovement(ev);
            } else {
                Log.i(LOG_TAG, "Velocity tracker is null");
            }

            mLastTouchX = getActiveX(ev);
            mLastTouchY = getActiveY(ev);
            mIsDragging = false;
            break;
        }

        case MotionEvent.ACTION_MOVE: {
            final float x = getActiveX(ev);
            final float y = getActiveY(ev);
            final float dx = x - mLastTouchX, dy = y - mLastTouchY;

            if (!mIsDragging) {
                // Use Pythagoras to see if drag length is larger than
                // touch slop
                mIsDragging = FloatMath.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
            }

            if (mIsDragging) {
                mListener.onDrag(dx, dy);
                mLastTouchX = x;
                mLastTouchY = y;

                if (null != mVelocityTracker) {
                    mVelocityTracker.addMovement(ev);
                }
            }
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            // Recycle Velocity Tracker
            if (null != mVelocityTracker) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            break;
        }

        case MotionEvent.ACTION_UP: {
            if (mIsDragging) {
                if (null != mVelocityTracker) {
                    mLastTouchX = getActiveX(ev);
                    mLastTouchY = getActiveY(ev);

                    // Compute velocity within the last 1000ms
                    mVelocityTracker.addMovement(ev);
                    mVelocityTracker.computeCurrentVelocity(1000);

                    final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker
                            .getYVelocity();

                    // If the velocity is greater than minVelocity, call
                    // listener
                    if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) {
                        mListener.onFling(mLastTouchX, mLastTouchY, -vX,
                                -vY);
                    }
                }
            }

            // Recycle Velocity Tracker
            if (null != mVelocityTracker) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            break;
        }
    }

    return true;
}
 
Example 11
Source File: RuleView.java    From RulerView with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getAction();
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    logD("onTouchEvent: action=%d", action);
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mScroller.forceFinished(true);
            mDownX = x;
            isMoved = false;
            break;
        case MotionEvent.ACTION_MOVE:
            final int dx = x - mLastX;

            // 判断是否已经滑动
            if (!isMoved) {
                final int dy = y - mLastY;
                // 滑动的触发条件:水平滑动大于垂直滑动;滑动距离大于阈值
                if (Math.abs(dx) < Math.abs(dy) || Math.abs(x - mDownX) < TOUCH_SLOP) {
                    break;
                }
                isMoved = true;
            }

            mCurrentDistance += -dx;
            calculateValue();
            break;
        case MotionEvent.ACTION_UP:
            // 计算速度:使用1000ms为单位
            mVelocityTracker.computeCurrentVelocity(1000, MAX_FLING_VELOCITY);
            // 获取速度。速度有方向性,水平方向:左滑为负,右滑为正
            int xVelocity = (int) mVelocityTracker.getXVelocity();
            // 达到速度则惯性滑动,否则缓慢滑动到刻度
            if (Math.abs(xVelocity) >= MIN_FLING_VELOCITY) {
                // 速度具有方向性,需要取反
                mScroller.fling((int)mCurrentDistance, 0, -xVelocity, 0,
                        0, (int)mNumberRangeDistance, 0, 0);
                invalidate();
            } else {
                scrollToGradation();
            }
            break;
        default:
            break;
    }
    mLastX = x;
    mLastY = y;
    return true;
}
 
Example 12
Source File: ScrollPageAnim.java    From NovelReader with MIT License 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();

    // 初始化速度追踪器
    if (mVelocity == null) {
        mVelocity = VelocityTracker.obtain();
    }

    mVelocity.addMovement(event);
    // 设置触碰点
    setTouchPoint(x, y);

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            isRunning = false;
            // 设置起始点
            setStartPoint(x, y);
            // 停止动画
            abortAnim();
            break;
        case MotionEvent.ACTION_MOVE:
            mVelocity.computeCurrentVelocity(VELOCITY_DURATION);
            isRunning = true;
            // 进行刷新
            mView.postInvalidate();
            break;
        case MotionEvent.ACTION_UP:
            isRunning = false;
            // 开启动画
            startAnim();
            // 删除检测器
            mVelocity.recycle();
            mVelocity = null;
            break;

        case MotionEvent.ACTION_CANCEL:
            try {
                mVelocity.recycle(); // if velocityTracker won't be used should be recycled
                mVelocity = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
    }
    return true;
}
 
Example 13
Source File: PileLayout.java    From timecat with Apache License 2.0 4 votes vote down vote up
private void initVelocityTrackerIfNotExists() {
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
}
 
Example 14
Source File: BGAStickyNavLayout.java    From AndroidStudyDemo with GNU General Public License v2.0 4 votes vote down vote up
private void initVelocityTrackerIfNotExists() {
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
}
 
Example 15
Source File: AbsHListView.java    From Klyph with MIT License 4 votes vote down vote up
private void initVelocityTrackerIfNotExists() {
	if ( mVelocityTracker == null ) {
		mVelocityTracker = VelocityTracker.obtain();
	}
}
 
Example 16
Source File: ImageViewActivity.java    From Zom-Android-XMPP with GNU General Public License v3.0 4 votes vote down vote up
public ConditionallyEnabledViewPager(Context context) {
    super(context);
    gestureDetectorListener = new SwipeToCloseListener(context);
    gestureDetector = new GestureDetector(context, gestureDetectorListener);
    velocityTracker = VelocityTracker.obtain();
}
 
Example 17
Source File: ELinkageScrollLayout.java    From ELinkageScroll with Apache License 2.0 4 votes vote down vote up
/**
 * 初始化VelocityTracker
 */
private void initVelocityTrackerIfNotExists() {
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
}
 
Example 18
Source File: CustomGestureDetector.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean processTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            mActivePointerId = ev.getPointerId(0);

            mVelocityTracker = VelocityTracker.obtain();
            if (null != mVelocityTracker) {
                mVelocityTracker.addMovement(ev);
            }

            mLastTouchX = getActiveX(ev);
            mLastTouchY = getActiveY(ev);
            mIsDragging = false;
            break;
        case MotionEvent.ACTION_MOVE:
            final float x = getActiveX(ev);
            final float y = getActiveY(ev);
            final float dx = x - mLastTouchX, dy = y - mLastTouchY;

            if (!mIsDragging) {
                // Use Pythagoras to see if drag length is larger than
                // touch slop
                mIsDragging = Math.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
            }

            if (mIsDragging) {
                mListener.onDrag(dx, dy);
                mLastTouchX = x;
                mLastTouchY = y;

                if (null != mVelocityTracker) {
                    mVelocityTracker.addMovement(ev);
                }
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            mActivePointerId = INVALID_POINTER_ID;
            // Recycle Velocity Tracker
            if (null != mVelocityTracker) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            break;
        case MotionEvent.ACTION_UP:
            mActivePointerId = INVALID_POINTER_ID;
            if (mIsDragging) {
                if (null != mVelocityTracker) {
                    mLastTouchX = getActiveX(ev);
                    mLastTouchY = getActiveY(ev);

                    // Compute velocity within the last 1000ms
                    mVelocityTracker.addMovement(ev);
                    mVelocityTracker.computeCurrentVelocity(1000);

                    final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker
                            .getYVelocity();

                    // If the velocity is greater than minVelocity, call
                    // listener
                    if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) {
                        mListener.onFling(mLastTouchX, mLastTouchY, -vX,
                                -vY);
                    }
                }
            }

            // Recycle Velocity Tracker
            if (null != mVelocityTracker) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            break;
        case MotionEvent.ACTION_POINTER_UP:
            final int pointerIndex = Util.getPointerIndex(ev.getAction());
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mActivePointerId = ev.getPointerId(newPointerIndex);
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
            }
            break;
    }

    mActivePointerIndex = ev
            .findPointerIndex(mActivePointerId != INVALID_POINTER_ID ? mActivePointerId
                    : 0);
    return true;
}
 
Example 19
Source File: ItemTouchHelperExtension.java    From CrazyDaily with Apache License 2.0 4 votes vote down vote up
private void obtainVelocityTracker() {
    if (mVelocityTracker != null) {
        mVelocityTracker.recycle();
    }
    mVelocityTracker = VelocityTracker.obtain();
}
 
Example 20
Source File: ScrollFlowLayout.java    From FlowHelper with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (!isCanMove){
        return super.onInterceptTouchEvent(ev);
    }
    final ViewParent parent = getParent();
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (isVertical()){
                mLastPos = ev.getY();
            }else {
                mLastPos = ev.getX();
            }
            //拿到上次的down坐标
            if (isVertical()){
                mMovePos = ev.getY();
            }else {
                mMovePos = ev.getX();
            }

            if (mScroller != null && !mScroller.isFinished()) {
                mScroller.abortAnimation();
            }

            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }

            mVelocityTracker = VelocityTracker.obtain();

            mVelocityTracker.addMovement(ev);
            //如果能滚动,应该要屏蔽父控件的触摸事件
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }

            break;

        case MotionEvent.ACTION_MOVE:
            float offset;
            if (isVerticalMove()){
                offset = ev.getY() - mLastPos;
            }else{
                offset = ev.getX() - mLastPos;
            }
            if (Math.abs(offset) >= mTouchSlop) {
                if (parent != null) {
                    parent.requestDisallowInterceptTouchEvent(true);
                }

                if (mVelocityTracker == null){
                    mVelocityTracker = VelocityTracker.obtain();
                }
                mVelocityTracker.addMovement(ev);
                //由父控件接管触摸事件
                return true;
            }
            if (isVerticalMove()){
                mLastPos = ev.getY();
            }else {
                mLastPos = ev.getX();
            }
            break;

        default:
            break;
    }
    return super.onInterceptTouchEvent(ev);
}