大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android实现ViewPager边界回弹效果实例代码-创新互联

废话不多说了,直接给大家贴代码了,具体代码如下所示:

创新互联公司-专业网站定制、快速模板网站建设、高性价比南明网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式南明网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖南明地区。费用合理售后完善,10多年实体公司更值得信赖。
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边界回弹效果实例代码-创新互联
标题路径:http://dzwzjz.com/article/cocjpc.html
在线咨询
服务热线
服务热线:028-86922220
TOP