安卓-自定義控制元件Scrollview
一、自定義的空間通過繼承ViewGroup來實現
二、scrollview的基礎條件
1、基礎條件scrollview需要的有:容器的大小,可視介面的大小,每個item的大小
這裡定義一個item為整個view的大小,所以在initView的時候進行獲取螢幕的高度
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); mScreenHeight= dm.heightPixels;
這裡的mScreenHeight儲存了螢幕的高度
關於每個item的尺寸測量,則需要在onMeasure中執行,如下所示
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int count = getChildCount(); for (int i = 0; i < count; ++i) { View childView= getChildAt(i); measureChild(childView, widthMeasureSpec, heightMeasureSpec); } }
這裡將寬和高的測量設定到了ViewGroup的子項,即每個scrollview的item
容器裡面有多個item了,但是每個item該如何排列則需要在layout中進行設定(注意:scrollview並不會幫你講每個item的位置設定好)
@Override protected void onLayout(boolean b, int l, int i1, inti2, int i3) { int childCount = getChildCount(); MarginLayoutParams lp = (MarginLayoutParams)getLayoutParams(); lp.height = childCount * mScreenHeight; setLayoutParams(lp); // 要設定scrollview的容器的高度,注意這裡不是view的高度,而是所有容器排列在一起的高度 for (int i = 0; i < childCount; ++i) { View child = getChildAt(i); if (child.getVisibility() != View.GONE) { child.layout(l, i * mScreenHeight, i2, (i + 1) * mScreenHeight); // 這裡每個item從上往下進行排列,每個item佔據一個mScreenHeight的高度 } } }
2、容器的內容
這裡只是基礎的操作,所以用最簡單的xml來設定,如下的配置所示
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.agiledeveloper.systemwidget.MyScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/test1" /> <ImageView android:id="@+id/imageView2" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/test2" /> <ImageView android:id="@+id/imageView3" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/test3" /> <ImageView android:id="@+id/imageView4" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/test4" /> </com.agiledeveloper.systemwidget.MyScrollView> </LinearLayout>View Code
到這裡scrollview已經能夠跑起來了,但是咱們如果要加點回彈的效果則需要做額外的操作
三、Scroller的使用
我們在上面initView函式中增加mScroller = new Scroller(context);
這裡mScroller是用來記錄和設定當前的滾動位置和狀態(注意:scroller只是記錄和設定沒有參與渲染操作)類似於MVC的model
Scroller的幾個比較常用的函式是:
startScroll(int startX, int startY, int dx, int dy) 將當前的scroller的滑動從(startX, startY)滑動到 (startX+dx, startY+dy) (注意:跟具體的ScrollView沒啥關係,只是一個記錄資料的動作,具體要滾動要呼叫scrollview的scrollTo或者scrollBy)
isFinished()和abortAnimation() 這裡的isFinished判斷是否完成動畫的滑動,abortAnimation是停止動畫的滑動,直接將最終的位置設定進來,具體可以看abortAnimation的原始碼
public void abortAnimation() { mCurrX = mFinalX; mCurrY = mFinalY; mFinished = true; }
computeScrollOffset() 該函式是判斷當前的滾動動畫是否還在繼續
四、computeScroll的繼承
我們打算讓滾動有回彈的效果可以重寫computeScroll,具體程式碼如下所示
@Override public void computeScroll() { super.computeScroll(); if (mScroller.computeScrollOffset()) { scrollTo(0, mScroller.getCurrY()); postInvalidate(); } }
這裡判斷當前如果動畫會在繼續就呼叫scrollTo滾動到mScroller當前的指定位置
關於computeScroll和mScroller的說明可以看這篇文章https://www.freesion.com/article/7912960070/
五、完整的程式碼如下所示:
package com.agiledeveloper.systemwidget; import android.content.Context; import android.os.Debug; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.Scroller; public class MyScrollView extends ViewGroup { public MyScrollView(Context context) { super(context); initView(context); } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); initView(context); } private int mScreenHeight; private Scroller mScroller; private void initView(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); mScreenHeight = dm.heightPixels; mScroller = new Scroller(context); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int count = getChildCount(); for (int i = 0; i < count; ++i) { View childView = getChildAt(i); measureChild(childView, widthMeasureSpec, heightMeasureSpec); } } @Override protected void onLayout(boolean b, int l, int i1, int i2, int i3) { int childCount = getChildCount(); MarginLayoutParams lp = (MarginLayoutParams)getLayoutParams(); lp.height = childCount * mScreenHeight; setLayoutParams(lp); for (int i = 0; i < childCount; ++i) { View child = getChildAt(i); if (child.getVisibility() != View.GONE) { child.layout(l, i * mScreenHeight, i2, (i + 1) * mScreenHeight); } } } private int mLastY = 0; private int mStartY = 0; @Override public boolean onTouchEvent(MotionEvent event) { int y = (int)event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastY = y; mStartY = getScrollY(); break; case MotionEvent.ACTION_MOVE: if (!mScroller.isFinished()) { mScroller.abortAnimation(); } int dy = mLastY - y; if (getScrollY() < 0) { dy = 0; } if (getScrollY() > (getChildCount() - 1) * mScreenHeight) { dy = 0; } scrollBy(0, dy); mLastY = y; break; case MotionEvent.ACTION_UP: int dScrollY = checkAlignment(); if (dScrollY > 0) { if (dScrollY < mScreenHeight / 3) { mScroller.startScroll(0, getScrollY(), 0, -dScrollY); } else { mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - dScrollY); } } else { if (-dScrollY < mScreenHeight / 3) { mScroller.startScroll(0, getScrollY(), 0, -dScrollY); } else { mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight - dScrollY); } } break; } postInvalidate(); return true; } private int checkAlignment() { int mEnd = getScrollY(); boolean isUp = (mEnd - mStartY) > 0? true : false; int lastPrev = mEnd % mScreenHeight; int lastNext = mScreenHeight - lastPrev; if (isUp) { return lastPrev; } else { return -lastNext; } } @Override public void computeScroll() { super.computeScroll(); if (mScroller.computeScrollOffset()) { scrollTo(0, mScroller.getCurrY()); postInvalidate(); } } }View Code