激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - 一篇文章弄懂Android自定義viewgroup的相關難點

一篇文章弄懂Android自定義viewgroup的相關難點

2022-03-02 15:46DK_BurNIng Android

這篇文章主要給大家介紹了關于如何通過一篇文章弄懂Android中自定義viewgroup的一些相關難點,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

本文的目的

目的在于教會大家到底如何自定義viewgroup,自定義布局和自定義測量到底如何寫。很多網上隨便搜搜的概念和流程圖
這里不再過多描述了,建議大家看本文之前,先看看基本的自定義viewgroup流程,心中有個大概即可。本文注重于實踐
viewgroup 的測量布局流程基本梳理

稍微回顧下,基本的viewgroup繪制和布局流程中的重點:

1.view 在onMeasure()方法中進行自我測量和保存,也就是說對于view(不是viewgroup噢)來說一定在onMeasure方法中計算出自己的尺寸并且保存下來

2.viewgroup實際上最終也是循環從上大小來調用子view的measure方法,注意子view的measure其實最終調用的是子view的onMeasure 方法。所以我們理解這個過程為:

viewgroup循環遍歷調用所有子view的onmeasure方法,利用onmeasure方法計算出來的大小,來確定這些子view最終可以占用的大小和所處的布局的位置。

3.measure方法是一個final方法,可以理解為做測量工作準備工作的,既然是final方法所以我們無法重寫它,不需要過多
關注他,因為measure最終要調用onmeasure ,這個onmeasure我們是可以重寫的。要關注這個。layout和onlayout是一樣的關系。

4.父view調用子view的layout方法的時候會把之前measure階段確定的位置和大小都傳遞給子view。

5.對于自定義view/viewgroup來說 我們幾乎只需要關注下面三種需求:

  • 對于已有的android自帶的view,我們只需要重寫他的onMeasure方法即可。修改一下這個尺寸即可完成需求。
  • 對于android系統沒有的,屬于我們自定義的view,比上面那個要復雜一點,要完全重寫onMeasure方法。
  • 第三種最復雜,需要重寫onmeasure和onlayout2個方法,來完成一個復雜viewgroup的測量和布局。

6.onMeasure方法的特殊說明:

一篇文章弄懂Android自定義viewgroup的相關難點

7.如何理解父view對子view的限制?

onMeasure的兩個參數既然是父view對子view的限制,那么這個限制的值到底是哪來的呢?

實際上,父view對子view的限制絕大多數就來自于我們開發者所設置的layout開頭的這些屬性

比方說我們給一個imageview設置了他的layout_width和layout_height 這2個屬性,那這2個屬性其實就是我們開發者
所期望的寬高屬性,但是要注意了,

設置的這2個屬性是給父view看的,實際上對于絕大多數的layout開頭的屬性這些屬性都是設置給父view看的

為什么要給父view看?因為父view要知道這些屬性以后才知道要對子view的測量加以什么限制?

到底是不限制(UNSPECIFIED)?還是限制個最大值(AT_MOST),讓子view不超過這個值?還是直接限制死,我讓你是多少就得是多少(EXACTLY)。

自定義一個BannerImageView 修改onMeasure方法

所謂bannerImageview,就是很多電商其實都會放廣告圖,這個廣告圖的寬高比都是可變的,我們在日常開發過程中
也會經常接觸到這種需求:imageview的寬高比 在高保真中都標注出來,但是考慮到很多手機的屏幕寬度或者高度都不確定

所以我們通常都要手動來計算出這個imageview高度或者寬度,然后動態改變width或者height的值。這種方法可用但是很麻煩

這里給出一個自定義的imageview,通過設置一個ratio的屬性即可動態的設置iv的高度。很是方便

一篇文章弄懂Android自定義viewgroup的相關難點

看下效果

一篇文章弄懂Android自定義viewgroup的相關難點

最后看下代碼,重要的部分都寫在注釋里了,不再過多講了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class BannerImageView extends ImageView {
 
 //寬高比
 float ratio;
 
 public BannerImageView(Context context) {
 super(context);
 }
 
 public BannerImageView(Context context, AttributeSet attrs) {
 super(context, attrs);
 
 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BannerImageView);
 ratio = typedArray.getFloat(R.styleable.BannerImageView_ratio, 1.0f);
 typedArray.recycle();
 }
 
 public BannerImageView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 }
 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 //人家自己的測量還是要自己走一遍的,因為這個方法內部會調用setMeasuredDimension方法來保存測量結果了
 //只有保存了以后 我們才能取得這個測量結果 否則你下面是取不到的
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
 //取測量結果
 int mWidth = getMeasuredWidth();
 
 int mHeight = (int) (mWidth * ratio);
 
 //保存了以后,父view就可以拿到這個測量的寬高了。不保存是拿不到的噢。
 setMeasuredDimension(mWidth, mHeight);
 }
}

自定義view,完全自己寫onMeasure方法

首先明確一個結論:

