本文整理汇总了Java中android.view.VelocityTracker.computeCurrentVelocity方法的典型用法代码示例。如果您正苦于以下问题:Java VelocityTracker.computeCurrentVelocity方法的具体用法?Java VelocityTracker.computeCurrentVelocity怎么用?Java VelocityTracker.computeCurrentVelocity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.view.VelocityTracker
的用法示例。
在下文中一共展示了VelocityTracker.computeCurrentVelocity方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: velocityTrackerPointerUpCleanUpIfNecessary
import android.view.VelocityTracker; //导入方法依赖的package包/类
public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev,
VelocityTracker tracker) {
// Check the dot product of current velocities.
// If the pointer that left was opposing another velocity vector, clear.
tracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
final int upIndex = ev.getActionIndex();
final int id1 = ev.getPointerId(upIndex);
final float x1 = tracker.getXVelocity(id1);
final float y1 = tracker.getYVelocity(id1);
for (int i = 0, count = ev.getPointerCount(); i < count; i++) {
if (i == upIndex)
continue;
final int id2 = ev.getPointerId(i);
final float x = x1 * tracker.getXVelocity(id2);
final float y = y1 * tracker.getYVelocity(id2);
final float dot = x + y;
if (dot < 0) {
tracker.clear();
break;
}
}
}
示例2: endFakeDragHorizontally
import android.view.VelocityTracker; //导入方法依赖的package包/类
private void endFakeDragHorizontally() {
if (mAdapter != null) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
velocityTracker, mActivePointerId);
mPopulatePending = true;
final int width = getClientWidth();
final int scrollX = getScrollX();
final ItemInfo ii = infoForCurrentScrollPosition();
final int currentPage = ii.position;
final float pageOffset = (((float) scrollX / width) - ii.offset) / ii.widthFactor;
final int totalDelta = (int) (mLastMotionX - mInitialMotionX);
int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
totalDelta);
setCurrentItemInternal(nextPage, true, true, initialVelocity);
}
endDrag();
mFakeDragging = false;
}
示例3: run
import android.view.VelocityTracker; //导入方法依赖的package包/类
@Override
public void run() {
final int activeId = mActivePointerId;
final VelocityTracker vt = mVelocityTracker;
final OverScroller scroller = mScroller;
if (vt == null || activeId == INVALID_POINTER) {
return;
}
vt.computeCurrentVelocity(1000, mMaximumVelocity);
final float xvel = -vt.getXVelocity(activeId);
if (Math.abs(xvel) >= mMinimumVelocity
&& scroller.isScrollingInDirection(xvel, 0)) {
// Keep the fling alive a little longer
postDelayed(this, FLYWHEEL_TIMEOUT);
} else {
endFling();
mTouchMode = TOUCH_MODE_SCROLL;
reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
}
}
示例4: getCurrentState
import android.view.VelocityTracker; //导入方法依赖的package包/类
@Override
public int getCurrentState(View selectedView, View targetView, int x, int y,
VelocityTracker velocityTracker, int selectedPosition,
int targetPosition) {
if(velocityTracker == null) return ClassifyView.MOVE_STATE_NONE;
int left = x;
int top = y;
int right = left + selectedView.getWidth();
int bottom = top + selectedView.getHeight();
if((Math.abs(left - targetView.getLeft())+Math.abs(right - targetView.getRight())+
Math.abs(top - targetView.getTop())+ Math.abs(bottom - targetView.getBottom()))
<(targetView.getWidth()+targetView.getHeight()
)/2){
velocityTracker.computeCurrentVelocity(100);
float xVelocity = velocityTracker.getXVelocity();
float yVelocity = velocityTracker.getYVelocity();
float limit = getVelocity(targetView.getContext());
if(xVelocity < limit && yVelocity < limit){
return ClassifyView.MOVE_STATE_MOVE;
}
}
return ClassifyView.MOVE_STATE_NONE;
}
示例5: endFakeDrag
import android.view.VelocityTracker; //导入方法依赖的package包/类
/**
* End a fake drawable_drag of the pager.
*
* @see #beginFakeDrag()
* @see #fakeDragBy(float)
*/
public void endFakeDrag() {
if (!mFakeDragging) {
throw new IllegalStateException("No fake drawable_drag in progress. Call beginFakeDrag first.");
}
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
velocityTracker, mActivePointerId);
final int width = getClientWidth();
final int scrollX = getScrollX();
final ItemInfo ii = infoForCurrentScrollPosition();
final int currentPage = ii.position;
final float pageOffset = (((float) scrollX / width) - ii.offset) / ii.widthFactor;
final int totalDelta = (int) (mLastMotionX - mInitialMotionX);
int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
totalDelta);
setCurrentItemInternal(nextPage, true, true, initialVelocity);
endDrag();
mFakeDragging = false;
}
示例6: getXVelocity
import android.view.VelocityTracker; //导入方法依赖的package包/类
private float getXVelocity() {
float xVelocity = 0;
Class viewpagerClass = ViewPager.class;
try {
Field velocityTrackerField = viewpagerClass.getDeclaredField("mVelocityTracker");
velocityTrackerField.setAccessible(true);
VelocityTracker velocityTracker = (VelocityTracker) velocityTrackerField.get(this);
Field activePointerIdField = viewpagerClass.getDeclaredField("mActivePointerId");
activePointerIdField.setAccessible(true);
Field maximumVelocityField = viewpagerClass.getDeclaredField("mMaximumVelocity");
maximumVelocityField.setAccessible(true);
int maximumVelocity = maximumVelocityField.getInt(this);
velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
xVelocity = VelocityTrackerCompat.getXVelocity(velocityTracker, activePointerIdField.getInt(this));
} catch (Exception e) {
}
return xVelocity;
}
示例7: endFakeDrag
import android.view.VelocityTracker; //导入方法依赖的package包/类
/**
* End a fake drag of the pager.
*
* @see #beginFakeDrag()
* @see #fakeDragBy(float)
*/
public void endFakeDrag() {
if (!mFakeDragging) {
throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
}
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
velocityTracker, mActivePointerId);
mPopulatePending = true;
final int width = getClientWidth();
final int scrollX = getScrollX();
final ItemInfo ii = infoForCurrentScrollPosition();
final int currentPage = ii.position;
final float pageOffset = (((float) scrollX / width) - ii.offset) / ii.widthFactor;
final int totalDelta = (int) (mLastMotionX - mInitialMotionX);
int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
totalDelta);
setCurrentItemInternal(nextPage, true, true, initialVelocity);
endDrag();
mFakeDragging = false;
}
示例8: endFakeDrag
import android.view.VelocityTracker; //导入方法依赖的package包/类
/**
* End a fake drag of the pager.
*
* @see #beginFakeDrag()
* @see #fakeDragBy(float)
*/
public void endFakeDrag() {
if (!mFakeDragging) {
throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
}
if (mAdapter != null) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
velocityTracker, mActivePointerId);
mPopulatePending = true;
final int width = getClientWidth();
final int scrollX = getScrollX();
final ViewPager.ItemInfo ii = infoForCurrentScrollPosition();
final int currentPage = ii.position;
final float pageOffset = (((float) scrollX / width) - ii.offset) / ii.widthFactor;
final int totalDelta = (int) (mLastMotionX - mInitialMotionX);
int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity,
totalDelta);
setCurrentItemInternal(nextPage, true, true, initialVelocity);
}
endDrag();
mFakeDragging = false;
}
示例9: endFakeDrag
import android.view.VelocityTracker; //导入方法依赖的package包/类
/**
* End a fake drag of the pager.
*
* @see #beginFakeDrag()
* @see #fakeDragBy(float)
*/
public void endFakeDrag() {
if (!mFakeDragging) {
throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
}
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker,
mActivePointerId);
mPopulatePending = true;
final int width = getClientWidth();
final int scrollX = getScrollX();
final ItemInfo ii = infoForCurrentScrollPosition();
final int currentPage = ii.position;
final float pageOffset = (((float) scrollX / width) - ii.offset) / ii.widthFactor;
final int totalDelta = (int) (mLastMotionX - mInitialMotionX);
int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
setCurrentItemInternal(nextPage, true, true, initialVelocity);
endDrag();
mFakeDragging = false;
}
示例10: endFakeDrag
import android.view.VelocityTracker; //导入方法依赖的package包/类
public void endFakeDrag() {
if (this.mFakeDragging) {
if (this.mAdapter != null) {
VelocityTracker velocityTracker = this.mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, (float) this.mMaximumVelocity);
int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, this.mActivePointerId);
this.mPopulatePending = true;
int width = getClientWidth();
int scrollX = getScrollX();
ItemInfo ii = infoForCurrentScrollPosition();
setCurrentItemInternal(determineTargetPage(ii.position, ((((float) scrollX) / ((float) width)) - ii.offset) / ii.widthFactor, initialVelocity, (int) (this.mLastMotionX - this.mInitialMotionX)), true, true, initialVelocity);
}
endDrag();
this.mFakeDragging = false;
return;
}
throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
}
示例11: getCurrentState
import android.view.VelocityTracker; //导入方法依赖的package包/类
@Override
public int getCurrentState(View selectedView, View targetView, int x, int y,
VelocityTracker velocityTracker, int selectedPosition,
int targetPosition) {
if (velocityTracker == null) return ClassifyView.MOVE_STATE_NONE;
int left = x;
int top = y;
int right = left + selectedView.getWidth();
int bottom = top + selectedView.getHeight();
if (canMergeItem(selectedPosition, targetPosition)) {
if ((Math.abs(left - targetView.getLeft()) + Math.abs(right - targetView.getRight()) +
Math.abs(top - targetView.getTop()) + Math.abs(bottom - targetView.getBottom()))
< (targetView.getWidth() + targetView.getHeight()
) / 3) {
return ClassifyView.MOVE_STATE_MERGE;
}
}
if ((Math.abs(left - targetView.getLeft()) + Math.abs(right - targetView.getRight()) +
Math.abs(top - targetView.getTop()) + Math.abs(bottom - targetView.getBottom()))
< (targetView.getWidth() + targetView.getHeight()
) / 2) {
velocityTracker.computeCurrentVelocity(100);
float xVelocity = velocityTracker.getXVelocity();
float yVelocity = velocityTracker.getYVelocity();
float limit = getVelocity(targetView.getContext());
if (xVelocity < limit && yVelocity < limit) {
return ClassifyView.MOVE_STATE_MOVE;
}
}
return ClassifyView.MOVE_STATE_NONE;
}
示例12: checkOnFlingGesture
import android.view.VelocityTracker; //导入方法依赖的package包/类
private boolean checkOnFlingGesture(MotionEvent ev) {
final VelocityTracker velocityTracker = this.velocityTracker;
final int pointerId = ev.getPointerId(0);
velocityTracker.computeCurrentVelocity(1000, MAX_FLING_VELOCITY);
final float velocityY = velocityTracker.getYVelocity(pointerId);
final float velocityX = velocityTracker.getXVelocity(pointerId);
if ((Math.abs(velocityY) >= MIN_FLING_VELOCITY) || (Math.abs(velocityX) >= MIN_FLING_VELOCITY)) {
return gestureListener.onFling(currentDownEvent, ev, velocityX, velocityY);
} else {
return false;
}
}
示例13: onTouchEvent
import android.view.VelocityTracker; //导入方法依赖的package包/类
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent ev) {
final int action = ev.getActionMasked();
final int actionIndex = ev.getActionIndex();
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(ev);
switch (action){
case MotionEvent.ACTION_POINTER_DOWN:
mActivePointerId = ev.getPointerId(actionIndex);
mLastMotionX = ev.getX(actionIndex);
mLastMotionY = ev.getY(actionIndex);
break;
case MotionEvent.ACTION_POINTER_UP:
final int pointerId = ev.getPointerId(actionIndex);
if(pointerId==mActivePointerId){
final int newIndex = actionIndex == 0 ? 1 : 0;
mActivePointerId = ev.getPointerId(newIndex);
mLastMotionX = ev.getX(newIndex);
mLastMotionY = ev.getY(newIndex);
}
break;
//down时,已经将capture item定下来了。所以,后面可以安心考虑event处理
case MotionEvent.ACTION_MOVE: {
final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
if (activePointerIndex == -1)
break;
final float x = ev.getX(activePointerIndex);
final float y = (int) ev.getY(activePointerIndex);
int deltaX = (int) (x - mLastMotionX);
if(mCaptureItem!=null && mCaptureItem.getTouchMode()== Mode.DRAG){
mLastMotionX = x;
mLastMotionY = y;
//对capture item进行拖拽
mCaptureItem.trackMotionScroll(deltaX);
}
break;
}
case MotionEvent.ACTION_UP:
if(mCaptureItem!=null){
Mode touchMode = mCaptureItem.getTouchMode();
if(touchMode== Mode.DRAG){
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int xVel = (int) velocityTracker.getXVelocity(mActivePointerId);
mCaptureItem.fling(xVel);
}
}
cancel();
break;
case MotionEvent.ACTION_CANCEL:
if(mCaptureItem!=null)
mCaptureItem.revise();
cancel();
break;
}
}
示例14: onTouchEvent
import android.view.VelocityTracker; //导入方法依赖的package包/类
@Override
public boolean onTouchEvent(MotionEvent event) {
if(mItemHeight == 0) return true;
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
currY = event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
mFlagMayPress = true;
mHandlerInNewThread.removeMessages(HANDLER_WHAT_REFRESH);
stopScrolling();
downY = currY;
downYGlobal = mCurrDrawGlobalY;
onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
float spanY = downY - currY;
if(mFlagMayPress && (-mScaledTouchSlop < spanY && spanY < mScaledTouchSlop)){
}else{
mFlagMayPress = false;
mCurrDrawGlobalY = limitY((int)(downYGlobal + spanY));
calculateFirstItemParameterByGlobalY();
invalidate();
}
onScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
break;
case MotionEvent.ACTION_UP:
if(mFlagMayPress){
click(event);
}else {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000);
int velocityY = (int) (velocityTracker.getYVelocity() * mFriction);
if (Math.abs(velocityY) > mMiniVelocityFling) {
mScroller.fling(0, mCurrDrawGlobalY, 0, -velocityY,
Integer.MIN_VALUE, Integer.MAX_VALUE, limitY(Integer.MIN_VALUE), limitY(Integer.MAX_VALUE));
invalidate();
onScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
}
mHandlerInNewThread.sendMessageDelayed(getMsg(HANDLER_WHAT_REFRESH), 0);
releaseVelocityTracker();
}
break;
case MotionEvent.ACTION_CANCEL:
downYGlobal = mCurrDrawGlobalY;
stopScrolling();
mHandlerInNewThread.sendMessageDelayed(getMsg(HANDLER_WHAT_REFRESH), 0);
break;
}
return true ;
}
示例15: onTouchEvent
import android.view.VelocityTracker; //导入方法依赖的package包/类
@Override
public boolean onTouchEvent(MotionEvent ev) {
initVelocityTrackerIfNotExists();
mVelocityTracker.addMovement(ev);
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mIsBeingDragged = false;
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) {
mIsBeingDragged = true;
if (deltaY > 0) {
deltaY -= mTouchSlop;
} else {
deltaY += mTouchSlop;
}
}
if (mIsBeingDragged) {
// Scroll to follow the motion event
mLastMotionY = y;
mFlexibleEffect.onPull((float) deltaY / getHeight());
if(!mFlexibleEffect.isFinished()){
return true;
}
}
break;
case MotionEvent.ACTION_UP:
if (mIsBeingDragged) {
mActivePointerId = INVALID_POINTER;
mIsBeingDragged = false;
if (mFlexibleEffect != null&&mFlexibleEffect.isPulling()) {
mFlexibleEffect.onAbsorb(-1);
}
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
currentVelocity = mVelocityTracker.getYVelocity(mActivePointerId);
Log.d(TAG, "currentVelocity:"+mVelocityTracker.getYVelocity(mActivePointerId) );
recycleVelocityTracker();
}
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return super.onTouchEvent(ev);
}