覆写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. 自定义控件代码
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
* 布局文件
* 手机屏幕屏幕是: 720 * 1280, 打印出log
* 布局文件
* 打印出log
通过int值32位记录两个值,通过MeasureSpec创建与读取,具体细节:
MeasureSpec 解析
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内容
- 覆写onMeaure进行measure操作
- 覆写onMeaure进行measure操作
- 覆写onMeaure进行measure操作
- 覆写onMeaure进行measure操作
- 【view】覆写onMeaure进行measure操作
- android view 覆写omMeasure进行measure操作
- 对服务器端文件进行写操作
- R:向上级层次进行写操作
- Measure
- 自定义view onMeaure
- 写文件系统时,内核在写操作的时候无法进行读操作
- JAVA操作属性文件,可进行读、写、更改
- 对项目配置文件app.comfig进行写操作
- java创建TXT文件并进行读、写、修改操作
- Java创建TXT文件并进行读、写、修改操作
- java创建TXT文件并进行读、写、修改操作
- JAVA操作属性文件,可进行读、写、更改
- java创建TXT文件并进行读、写、修改操作
- ruby版本升级后改动还挺大的(1.8.6-1.9.2)。
- 数据挖掘十大经典算法之SVM
- C++与Lua的交互(一)
- VC++使用DC画出点,线,矩形,椭圆
- C++基础拾遗
- 覆写onMeaure进行measure操作
- 反射获取方法参数及返回值类型
- 数据挖掘十大经典算法之NB
- 技术博客应该写什么
- C语言:矩阵操作练习
- 数据挖掘十大经典算法之AdaBoost
- 使用xeCJK排版中文
- usb_modeswitch
- 数据挖掘十大经典算法简介