對于完全自定義的view,完全自己寫的onMeasure方法來說,你保存的寬高必須要符合父view的限制,否則會發生bug,
保存父view對子view的限制的方法也很簡單直接調用resolveSize方法即可。

一篇文章弄懂Android自定義viewgroup的相關難點

一篇文章弄懂Android自定義viewgroup的相關難點

所以對于完全自定義的view onMeasure方法也不難寫了,

  • 先算自己想要的寬高,比如你畫了個圓,那么寬高就肯定是半徑的兩倍大小, 要是圓下面還有字,
  • 那么高度肯定除了半徑的兩倍還要有字體的大小。對吧。很簡單。這個純看你自定義view是啥樣的
  • 算完自己想要的寬高以后 直接拿resolveSize 方法處理一下 即可。
  • 最后setMeasuredDimension 保存。

范例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class LoadingView extends View {
 
 //圓形的半徑
 int radius;
 
 //圓形外部矩形rect的起點
 int left = 10, top = 30;
 
 
 Paint mPaint = new Paint();
 
 public LoadingView(Context context) {
 super(context);
 }
 
 public LoadingView(Context context, AttributeSet attrs) {
 super(context, attrs);
 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingView);
 radius = typedArray.getInt(R.styleable.LoadingView_radius, 0);
 }
 
 public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 }
 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
 
 int width = left + radius * 2;
 int height = top + radius * 2;
 
 //一定要用resolveSize方法來格式化一下你的view寬高噢,否則遇到某些layout的時候一定會出現奇怪的bug的。
 //因為不用這個 你就完全沒有父view的感受了 最后強調一遍
 width = resolveSize(width, widthMeasureSpec);
 height = resolveSize(height, heightMeasureSpec);
 
 setMeasuredDimension(width, height);
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 
 RectF oval = new RectF(left, top,
  left + radius * 2, top + radius * 2);
 mPaint.setColor(Color.BLUE);
 canvas.drawRect(oval, mPaint);
 //先畫圓弧
 mPaint.setColor(Color.RED);
 mPaint.setStyle(Paint.Style.STROKE);
 mPaint.setStrokeWidth(2);
 canvas.drawArc(oval, -90, 360, false, mPaint);
 }
}

布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<LinearLayout
  android:layout_width="200dp"
  android:layout_height="200dp"
  android:background="#000000"
  android:orientation="horizontal">
 
  <com.example.a16040657.customviewtest.LoadingView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@mipmap/dly"
   app:radius="200"></com.example.a16040657.customviewtest.LoadingView>
 
  <com.example.a16040657.customviewtest.LoadingView
   android:layout_marginLeft="10dp"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@mipmap/dly"
   app:radius="200"></com.example.a16040657.customviewtest.LoadingView>
 </LinearLayout>

最后效果:

一篇文章弄懂Android自定義viewgroup的相關難點

自定義一個viewgroup

這個其實也就是稍微復雜了一點,但是還是有跡可循的,只是稍微需要一點額外的耐心。

自定義一個viewgroup 需要注意的點如下:

  • 一定是先重寫onMeasure確定子view的寬高和自己的寬高以后 才可以繼續寫onlayout 對這些子view進行布局噢~~
  • viewgroup 的onMeasure其實就是遍歷自己的view 對自己的每一個子view進行measure,絕大多數時候對子view的measure都可以直接用 measureChild()這個方法來替代,簡化我們的寫法,如果你的viewgroup很復雜的話無法就是自己寫一遍measureChild 而不是調用measureChild 罷了。
  • 計算出viewgroup自己的尺寸并且保存,保存的方法還是哪個setMeasuredDimension 不要忘記了
  • 逼不得已要重寫measureChild方法的時候,其實也不難無非就是對父view的測量和子view的測量 做一個取舍關系而已,你看懂了基礎的measureChild方法,以后就肯定會寫自己的復雜的measureChild方法了。

下面是一個極簡的例子,一個很簡單的flowlayout的實現,沒有對margin paddding做處理,也假設了每一個tag的高度
是固定的,可以說是極為簡單了,但是麻雀雖小 五臟俱全,足夠你們好好理解自定義viewgroup的關鍵點了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
 * 寫一個簡單的flowlayout 從左到右的簡單layout,如果寬度不夠放 就直接另起一行layout
 * 這個類似的開源控件有很多,有很多寫的出色的,我這里只僅僅實現一個初級的flowlayout
 * 也是最簡單的,目的是為了理解自定義viewgroup的關鍵核心點。
 * <p>
 * 比方說這里并沒有對padding或者margin做特殊處理,你們自己寫viewgroup的時候 記得把這些屬性的處理都加上
 * 否則一旦有人用了這些屬性 發現沒有生效就比較難看了。。。。。。
 */
public class SimpleFlowLayout extends ViewGroup {
 public SimpleFlowLayout(Context context) {
  super(context);
 }
 
 public SimpleFlowLayout(Context context, AttributeSet attrs) {
  super(context, attrs);
 }
 
