您好,登錄后才能下訂單哦!
在Android中,實現(xiàn)滑動控件的滑動慣性效果通常使用Scroller
類
ViewGroup
(例如LinearLayout
、RelativeLayout
等)。public class InertiaScrollView extends LinearLayout {
// ...
}
Scroller
對象和其他相關(guān)變量。private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private int mTouchSlop;
private float mLastMotionX;
private float mLastMotionY;
public InertiaScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mScroller = new Scroller(context);
ViewConfiguration config = ViewConfiguration.get(context);
mTouchSlop = config.getScaledTouchSlop();
}
onInterceptTouchEvent
方法,攔截觸摸事件。@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
return true;
}
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_MOVE: {
final float x = ev.getX();
final float y = ev.getY();
final int xDiff = (int) Math.abs(x - mLastMotionX);
final int yDiff = (int) Math.abs(y - mLastMotionY);
if (yDiff > mTouchSlop) {
mIsBeingDragged = true;
mLastMotionY = y;
}
break;
}
case MotionEvent.ACTION_DOWN: {
mIsBeingDragged = false;
mLastMotionX = ev.getX();
mLastMotionY = ev.getY();
break;
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: {
mIsBeingDragged = false;
break;
}
}
return mIsBeingDragged;
}
onTouchEvent
方法,處理觸摸事件。@Override
public boolean onTouchEvent(MotionEvent event) {
initVelocityTrackerIfNotExists();
mVelocityTracker.addMovement(event);
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN: {
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
mLastMotionX = event.getX();
mLastMotionY = event.getY();
break;
}
case MotionEvent.ACTION_MOVE: {
final float x = event.getX();
final float y = event.getY();
final int deltaX = (int) (mLastMotionX - x);
final int deltaY = (int) (mLastMotionY - y);
mLastMotionX = x;
mLastMotionY = y;
scrollBy(0, deltaY);
break;
}
case MotionEvent.ACTION_UP: {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000);
int initialVelocity = (int) velocityTracker.getYVelocity();
if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
fling(-initialVelocity);
}
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
break;
}
case MotionEvent.ACTION_CANCEL: {
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
break;
}
}
return true;
}
fling
方法,用于處理慣性滾動。private void fling(int velocityY) {
mScroller.fling(0, getScrollY(), 0, velocityY, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
invalidate();
}
computeScroll
方法,用于計算滾動偏移。@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
int oldY = getScrollY();
int y = mScroller.getCurrY();
if (oldY != y) {
scrollTo(0, y);
}
postInvalidate();
}
}
onDetachedFromWindow
方法中釋放VelocityTracker
對象。@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
}
現(xiàn)在,你已經(jīng)實現(xiàn)了一個具有滑動慣性效果的自定義滑動控件。你可以根據(jù)需要對其進行進一步的優(yōu)化和擴展。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。