自定义View实现一个弹性滑动的效果,供大家参考,具体内容如下
实现原理
onMeasure()中测量所有子View
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 测量所有子View int count = getChildCount(); for (int i = 0; i < count; i++) { View childView = getChildAt(i); measureChild(childView, widthMeasureSpec, heightMeasureSpec); } setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); }
onLayout()中,将所有的子View按照位置依次往下排列
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // 设置ViewGroup的高度,对所有子View进行排列 int childCount = getChildCount(); MarginLayoutParams params = (MarginLayoutParams) getLayoutParams(); params.height = mScreenHeight * childCount; for (int i = 0; i < childCount; i++) { View childView = getChildAt(i); if (childView.getVisibility() != View.GONE) { // 给每个ChildView放置在指定位置 childView.layout(l, i * mScreenHeight, r, (i + 1) * mScreenHeight); } } }
onTouchEvent()中处理滑动
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastY = (int) event.getY(); mStart = getScrollY(); return true; case MotionEvent.ACTION_MOVE: if (!mScroller.isFinished()) { // 终止滑动 mScroller.abortAnimation(); } int offsetY = (int) (mLastY - event.getY()); Log.d(TAG, "onTouchEvent: getScrollY: " + getScrollY()); Log.d(TAG, "onTouchEvent: offsetY " + offsetY); // 到达顶部,使用offset判断方向 if (getScrollY() + offsetY < 0) { // 当前已经滑动的 Y 位置 offsetY = 0; } // 到达底部 if (getScrollY() > getHeight() - mScreenHeight && offsetY > 0) { offsetY = 0; } scrollBy(0, offsetY); // 滑动完成后,重新设置LastY位置 mLastY = (int) event.getY(); break; case MotionEvent.ACTION_UP: mEnd = getScrollY(); int distance = mEnd - mStart; if (distance > 0) { // 向上滑动 if (distance < mScreenHeight / 3) { Log.d(TAG, "onTouchEvent: distance < screen/3"); // 回到原来位置 mScroller.startScroll(0, getScrollY(), 0, -distance); } else { // 滚到屏幕的剩余位置 mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - distance); } } else { // 向下滑动 if (-distance < mScreenHeight / 3) { mScroller.startScroll(0, getScrollY(), 0, -distance); } else { mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight - distance); } } postInvalidate(); } return super.onTouchEvent(event); }
其中ACTION_UP这段代码是处理弹性滑动的
case MotionEvent.ACTION_UP: mEnd = getScrollY(); int distance = mEnd - mStart; if (distance > 0) { // 向上滑动 if (distance < mScreenHeight / 3) { Log.d(TAG, "onTouchEvent: distance < screen/3"); // 回到原来位置 mScroller.startScroll(0, getScrollY(), 0, -distance); } else { // 滚到屏幕的剩余位置 mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - distance); } } else { // 向下滑动 if (-distance < mScreenHeight / 3) { mScroller.startScroll(0, getScrollY(), 0, -distance); } else { mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight - distance); } } postInvalidate();
完整代码
public class ScrollViewGroup extends ViewGroup { private static final String TAG = "ScrollView"; private Scroller mScroller; private int mScreenHeight; // 窗口高度 private int mLastY; private int mStart; private int mEnd; public ScrollViewGroup(Context context) { this(context, null); } public ScrollViewGroup(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mScroller = new Scroller(context); // 获取屏幕高度 WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics metrics = new DisplayMetrics(); windowManager.getDefaultDisplay().getMetrics(metrics); mScreenHeight = metrics.heightPixels; Log.d(TAG, "ScrollViewGroup: ScreenHeight " + mScreenHeight); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 测量所有子View int count = getChildCount(); for (int i = 0; i < count; i++) { View childView = getChildAt(i); measureChild(childView, widthMeasureSpec, heightMeasureSpec); } setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // 设置ViewGroup的高度,对所有子View进行排列 int childCount = getChildCount(); MarginLayoutParams params = (MarginLayoutParams) getLayoutParams(); params.height = mScreenHeight * childCount; for (int i = 0; i < childCount; i++) { View childView = getChildAt(i); if (childView.getVisibility() != View.GONE) { // 给每个ChildView放置在指定位置 childView.layout(l, i * mScreenHeight, r, (i + 1) * mScreenHeight); } } } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastY = (int) event.getY(); mStart = getScrollY(); return true; case MotionEvent.ACTION_MOVE: if (!mScroller.isFinished()) { // 终止滑动 mScroller.abortAnimation(); } int offsetY = (int) (mLastY - event.getY()); Log.d(TAG, "onTouchEvent: getScrollY: " + getScrollY()); Log.d(TAG, "onTouchEvent: offsetY " + offsetY); // 到达顶部,使用offset判断方向 if (getScrollY() + offsetY < 0) { // 当前已经滑动的 Y 位置 offsetY = 0; } // 到达底部 if (getScrollY() > getHeight() - mScreenHeight && offsetY > 0) { offsetY = 0; } scrollBy(0, offsetY); // 滑动完成后,重新设置LastY位置 mLastY = (int) event.getY(); break; case MotionEvent.ACTION_UP: mEnd = getScrollY(); int distance = mEnd - mStart; if (distance > 0) { // 向上滑动 if (distance < mScreenHeight / 3) { Log.d(TAG, "onTouchEvent: distance < screen/3"); // 回到原来位置 mScroller.startScroll(0, getScrollY(), 0, -distance); } else { // 滚到屏幕的剩余位置 mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - distance); } } else { // 向下滑动 if (-distance < mScreenHeight / 3) { mScroller.startScroll(0, getScrollY(), 0, -distance); } else { mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight - distance); } } postInvalidate(); } return super.onTouchEvent(event); } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } } }
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