 public SimpleFlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
 }
 
 /**
  * layout的算法 其實就是 不夠放剩下一行 那另外放一行 這個過程一定要自己寫一遍才能體會,
  * 個人有個人的寫法,說不定你的寫法比開源的項目還要好
  * 其實也沒什么夸張的,無法就是前面onMeasure結束以后 你可以拿到所有子view和自己的 測量寬高 然后就算唄
  *
  * @param changed
  * @param l
  * @param t
  * @param r
  * @param b
  */
 
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  int childTop = 0;
  int childLeft = 0;
  int childRight = 0;
  int childBottom = 0;
 
  //已使用 width
  int usedWidth = 0;
 
 
  //customlayout 自己可使用的寬度
  int layoutWidth = getMeasuredWidth();
  Log.v("wuyue", "layoutWidth==" + layoutWidth);
  for (int i = 0; i < getChildCount(); i++) {
   View childView = getChildAt(i);
   //取得這個子view要求的寬度和高度
   int childWidth = childView.getMeasuredWidth();
   int childHeight = childView.getMeasuredHeight();
 
   //如果寬度不夠了 就另外啟動一行
   if (layoutWidth - usedWidth < childWidth) {
    childLeft = 0;
    usedWidth = 0;
    childTop += childHeight;
    childRight = childWidth;
    childBottom = childTop + childHeight;
    childView.layout(0, childTop, childRight, childBottom);
    usedWidth = usedWidth + childWidth;
    childLeft = childWidth;
    continue;
   }
   childRight = childLeft + childWidth;
   childBottom = childTop + childHeight;
   childView.layout(childLeft, childTop, childRight, childBottom);
   childLeft = childLeft + childWidth;
   usedWidth = usedWidth + childWidth;
 
  }
 }
 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 
  //先取出SimpleFlowLayout的父view 對SimpleFlowLayout 的測量限制 這一步很重要噢。
  //你只有知道自己的寬高 才能限制你子view的寬高
  int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  int heightSize = MeasureSpec.getSize(heightMeasureSpec);
 
 
  int usedWidth = 0//已使用的寬度
  int remaining = 0//剩余可用寬度
  int totalHeight = 0; //總高度
  int lineHeight = 0//當前行高
 
  for (int i = 0; i < getChildCount(); i++) {
   View childView = getChildAt(i);
   LayoutParams lp = childView.getLayoutParams();
 
   //先測量子view
   measureChild(childView, widthMeasureSpec, heightMeasureSpec);
   //然后計算一下寬度里面 還有多少是可用的 也就是剩余可用寬度
   remaining = widthSize - usedWidth;
 
   //如果一行不夠放了,也就是說這個子view測量的寬度 大于 這一行 剩下的寬度的時候 我們就要另外啟一行了
   if (childView.getMeasuredWidth() > remaining) {
    //另外啟動一行的時候,使用過的寬度 當然要設置為0
    usedWidth = 0;
    //另外啟動一行了 我們的總高度也要加一下,不然高度就不對了
    totalHeight = totalHeight + lineHeight;
   }
 
   //已使用 width 進行 累加
   usedWidth = usedWidth + childView.getMeasuredWidth();
   //當前 view 的高度
   lineHeight = childView.getMeasuredHeight();
  }
 
  //如果SimpleFlowLayout 的高度 為wrap cotent的時候 才用我們疊加的高度,否則,我們當然用父view對如果SimpleFlowLayout 限制的高度
  if (heightMode == MeasureSpec.AT_MOST) {
   heightSize = totalHeight;
  }
  setMeasuredDimension(widthSize, heightSize);
 }
}

最后看下效果

一篇文章弄懂Android自定義viewgroup的相關難點

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:https://juejin.im/post/5b25bc136fb9a00e373bd0c8

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美色视 | 毛片视频免费播放 | 国产美女的小嫩bbb图片 | 一区二区三高清 | 久久亚洲国产精品 | 久久精品视频一区 | 护士hd欧美free性xxxx | 欧美激情精品久久久久久久久久 | 久久精品国产一区二区 | 深夜福利视频免费观看 | 7777在线视频免费播放 | 久久精品国产亚洲7777小说 | 日本黄色大片免费 | 国产午夜网 | 欧美成人高清视频 | 欧美黑大粗硬毛片视频 | 国产精品手机在线亚洲 | 久久久久久久一区二区三区 | 亚洲电影在线观看高清免费 | 2019中文字幕在线播放 | 91麻豆精品国产91久久久更新资源速度超快 | 亚洲一区免费电影 | 久久精品视频2 | 久草成人在线观看 | 黄色视屏免费看 | 久草在线高清 | 亚洲精品永久视频 | 黄在线 | 九九热免费精品 | 免费在线观看亚洲 | 成人资源在线观看 | 日韩黄色av网站 | 午夜精品久久久久久毛片 | 伊人亚洲精品 | 亚洲影院久久久av天天蜜桃臀 | 久久草在线视频免费 | 综合精品一区 | 视频一区免费观看 | 久久久久久久久久久久免费 | 亚洲成年人免费网站 | 日本不卡一二三区 |