FlowLayout詳解(二)——FlowLayout實現,flowlayout詳解實現
前言:我最大的夢想,就是有一天,等老了坐在搖椅上回望一生,有故事給孩子們講……。
相關文章:
1、《FlowLayout詳解(一)——onMeasure()與onLayout()》
2、《FlowLayout詳解(二)——FlowLayout實現》
經過上篇的鋪墊,這篇就開始正式開始FlowLayout的開發啦,還是先給大家上上效果:
從中可以看到,底部container的布局方式應該是layout_width="match_parent",layout_height="wrap_content";
好了,廢話不多說了,下面開始進入正規。
一、XML布局從布局圖中可以看到,FlowLayout中包含了很多TextView.難度不大,布局代碼如下:
先定義一個style,這是為FlowLayout中的TextView定義的:
<style name="text_flag_01"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_margin">4dp</item> <item name="android:background">@drawable/flag_01</item> <item name="android:textColor">#ffffff</item></style>
注意,注意!!!我們這裡定義了margin,還記得上篇中怎麼樣提取Margin值嗎?重寫generateLayoutParams()函數。
下面看activity_main.xml的布局代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.example.harvic.myapplication.FlowLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff00ff"> <TextView style="@style/text_flag_01" android:background="@drawable/flag_03" android:text="Welcome" android:textColor="#43BBE7" /> <TextView style="@style/text_flag_01" android:background="@drawable/flag_03" android:text="IT工程師" android:textColor="#43BBE7" /> <TextView style="@style/text_flag_01" android:background="@drawable/flag_03" android:text="我真是可以的" android:textColor="#43BBE7" /> <TextView style="@style/text_flag_01" android:background="@drawable/flag_03" android:text="你覺得呢" android:textColor="#43BBE7" /> <TextView style="@style/text_flag_01" android:background="@drawable/flag_03" android:text="不要只知道掙錢" android:textColor="#43BBE7" /> <TextView style="@style/text_flag_01" android:background="@drawable/flag_03" android:text="努力ing" android:textColor="#43BBE7" /> <TextView style="@style/text_flag_01" android:background="@drawable/flag_03" android:text="I thick i can" android:textColor="#43BBE7" /> </com.example.harvic.myapplication.FlowLayout></LinearLayout>
這裡注意兩點,FlowLayout的android:layout_width設定為"match_parent",android:layout_height設定為""wrap_content";同時,我們為FlowLayout添加背景來明顯看出我們計算出來的所佔地區大小。
二、提取margin與onMeasure()重寫1、提取margin
上篇我們講過要提取margin,就一定要重寫generateLayoutParams
@Overrideprotected LayoutParams generateLayoutParams(LayoutParams p){ return new MarginLayoutParams(p);}@Overridepublic LayoutParams generateLayoutParams(AttributeSet attrs){ return new MarginLayoutParams(getContext(), attrs);}@Overrideprotected LayoutParams generateDefaultLayoutParams(){ return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);}具體為什麼我們就不再講了,上篇已經講的非常熟悉了,下面就看看如何在onMeasure()中計算當前container所佔的位置大小。
2、重寫onMeasure()——計算當前FlowLayout所佔的寬高這裡就要重寫onMeasure()函數,在其中計算所有當前container所佔的大小。
要做FlowLayout,首先涉及下面幾個問題:
(1)何時換行
從中可以看到,FlowLayout的布局是一行行的,如果當前行已經放不下下一個控制項,那就把這個控制項移到下一行顯示。所以我們要有個變數來計算當前行已經佔據的寬度,以判斷剩下的空間是否還能容得下下一個控制項。
(2)、如何得到FlowLayout的寬度
FlowLayout的寬度是所有行寬度的最大值,所以我們要記錄下每一行的所佔據的寬度值,進而找到所有值中的最大值。
(3)、如何得到FlowLayout的高度
很顯然,FlowLayout的高度是每一行高度的總和,而每一行的高度則是取該行中所有控制項高度的最大值。
原理到這裡就講完了,下面看看代碼:
(1)首先,剛進來的時候是利用MeasureSpec擷取系統建議的數值的模式
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int measureWidth = MeasureSpec.getSize(widthMeasureSpec); int measureHeight = MeasureSpec.getSize(heightMeasureSpec); int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec); int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec); ………………}(2)然後,是計算FlowLayout所佔用的空間大小
先申請幾個變數:
int lineWidth = 0;//記錄每一行的寬度int lineHeight = 0;//記錄每一行的高度int height = 0;//記錄整個FlowLayout所佔高度int width = 0;//記錄整個FlowLayout所佔寬度
然後開始計算:(先貼出代碼,再細講)
int count = getChildCount();for (int i=0;i<count;i++){ View child = getChildAt(i); measureChild(child,widthMeasureSpec,heightMeasureSpec); MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int childWidth = child.getMeasuredWidth() + lp.leftMargin +lp.rightMargin; int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; if (lineWidth + childWidth > measureWidth){ //需要換行 width = Math.max(lineWidth,childWidth); height += lineHeight; //因為由於盛不下當前控制項,而將此控制項調到下一行,所以將此控制項的高度和寬度初始化給lineHeight、lineWidth lineHeight = childHeight; lineWidth = childWidth; }else{ // 否則累加值lineWidth,lineHeight取最大高度 lineHeight = Math.max(lineHeight,childHeight); lineWidth += childWidth; } //最後一行是不會超出width範圍的,所以要單獨處理 if (i == count -1){ height += lineHeight; width = Math.max(width,lineWidth); }}在整個For迴圈遍曆每個控制項時,先計算每個子控制項的寬和高,代碼如下:
View child = getChildAt(i);measureChild(child,widthMeasureSpec,heightMeasureSpec);MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();int childWidth = child.getMeasuredWidth() + lp.leftMargin +lp.rightMargin;int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
注意我們在計算控制項高度和寬度時,要加上上、下、左、右的margin值。
這裡一定要注意的是:在調用child.getMeasuredWidth()、child.getMeasuredHeight()之前,一定要調用measureChild(child,widthMeasureSpec,heightMeasureSpec);!!!!在上篇中我們講過,在onMeasure()之後才能調用getMeasuredWidth()獲得值;同樣,只有調用onLayout()後,getWidth()才能擷取值。
下面就是判斷當前控制項是否換行及計算出最大高度和寬度了:
if (lineWidth + childWidth > measureWidth){ //需要換行 width = Math.max(lineWidth,width); height += lineHeight; //因為由於盛不下當前控制項,而將此控制項調到下一行,所以將此控制項的高度和寬度初始化給lineHeight、lineWidth lineHeight = childHeight; lineWidth = childWidth; }else{ // 否則累加值lineWidth,lineHeight取最大高度 lineHeight = Math.max(lineHeight,childHeight); lineWidth += childWidth; }由於lineWidth是用來累加當前行的總寬度的,所以當lineWidth + childWidth > measureWidth時就表示已經容不下當前這個控制項了,這個控制項就需要轉到下一行;我們先看else部分,即不換行時怎麼辦?
在不換行時,計算出當前行的最大高度,同時將當前子控制項的寬度累加到lineWidth上:
lineHeight = Math.max(lineHeight,childHeight);lineWidth += childWidth;
當需要換行時,首先將當前行寬lineWidth與目前的最大行寬width比較計算出最新的最大行寬width,作為當前FlowLayout所佔的寬度,還要將行高lineHeight累加到height變數上,以便計算出FlowLayout所佔的總高度。
width = Math.max(lineWidth,width);height += lineHeight;
下面就是重新初始化lineWidth和lineHeight了,由於換行,那當前控制項就是下一行控制項的第一個控制項,那麼當前行的行高就是這個控制項的高,當前行的行寬就是這個控制項的寬度值了:
lineHeight = childHeight;lineWidth = childWidth;
非常需要注意的是,當計算最後一行時,由於肯定是不會超過行寬的,而我們在for迴圈中,當不超過行寬中只做了下面處理:
//上面if語句的else部分 }else{ // 否則累加值lineWidth,lineHeight取最大高度 lineHeight = Math.max(lineHeight,childHeight); lineWidth += childWidth; }在這裡,我們只計算了行寬和行高,但並沒有將其width和height做計算,所以,當是最後一行的最後一個控制項時,我們要單獨運算width、height:
//最後一行是不會超出width範圍的,所以要單獨處理if (i == count -1){ height += lineHeight; width = Math.max(width,lineWidth);}(3)最後,通過setMeasuredDimension()設定到系統中:
setMeasuredDimension((measureWidthMode == MeasureSpec.EXACTLY) ? measureWidth : width, (measureHeightMode == MeasureSpec.EXACTLY) ? measureHeight : height);
完整的代碼如下:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int measureWidth = MeasureSpec.getSize(widthMeasureSpec); int measureHeight = MeasureSpec.getSize(heightMeasureSpec); int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec); int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec); int lineWidth = 0; int lineHeight = 0; int height = 0; int width = 0; int count = getChildCount(); for (int i=0;i<count;i++){ View child = getChildAt(i); measureChild(child,widthMeasureSpec,heightMeasureSpec); MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int childWidth = child.getMeasuredWidth() + lp.leftMargin +lp.rightMargin; int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; if (lineWidth + childWidth > measureWidth){ //需要換行 width = Math.max(lineWidth,width); height += lineHeight; //因為由於盛不下當前控制項,而將此控制項調到下一行,所以將此控制項的高度和寬度初始化給lineHeight、lineWidth lineHeight = childHeight; lineWidth = childWidth; }else{ // 否則累加值lineWidth,lineHeight取最大高度 lineHeight = Math.max(lineHeight,childHeight); lineWidth += childWidth; } //最後一行是不會超出width範圍的,所以要單獨處理 if (i == count -1){ height += lineHeight; width = Math.max(width,lineWidth); } } //當屬性是MeasureSpec.EXACTLY時,那麼它的高度就是確定的, // 只有當是wrap_content時,根據內部控制項的大小來確定它的大小時,大小是不確定的,屬性是AT_MOST,此時,就需要我們自己計算它的應當的大小,並設定進去 setMeasuredDimension((measureWidthMode == MeasureSpec.EXACTLY) ? measureWidth : width, (measureHeightMode == MeasureSpec.EXACTLY) ? measureHeight : height);}3、重寫onLayout()——布局所有子控制項在onLayout()中就是一個個布局子控制項了,由於控制項要後移和換行,所以我們要標記當前控制項的left座標和top座標,所以我們要先申請下面幾個變數:
protected void onLayout(boolean changed, int l, int t, int r, int b) { int count = getChildCount(); int lineWidth = 0;//累加當前行的行寬 int lineHeight = 0;//當前行的行高 int top=0,left=0;//當前座標的top座標和left座標 ………………}然後就是計算每個控制項的top座標和left座標,然後調用layout(int left,int top,int right,int bottom)來布局每個子控制項,代碼如下:(先列出來全部代碼,然後再細講)
for (int i=0; i<count;i++){ View child = getChildAt(i); MarginLayoutParams lp = (MarginLayoutParams) child .getLayoutParams(); int childWidth = child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin; int childHeight = child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin; if (childWidth + lineWidth >getMeasuredWidth()){ //如果換行 top += lineHeight; left = 0; lineHeight = childHeight; lineWidth = childWidth; }else{ lineHeight = Math.max(lineHeight,childHeight); lineWidth += childWidth; } //計算childView的left,top,right,bottom int lc = left + lp.leftMargin; int tc = top + lp.topMargin; int rc =lc + child.getMeasuredWidth(); int bc = tc + child.getMeasuredHeight(); child.layout(lc, tc, rc, bc); //將left置為下一子控制項的起始點 left+=childWidth;}(1)首先,與onMeasure()一樣,先計算出當前孩子的寬和高:
View child = getChildAt(i);MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();int childWidth = child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;int childHeight = child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin;
(2)然後根據是否要換行來計算當行控制項的top座標和left座標:
if (childWidth + lineWidth >getMeasuredWidth()){ //如果換行,當前控制項將跑到下一行,從最左邊開始,所以left就是0,而top則需要加上上一行的行高,才是這個控制項的top點; top += lineHeight; left = 0; //同樣,重新初始化lineHeight和lineWidth lineHeight = childHeight; lineWidth = childWidth;}else{ // 否則累加值lineWidth,lineHeight取最大高度 lineHeight = Math.max(lineHeight,childHeight); lineWidth += childWidth;}在計算好left,top之後,然後分別計算出控制項應該布局的上、下、左、右四個點座標:
需要非常注意的是margin不是padding,margin的距離是不繪製的控制項內部的,而是控制項間的間隔!
int lc = left + lp.leftMargin;//左座標+左邊距是控制項的開始位置int tc = top + lp.topMargin;//同樣,頂座標加頂邊距int rc =lc + child.getMeasuredWidth();int bc = tc + child.getMeasuredHeight();child.layout(lc, tc, rc, bc);
最後,計算下一座標的位置:由於在換行時才會變更top座標,所以在一個控制項繪製結束時,只需要變更left座標即可:
//將left置為下一子控制項的起始點left+=childWidth;
到這裡就結束了,onLayout的完整代碼如下:
protected void onLayout(boolean changed, int l, int t, int r, int b) { int count = getChildCount(); int lineWidth = 0; int lineHeight = 0; int top=0,left=0; for (int i=0; i<count;i++){ View child = getChildAt(i); MarginLayoutParams lp = (MarginLayoutParams) child .getLayoutParams(); int childWidth = child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin; int childHeight = child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin; if (childWidth + lineWidth >getMeasuredWidth()){ //如果換行,當前控制項將跑到下一行,從最左邊開始,所以left就是0,而top則需要加上上一行的行高,才是這個控制項的top點; top += lineHeight; left = 0; //同樣,重新初始化lineHeight和lineWidth lineHeight = childHeight; lineWidth = childWidth; }else{ lineHeight = Math.max(lineHeight,childHeight); lineWidth += childWidth; } //計算childView的left,top,right,bottom int lc = left + lp.leftMargin; int tc = top + lp.topMargin; int rc =lc + child.getMeasuredWidth(); int bc = tc + child.getMeasuredHeight(); child.layout(lc, tc, rc, bc); //將left置為下一子控制項的起始點 left+=childWidth; }}好啦,有關FlowLayout的系列文章到這裡就結束了,這裡主要涉及到ViewGroup的繪製流程的相關知識。希望大家能掌握。這篇文章感覺有點亂,難度倒是不大,凡是跟代碼有關的東東總是很難駕馭,可能還是語文不行啊,大家見量,多看看源碼吧,理解了上一篇之後,這篇難度不大。
源碼在文章底部給出參考文章:
1、《Android學習筆記]View的measure過程學習》
2、《Android中measure過程、WRAP_CONTENT詳解以及xml布局檔案解析流程淺析(下)》
3、《【Android】ViewGroup全面分析》
4、《安卓冷知識:LayoutParams》
5、《MeasureSpec介紹及使用詳解》
6、《Android 自訂ViewGroup 實戰篇 -> 實現FlowLayout》
7、《Android中View繪製流程以及invalidate()等相關方法分析》
8、《android中onMeasure初看,深入理解布局之一!》
9、《MeasureSpec中的測量模式和match_parent、wrap_content是什麼對應關係?》
10、《Android視圖繪製流程完全解析,帶你一步步深入瞭解View(二)》
11、《通過重寫ViewGroup學習onMeasure()和onLayout()方法》
12、《Android開發實踐:自訂ViewGroup的onLayout()分析》
如果本文有幫到你,記得加關注哦
源碼:http://download.csdn.net/detail/harvic880925/8928371
請大家尊重原創者著作權,轉載請標明出處:http://blog.csdn.net/harvic880925/article/details/47035455 ,謝謝
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。