覆写onMeaure进行measure操作

本文探讨了Android开发中自定义视图的关键步骤——覆写`onMeasure`方法来精确控制组件尺寸。通过详细解释`onMeasure`的参数及测量过程,帮助开发者实现更灵活的布局定制。
摘要由CSDN通过智能技术生成


     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 720
widthMeasureSpec = -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 720
widthMeasureSpec = 1073742544 , measureWidth = 720 , modeWidth = 1073741824 , toString = MeasureSpec: EXACTLY 720


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




资料

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



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



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


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值