日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ViewGroup的测量及绘制

發布時間:2023/12/2 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ViewGroup的测量及绘制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、ViewGroup的測量 ? public abstract class

ViewGroup

extends View
implements ViewManager ViewParent
java.lang.Object
????android.view.View
?????android.view.ViewGroup

Class Overview

A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers.

對于ViewGroup來說除了完成自身的measure過程外,還要遍歷去調用所有子元素的measure方法,各子元素再遞歸去執行這個過程。ViewGroup提供了一個measureChildren方法:

protected voidmeasureChildren (int widthMeasureSpec, int heightMeasureSpec)

Ask all of the children of this view to measure themselves, taking into account both the MeasureSpec requirements for this view and its padding. We skip children that are in the GONE state The heavy lifting is done in getChildMeasureSpec.

Parameters
widthMeasureSpecheightMeasureSpec
The width requirements for this view
The height requirements for this view
protected void measureChildren(int widthMeasureSpec,int heightMeasureSpec){final int size = mChildrenCount;final View[] children = mChildren;for(int i = 0;i < size; ++i){final View child = children[i];if((child.mViewFlags & VISIBILITY_MASK) != GONE){measureChild(child,widthMeasureSpec,heightMeasureSpec);}} }

?

從上面的源碼看,ViewGroup在measure時,會對每一個元素進行measure。

measureChild方法:

protected void measureChild(View child,int parentWidthMeasureSpec,int parentHeightMeasureSpec){final LayoutParams lp = child.getLayoutParams();final int childWidthMeasureSpec = getChildMeasureSpec(parentWidth - MeasureSpec,mPaddingLeft + mPaddingRight,lp.width);final int childHeightMeasureSpec = getChildMeasureSpec(parentHeight - MeasureSpec,mPaddingTop + mPaddingBottom,lp.height);child.measure(childWidthMeasureSpec, childHeightMeasureSpec); }


measureChild的思想就是取出子元素的LayoutParams,再通過getChildMeasureSpec來獲取子元素的MeasureSpec,接著直接將MeasureSpec傳遞給View的measure方法進行測量。

?

2、ViewGroup的繪制

?

ViewGroup通常情況下不需要繪制,如果不用指定ViewGroup的背景顏色,其onDraw()方法都不會被調用。ViewGroup會使用dispatchDraw()方法繪制其子View,過程同樣是遍歷所有子View,并調用子View的繪制方法來完成繪制。

總結

以上是生活随笔為你收集整理的ViewGroup的测量及绘制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。