覆写onMeaure进行measure操作

来源:互联网 发布:windows xp密钥 编辑:程序博客网 时间:2024/05/16 04:39


     android在屏幕上绘制视图3步: measure测量、layout布局、draw绘制。
     这里主要介绍第一步measure,measure是view大小计算的过程。先来一个自定义View的例子,演示如何覆写onMeasure方法


一、覆写onMeasure的例子(自定义View)  


自定义view全屏显示蓝色方块


1. 覆写onMeasure
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { }


2. 分别计算出宽高,后面解释使用的方法的作用
    private int measureWidth(int pWidthMeasureSpec) {    int result = 0;            int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);        int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);                switch (widthMode) {        case MeasureSpec.AT_MOST:        case MeasureSpec.EXACTLY:        result = widthSize;            break;        }        return result;    }            private int measureHeight(int pHeightMeasureSpec) {    int result = 0;            int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);        int heightSize = MeasureSpec.getSize(pHeightMeasureSpec);                switch (heightMode) {        case MeasureSpec.AT_MOST:        case MeasureSpec.EXACTLY:        result = heightSize;            break;        }                return result;    }


3. 调用setMeasuredDimension,指定视图在屏幕上的大小、
        int measureWidth = measureWidth(widthMeasureSpec);        int measureHeight = measureHeight(heightMeasureSpec);                setMeasuredDimension(measureWidth, measureHeight);


例子下载地址


二、onMeasure参数


1. 自定义控件代码
public class LoveWorld extends View {    ......        @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {            int measureWidth = MeasureSpec.getSize(widthMeasureSpec);    int modeWidth = MeasureSpec.getMode(widthMeasureSpec);        Log.e("Test", "widthMeasureSpec = " + widthMeasureSpec     + " , measureWidth = " + measureWidth    + " , modeWidth = " + modeWidth    + " , toString = " + MeasureSpec.toString(widthMeasureSpec));                setMeasuredDimension(measureWidth,  MeasureSpec.getSize(heightMeasureSpec));    }......}


2. onMeasure 中使用方法说明:
MeasureSpec类有以下方法:
public static int getMode (int measureSpec) 从传入的值中获取属于MeasureSpec定义的哪一种模式
public static int getSize (int measureSpec) 从传入的值中获取大小
public static int makeMeasureSpec(int size, int mode) 创建MeasureSpec


* 布局文件
<com.example.onmeasuredemo.LoveWorld xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/custom_textview"    android:layout_width="wrap_content"    android:layout_height="100dip" />

* 手机屏幕屏幕是: 720 * 1280, 打印出log
widthMeasureSpec = -2147482928 , measureWidth = 720 , modeWidth = -2147483648 , toString = MeasureSpec: AT_MOST 720widthMeasureSpec = -2147482928 , measureWidth = 720 , modeWidth = -2147483648 , toString = MeasureSpec: AT_MOST 720


* 布局文件
<com.example.onmeasuredemo.LoveWorld xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/custom_textview"    android:layout_width="match_parent"    android:layout_height="100dip" />


* 打印出log
widthMeasureSpec = 1073742544 , measureWidth = 720 , modeWidth = 1073741824 , toString = MeasureSpec: EXACTLY 720widthMeasureSpec = 1073742544 , measureWidth = 720 , modeWidth = 1073741824 , toString = MeasureSpec: EXACTLY 720


通过int值32位记录两个值,通过MeasureSpec创建与读取,具体细节:
MeasureSpec 解析




资料
官方资料:Custom Components

计算控件尺寸

MeasureSpec介绍  ( 如何计算size和mode )

重写onMeasure典型例子


    本篇对覆写onMeausre有所了解,但是知道进行测量并不能显示视图,还需要进行onLayout操作,可以查看此文章《覆写onLayout进行layout,含自定义ViewGroup例子》



2013-02-28  “待添加” 内容
2013-03-31 添加与onLayout关联
2013-04-07  添加MeasureSpec.EXACTLY | itemWidth
2015-03-15 增加MeasureSpec内容


原创粉丝点击