关于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);
    }
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值