Android 实现ViewPager边界回弹效果实例代码
程序员文章站
2023-12-04 17:09:58
废话不多说了,直接给大家贴代码了,具体代码如下所示:
public class bouncebackviewpager extends 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; } }
以上所述是小编给大家介绍的android 实现viewpager边界回弹效果实例代码,希望对大家有所帮助
上一篇: 初次使用IDEA创建maven项目的教程
推荐阅读