您好,登錄后才能下訂單哦!
廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:
public class BounceBackViewPager extends ViewPager { private int currentPosition = 0; private Rect mRect = new Rect();//用來記錄初始位置 private boolean handleDefault = true; private float preX = 0f; private static final float RATIO = 0.5f;//摩擦系數 private static final float SCROLL_WIDTH = 10f; public BounceBackViewPager(Context context) { super(context); } public BounceBackViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean dispatchKeyEvent(KeyEvent event) { return super.dispatchKeyEvent(event); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { preX = ev.getX();//記錄起點 currentPosition = getCurrentItem(); } return super.onInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_UP: onTouchActionUp(); break; case MotionEvent.ACTION_MOVE: if (getAdapter().getCount() == 1) { float nowX = ev.getX(); float offset = nowX - preX; preX = nowX; if (offset > SCROLL_WIDTH) {//手指滑動的距離大于設定值 whetherConditionIsRight(offset); } else if (offset < -SCROLL_WIDTH) { whetherConditionIsRight(offset); } else if (!handleDefault) {//這種情況是已經出現緩沖區域了,手指慢慢恢復的情況 if (getLeft() + (int) (offset * RATIO) != mRect.left) { layout(getLeft() + (int) (offset * RATIO), getTop(), getRight() + (int) (offset * RATIO), getBottom()); } } } else if ((currentPosition == 0 || currentPosition == getAdapter().getCount() - 1)) { float nowX = ev.getX(); float offset = nowX - preX; preX = nowX; if (currentPosition == 0) { if (offset > SCROLL_WIDTH) {//手指滑動的距離大于設定值 whetherConditionIsRight(offset); } else if (!handleDefault) {//這種情況是已經出現緩沖區域了,手指慢慢恢復的情況 if (getLeft() + (int) (offset * RATIO) >= mRect.left) { layout(getLeft() + (int) (offset * RATIO), getTop(), getRight() + (int) (offset * RATIO), getBottom()); } } } else { if (offset < -SCROLL_WIDTH) { whetherConditionIsRight(offset); } else if (!handleDefault) { if (getRight() + (int) (offset * RATIO) <= mRect.right) { layout(getLeft() + (int) (offset * RATIO), getTop(), getRight() + (int) (offset * RATIO), getBottom()); } } } } else { handleDefault = true; } if (!handleDefault) { return true; } break; default: break; } return super.onTouchEvent(ev); } private void whetherConditionIsRight(float offset) { if (mRect.isEmpty()) { mRect.set(getLeft(), getTop(), getRight(), getBottom()); } handleDefault = false; layout(getLeft() + (int) (offset * RATIO), getTop(), getRight() + (int) (offset * RATIO), getBottom()); } private void onTouchActionUp() { if (!mRect.isEmpty()) { recoveryPosition(); } } private void recoveryPosition() { TranslateAnimation ta = new TranslateAnimation(getLeft(), mRect.left, 0, 0); ta.setDuration(300); startAnimation(ta); layout(mRect.left, mRect.top, mRect.right, mRect.bottom); mRect.setEmpty(); handleDefault = true; } }
以上所述是小編給大家介紹的Android 實現ViewPager邊界回彈效果實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。