在現(xiàn)實生活中,我們想要畫一個圖形怔匣,要知道他的大小和繪制握联,才回去進行繪畫。同樣每瞒,在Android系統(tǒng)中金闽,繪制View前,也需要知道View的大小和位置剿骨,即告訴系統(tǒng)該畫一個多大的View(onMeasure),在哪個位置繪畫(onLayout),今天主要了解view的測量代芜。
為了更好地理解View的測量,我分別打印出不同的寬懦砂、高和模式
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSize=MeasureSpec.getSize(widthMeasureSpec);
int widthMode=MeasureSpec.getMode(widthMeasureSpec);
int heightSize=MeasureSpec.getSize(heightMeasureSpec);
int heightMode=MeasureSpec.getMode(heightMeasureSpec);
Log.e("tag","widthMode:"+widthSize+"\nwidthSize:"+widthMode+"\nheightMode:"+heightMode+"\nheightSize:"+heightSize);
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.zx.test.CustomTextView
android:layout_width="300dp"
android:layout_height="200dp"
android:background="@android:color/darker_gray"
app:text="測量"
app:textColor="@color/colorPrimary"
app:textSize="22dp"/>
</RelativeLayout>
效果圖:
Log打印輸出:
E/tag: widthMode:300
widthSize:1073741824
heightMode:1073741824
heightSize:300
布局文件:
<com.zx.test.CustomTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
app:text="測量"
app:textColor="@color/colorPrimary"
app:textSize="22dp"/>
Log打印輸出:
E/tag: widthMode:480
widthSize:1073741824
heightMode:1073741824
heightSize:732
布局文件:
<com.zx.test.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
app:text="測量"
app:textColor="@color/colorPrimary"
app:textSize="22dp"/>
效果圖:
![NEJF$ZCJWO_$FN%K$AMR]{X.png](http://upload-images.jianshu.io/upload_images/2129339-0b76974301b1a6d1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Log打印輸出:
E/tag:widthMode:480
widthSize:1073741824
heightMode:-2147483648
heightSize:732
總結:
SpecMode有三類
EXACTLY 父容器已經(jīng)檢測出view的大小蜒犯,View的大小就是SpecSize所指定的值
AT_MOST 父容器指定了可用的大小即SpecSize,View的帶下不能超過這個值
UNSPECIFIED 父容器不對View有任何的限制,要多大有多大,一般用于系統(tǒng)內(nèi)部測量
但是荞膘,為什么上面的自定義view的width和height設置為wrap_content仍然是填充整個屏幕罚随,來看一下onMeausre源碼
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
public static int getDefaultSize(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
case MeasureSpec.UNSPECIFIED:
result = size;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}
通過源碼發(fā)現(xiàn)onMeasure方法最終調(diào)用了setMeasuredDimension(int measuredWidth, int measuredHeight)方法,而傳入的參數(shù)已經(jīng)是測量過的默認寬和高的值了羽资;我們看看getDefaultSize 方法是怎么計算測量寬高的淘菩。根據(jù)父控件給予的約束,發(fā)現(xiàn)AT_MOST (相當于wrap_content )和EXACTLY (相當于match_parent )兩種情況返回的測量寬高都是specSize屠升,而這個specSize正是我們上面說的父控件剩余的寬高潮改,所以默認onMeasure方法中wrap_content 和match_parent 的效果是一樣的。
下面重寫onMeasure方法腹暖,通過判斷測量的模式汇在,給出不同的測量值。當specMode為EXACTLY時脏答,直接使用指定的specSize即可糕殉,當specMode為其他兩種模式時,如果是AT_MOST(wrap_content)模式時殖告,要計算控件的尺寸阿蝶,控件帶下=文本的尺寸+左邊距+右邊距
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSize=MeasureSpec.getSize(widthMeasureSpec);
int widthMode=MeasureSpec.getMode(widthMeasureSpec);
int heightSize=MeasureSpec.getSize(heightMeasureSpec);
int heightMode=MeasureSpec.getMode(heightMeasureSpec);
Log.e("tag","widthMode"+widthSize+"\nwidthSize"+widthMode+"\nheightMode"+heightMode+"\nheightSize"+heightSize);
int width=0;
int height=0;
if(widthMode==MeasureSpec.AT_MOST){
width=mRect.width()+getPaddingLeft()+getPaddingRight();
Log.e("tag","text width"+width);
}else{
width=widthSize;
}
if(heightMode==MeasureSpec.AT_MOST){
height=mRect.height()+getPaddingBottom()+getPaddingTop();
Log.e("tag","text height"+height);
}else{
height=heightSize;
}
setMeasuredDimension(width,height);
}
Log打印輸出:
E/tag: widthMode480
widthSize-2147483648
heightMode-2147483648
heightSize732
text width63
text height31
效果如圖所示:
![H6{(YS]H)7NX$CZ0}FAR_IB.png](http://upload-images.jianshu.io/upload_images/2129339-924c3a4d29bffe5d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
好了,暫時就寫到這吧黄绩。