您好,登錄后才能下訂單哦!
本篇內容主要講解“Android中如何通過自定義ViewGroup實現一個彈性滑動效果”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Android中如何通過自定義ViewGroup實現一個彈性滑動效果”吧!
實現原理
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(); } } }
到此,相信大家對“Android中如何通過自定義ViewGroup實現一個彈性滑動效果”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。