天天看點

關于Android中onMeasure方法

       首先,我們來了解Android是如何繪制頁面的view的。

一、Android中View的繪制過程

        Android的頁面結構是一個樹狀結構,是以在繪制的過程是按根節點開始逐級繪制的,在繪制每個節點,也就是每個View的時候,需要完成兩個計算:測量Measure和布局Layout。對應的方法就是measure (int widthMeasureSpec, int heightMeasureSpec)和layout (int l, int t, int r, int b).

       measure()方法會計算view所需要的空間,在調用結束,會儲存尺寸資訊在自己的view中。而layout()則是計算view的布局資訊,包括位置,邊距等。

       當一個View的Measure()方法傳回的時候,

它的getMeasureWidth()和getMeasureHeight()值必須已經被設定了,并且其子view的對應屬性也一并被設定了。同時,一個view測量的尺寸必須與父view的所給出的限制條件。也就是說,子View在計算自身所需要的空間大小的時候,必須考慮父View的一些對應的限制條件。這樣,對于每一個View,其子View的所有測量結果也已結束。

   注意:一個

parent可能不止一次的調用其子View的measure()方法。這是由于在第一次調用的時候,如果所有子view所測量的結果都是未指明具體的大小,可是parent是希望能夠确切的知道其所希望的大小,是以這時候會再次調用measure()方法,不過這次parent會增加一些限制條件,以便能得到具體的大小值。

       在計算View所需要的空間大小的時候,會用到

LayoutParams,對應的不同的Layout,所用到的LayoutParams也會有所不同

。在計算View所需要的大小的時候,就隻需要一下三個屬性:

       1、MATCH_PARENT(FILL_PARENT)   盡量和parent一樣大

       2、WRAP_CONTENT   根據自身所展示的内容來計算大小

       3、具體的數值

二、onMeasure()方法

      onMeasure()是呈現一個元件與其上層容器至關重要的一個方法。此方法是測量view和其内容所需要的空間大小,這個方法是由measure()方法調用的,隻需要覆寫onMeasure()方法,為其提供更準确的測量值。在複寫onMeasure()方法的時候,必須調用setmeasureDimension()來設定計算出來的width和height值,這樣才能儲存view所包含的的測量資料。如果不調用此方法,将會抛出異常。

     onMeasure(int widthMeasureSpec, int heightMeasureSpec)

     其中的widthMeasureSpec和heightMeasureSpec是parent所能提供的空間大小。

     這兩個參數是按照View.MeasureSpec來進行編碼存儲的,可以通過MeasureSpec來解析,擷取到對應的資料;

     每一個MeasureSpec包含了一個Size值和一個Mode值。Size值就是具體的大小,重點說明一下Mode。

    總共有三種模式:

    UNSPECIFIED:parent沒有做任何限制,子view可以用所有的想要的尺寸;

    EXACTLY :parent為子view提供了一個絕對尺寸的限制,子view将被賦予這些限制;

    AT_MOST :parent會給子view一個尺寸上限,子View可以在這個上限以内定義值。

   覆寫onMeasure()方法時,子類有責任確定這個view的最小height和width,也就是getSuggestedMinimumHeight和getSUggestedMinimusWidth();

  大家可以參考文檔:

   https://developer.android.com/guide/topics/ui/how-android-draws.html

  http://developer.android.com/guide/topics/ui/custom-components.html

  http://developer.android.com/reference/android/view/View.html#onMeasure%28int,%20int%29

  官方提供了一個例子,在APIDemo中,搜尋LabelView(com.example.android.apis.view)

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.view;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.example.android.apis.R;


/**
 * Example of how to write a custom subclass of View. LabelView
 * is used to draw simple text views. Note that it does not handle
 * styled text or right-to-left writing systems.
 *
 */
public class LabelView extends View {
    private Paint mTextPaint;
    private String mText;
    private int mAscent;
    
    /**
     * Constructor.  This version is only needed if you will be instantiating
     * the object manually (not from a layout XML file).
     * @param context
     */
    public LabelView(Context context) {
        super(context);
        initLabelView();
    }

    /**
     * Construct object, initializing with any attributes we understand from a
     * layout file. These attributes are defined in
     * SDK/assets/res/any/classes.xml.
     * 
     * @see android.view.View#View(android.content.Context, android.util.AttributeSet)
     */
    public LabelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLabelView();

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.LabelView);

        CharSequence s = a.getString(R.styleable.LabelView_text);
        if (s != null) {
            setText(s.toString());
        }

        // Retrieve the color(s) to be used for this view and apply them.
        // Note, if you only care about supporting a single color, that you
        // can instead call a.getColor() and pass that to setTextColor().
        setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));

        int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
        if (textSize > 0) {
            setTextSize(textSize);
        }

        a.recycle();
    }

    private final void initLabelView() {
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        // Must manually scale the desired text size to match screen density
        mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
        mTextPaint.setColor(0xFF000000);
        setPadding(3, 3, 3, 3);
    }

    /**
     * Sets the text to display in this label
     * @param text The text to display. This will be drawn as one line.
     */
    public void setText(String text) {
        mText = text;
        requestLayout();
        invalidate();
    }

    /**
     * Sets the text size for this label
     * @param size Font size
     */
    public void setTextSize(int size) {
        // This text size has been pre-scaled by the getDimensionPixelOffset method
        mTextPaint.setTextSize(size);
        requestLayout();
        invalidate();
    }

    /**
     * Sets the text color for this label.
     * @param color ARGB value for the text
     */
    public void setTextColor(int color) {
        mTextPaint.setColor(color);
        invalidate();
    }

    /**
     * @see android.view.View#measure(int, int)
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }

    /**
     * Determines the width of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The width of the view, honoring constraints from measureSpec
     */
    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text
            result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
                    + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }

        return result;
    }

    /**
     * Determines the height of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        mAscent = (int) mTextPaint.ascent();
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text (beware: ascent is a negative number)
            result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop()
                    + getPaddingBottom();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    /**
     * Render the text
     * 
     * @see android.view.View#onDraw(android.graphics.Canvas)
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint);
    }
}