您的位置:首页 > 移动开发 > 微信开发

android 滚动条HorizontalScrollView左右滑动反弹的效果(类似微信朋友圈下拉反弹)...

2016-06-07 15:02 531 查看
最近一个项目顶部 是新闻频道 左右滑动时 要出现留出空白 并且反弹的效果 类似:http://blog.csdn.net/qq_16064871/article/details/50224635中的下拉反弹。 不过我的是左右滑动的,具体代码如下: @Override protected void onFinishInflate() { if (getChildCount() > 0) { inner = getChildAt(0); } }
@Override public boolean onTouchEvent(MotionEvent ev) { if (inner != null) { commOnTouchEvent(ev); }
return super.onTouchEvent(ev); }
public void commOnTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: // 手指松开. if (isNeedAnimation()) { animation(); isCount = false; } break; case MotionEvent.ACTION_MOVE: final float preX = x;// 按下时的x坐标 float nowX = ev.getX();// 时时x坐标 int deltaX = (int) (preX - nowX) / 2;// 滑动距离 if (!isCount) { deltaX = 0; // 在这里要归0. }
x = nowX; // 当滚动到最右或者最左时就不会再滚动,这时移动布局 if (isNeedMove()) { // 初始化头部矩形 if (normal.isEmpty()) { // 保存正常的布局位置 normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom()); } // Log.e("jj", "矩形:" + inner.getLeft() + "," + inner.getTop() // + "," + inner.getRight() + "," + inner.getBottom()); // 移动布局 inner.layout(inner.getLeft() - deltaX / 2, inner.getTop(), inner.getRight() - deltaX / 2, inner.getBottom()); } isCount = true; break;
default: break; } }
public void animation() { // 开启移动动画 TranslateAnimation ta = new TranslateAnimation(inner.getLeft(), normal.left, 0, 0); ta.setDuration(200); inner.startAnimation(ta); // 设置回到正常的布局位置 inner.layout(normal.left, normal.top, normal.right, normal.bottom);
// Log.e("jj", "回归:" + normal.left + "," + normal.top + "," + // normal.right // + "," + normal.bottom);
normal.setEmpty();
}
// 是否需要开启动画 public boolean isNeedAnimation() { return !normal.isEmpty(); }
public boolean isNeedMove() { int offset = inner.getMeasuredWidth() - getWidth(); int scrollX = getScrollX(); // Log.e("jj", "scrolly=" + scrollY); // 0是最右边,后面那个是最左边 if (scrollX == 0 || scrollX == offset) { return true; } return false; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