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; }
@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; }
相关文章推荐
- Android HorizontalScrollView结合GridView实现类似网易新闻那样的栏目滑动效果
- Android ScrollView HorizontalScrollView 实现全方向(上下左右)反弹效果
- 实现android左右滑动效果:ViewFlipper、ViewPager、HorizontalScrollView
- 布局动画Android ScrollView HorizontalScrollView 实现全方向(上下左右)反弹效果
- Android HorizontalScrollView左右滑动效果
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- android ViewPager加Fragment实 4000 现导航加左右滑动(类似QQ、微信)
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- Android学QQ空间相册浏览类型横向滑动效果显示多图片MyHorizontalScrollView
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- Android HorizontalScrollView左右滑动
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果