Android之自定义ViewGroup
概述
在寫代碼之前,我必須得問幾個問題:
1、ViewGroup的職責是啥?
ViewGroup相當于一個放置View的容器,并且我們在寫布局xml的時候,會告訴容器(凡是以layout為開頭的屬性,都是為用于告訴容器的),我們的寬度(layout_width)、高度(layout_height)、對齊方式(layout_gravity)等;當然還有margin等;于是乎,ViewGroup的職能為:給childView計算出建議的寬和高和測量模式 ;決定childView的位置;為什么只是建議的寬和高,而不是直接確定呢,別忘了childView寬和高可以設置為wrap_content,這樣只有childView才能計算出自己的寬和高。
2、View的職責是啥?
View的職責,根據測量模式和ViewGroup給出的建議的寬和高,計算出自己的寬和高;同時還有個更重要的職責是:在ViewGroup為其指定的區域內繪制自己的形態。
3、ViewGroup和LayoutParams之間的關系?
大家可以回憶一下,當在LinearLayout中寫childView的時候,可以寫layout_gravity,layout_weight屬性;在RelativeLayout中的childView有layout_centerInParent屬性,卻沒有layout_gravity,layout_weight,這是為什么呢?這是因為每個ViewGroup需要指定一個LayoutParams,用于確定支持childView支持哪些屬性,比如LinearLayout指定LinearLayout.LayoutParams等。如果大家去看LinearLayout的源碼,會發現其內部定義了LinearLayout.LayoutParams,在此類中,你可以發現weight和gravity的身影。
2、View的3種測量模式
上面提到了ViewGroup會為childView指定測量模式,下面簡單介紹下三種測量模式:
EXACTLY:表示設置了精確的值,一般當childView設置其寬、高為精確值、match_parent時,ViewGroup會將其設置為EXACTLY;
AT_MOST:表示子布局被限制在一個最大值內,一般當childView設置其寬、高為wrap_content時,ViewGroup會將其設置為AT_MOST;
UNSPECIFIED:表示子布局想要多大就多大,一般出現在AadapterView的item的heightMode中、ScrollView的childView的heightMode中;此種模式比較少見。
注:上面的每一行都有一個一般,意思上述不是絕對的,對于childView的mode的設置還會和ViewGroup的測量mode有一定的關系;當然了,這是第一篇自定義ViewGroup,而且絕大部分情況都是上面的規則,所以為了通俗易懂,暫不深入討論其他內容。
3、從API角度進行淺析
上面敘述了ViewGroup和View的職責,下面從API角度進行淺析。
View的根據ViewGroup傳人的測量值和模式,對自己寬高進行確定(onMeasure中完成),然后在onDraw中完成對自己的繪制。
ViewGroup需要給View傳入view的測量值和模式(onMeasure中完成),而且對于此ViewGroup的父布局,自己也需要在onMeasure中完成對自己寬和高的確定。此外,需要在onLayout中完成對其childView的位置的指定。
4、完整的例子
1、決定該ViewGroup的LayoutParams
對于我們這個例子,我們只需要ViewGroup能夠支持margin即可,那么我們直接使用系統的MarginLayoutParams
重寫父類的該方法,返回MarginLayoutParams的實例,這樣就為我們的ViewGroup指定了其LayoutParams為MarginLayoutParams。
onMeasure
在onMeasure中計算childView的測量值以及模式,以及設置自己的寬和高:
/** * 計算所有ChildView的寬度和高度 然后根據ChildView的計算結果,設置自己的寬和高 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { /** * 獲得此ViewGroup上級容器為其推薦的寬和高,以及計算模式 */ int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); // 計算出所有的childView的寬和高 measureChildren(widthMeasureSpec, heightMeasureSpec); /** * 記錄如果是wrap_content是設置的寬和高 */ int width = 0; int height = 0; int cCount = getChildCount(); int cWidth = 0; int cHeight = 0; MarginLayoutParams cParams = null; // 用于計算左邊兩個childView的高度 int lHeight = 0; // 用于計算右邊兩個childView的高度,最終高度取二者之間大值 int rHeight = 0; // 用于計算上邊兩個childView的寬度 int tWidth = 0; // 用于計算下面兩個childiew的寬度,最終寬度取二者之間大值 int bWidth = 0; /** * 根據childView計算的出的寬和高,以及設置的margin計算容器的寬和高,主要用于容器是warp_content時 */ for (int i = 0; i < cCount; i++) { View childView = getChildAt(i); cWidth = childView.getMeasuredWidth(); cHeight = childView.getMeasuredHeight(); cParams = (MarginLayoutParams) childView.getLayoutParams(); // 上面兩個childView if (i == 0 || i == 1) { tWidth += cWidth + cParams.leftMargin + cParams.rightMargin; } if (i == 2 || i == 3) { bWidth += cWidth + cParams.leftMargin + cParams.rightMargin; } if (i == 0 || i == 2) { lHeight += cHeight + cParams.topMargin + cParams.bottomMargin; } if (i == 1 || i == 3) { rHeight += cHeight + cParams.topMargin + cParams.bottomMargin; } } width = Math.max(tWidth, bWidth); height = Math.max(lHeight, rHeight); /** * 如果是wrap_content設置為我們計算的值 * 否則:直接設置為父容器計算的值 */ setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? sizeWidth : width, (heightMode == MeasureSpec.EXACTLY) ? sizeHeight : height); }0-14行,獲取該ViewGroup父容器為其設置的計算模式和尺寸,大多情況下,只要不是wrap_content,父容器都能正確的計算其尺寸。所以我們自己需要計算如果設置為wrap_content時的寬和高,如何計算呢?那就是通過其childView的寬和高來進行計算。
17行,通過ViewGroup的measureChildren方法為其所有的孩子設置寬和高,此行執行完成后,childView的寬和高都已經正確的計算過了
43-71行,根據childView的寬和高,以及margin,計算ViewGroup在wrap_content時的寬和高。
80-82行,如果寬高屬性值為wrap_content,則設置為43-71行中計算的值,否則為其父容器傳入的寬和高。
onLayout對其所有childView進行定位(設置childView的繪制區域)
// abstract method in viewgroup @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int cCount = getChildCount(); int cWidth = 0; int cHeight = 0; MarginLayoutParams cParams = null; /** * 遍歷所有childView根據其寬和高,以及margin進行布局 */ for (int i = 0; i < cCount; i++) { View childView = getChildAt(i); cWidth = childView.getMeasuredWidth(); cHeight = childView.getMeasuredHeight(); cParams = (MarginLayoutParams) childView.getLayoutParams(); int cl = 0, ct = 0, cr = 0, cb = 0; switch (i) { case 0: cl = cParams.leftMargin; ct = cParams.topMargin; break; case 1: cl = getWidth() - cWidth - cParams.leftMargin - cParams.rightMargin; ct = cParams.topMargin; break; case 2: cl = cParams.leftMargin; ct = getHeight() - cHeight - cParams.bottomMargin; break; case 3: cl = getWidth() - cWidth - cParams.leftMargin - cParams.rightMargin; ct = getHeight() - cHeight - cParams.bottomMargin; break; } cr = cl + cWidth; cb = cHeight + ct; childView.layout(cl, ct, cr, cb); } }參考鏈接
Android 手把手教您自定義ViewGroup(一) - Hongyang - 博客頻道 - CSDN.NET
教你搞定Android自定義ViewGroup - 簡書
總結
以上是生活随笔為你收集整理的Android之自定义ViewGroup的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吴恩达作业4:权重初始化
- 下一篇: Android实现网络音乐播放器