Android ViewPager 경계 리 턴 효과 인 스 턴 스 코드 구현

잔말 말고 바로 코드 를 붙 여 드 리 겠 습 니 다.구체 적 인 코드 는 다음 과 같 습 니 다.

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;
 }
}
위 에서 말 한 것 은 소 편 이 소개 한 안 드 로 이 드 가 ViewPager 경계 리 턴 효 과 를 실현 하 는 인 스 턴 스 코드 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.소 편 은 제때에 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기