android中onMeasure初看,深入理解布局之一!
来源:互联网 发布:魔方数据恢复官网 编辑:程序博客网 时间:2024/05/22 02:27
今天学习android自定义组件:docs/guide/topics/ui/custom-components.html
其中有两个对布局界面影响很的方法,onDraw(),和onMeasure().
onDraw()比较好理解.onMeasure()就比较难理解一些,也更复杂些 ,引用文档中的说法就是:
onMeasure() is a little more involved.其实还有另一个方面的原因就是我对这个单词measure不是很知道,然后果了下词典,就放了下心,确实是测量的意思.
实现onMeasure()方法基本需要完成下面三个方面的事情(最终结果是你自己写相应代码得出测量值并调用view的一个方法进行设置,告诉给你的view安排位置大小的父容器你要多大的空间.):
1.传递进来的参数,widthMeasureSpec,和heightMeasureSpec是你对你应该得出来的测量值的限制.
The overidden onMeasure() method is called with width and height measure specifications(widthMeasureSpec and heightMeasureSpec parameters,both are integer codes representing dimensions) which should be treated as requirements for the restrictions on the width and height measurements you should produce.
2. 你在onMeasure计算出来设置的width和height将被用来渲染组件.应当尽量在传递进来的width和height 声明之间.
虽然你也可以选择你设置的尺寸超过传递进来的声明.但是这样的话,父容器可以选择,如clipping,scrolling,或者抛出异常,或者(也许是用新的声明参数)再次调用onMeasure()
Your component's onMeasure() method should calculate a measurement width and height which will be required to render the component.it should try to stay within the specified passed in.although it can choose to exceed them(in this case,the parent can choose what to do,including clipping,scrolling,throwing an excption,or asking the onMeasure to try again,perhaps with different measurement specifications).
3.一但width和height计算好了,就应该调用View.setMeasuredDimension(int width,int height)方法,否则将导致抛出异常.
Once the width and height are calculated,the setMeasureDimension(int width,int height) method must be called with the calculated measurements.Failure to do this will result in an exceptiion being thrown
在Android提提供的一个自定义View示例中(在API demos 中的 view/LabelView)可以看到一个重写onMeasure()方法的
实例,也比较好理解.
01
/**
03
*/
05
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
07
measureHeight(heightMeasureSpec));
09
11
* Determines the width of this view
13
* @return The width of the view, honoring constraints from measureSpec
15
private
int
measureWidth(
int
measureSpec) {
17
int
specMode = MeasureSpec.getMode(measureSpec);
19
21
// We were told how big to be
23
}
else
{
25
result = (
int
) mTextPaint.measureText(mText) + getPaddingLeft()
27
if
(specMode == MeasureSpec.AT_MOST) {
29
result = Math.min(result, specSize);
31
}
33
return
result;
02
private
static
final
int
MODE_SHIFT =
30
;
04
06
public
static
final
int
EXACTLY =
1
<< MODE_SHIFT;
08
10
return
size + mode;
12
public
static
int
getMode(
int
measureSpec) {
14
}
16
return
(measureSpec & ~MODE_MASK);
1
而把MODE_SHIFF就看成30.那为什么是二进制 的11呢?
呢,因为只有三各模式,如果有四种模式就是111了因为111三个位才可以有四种组合对吧.
我们这样来看,
UNSPECIFIED=00000000000000000000000000000000,
EXACTLY=01000000000000000000000000000000,
AT_MOST=10000000000000000000000000000000
也就是说,0,1,2
对应 00,01,10
当跟11想与时 00 &11 还是得到 00,11&01 -> 01,10&
我觉得到了这个份上相信,看我博客的也都理解了.
return (measureSpec & ~MODE_MASK);应该是 return (measureSpec & (~MODE_MASK));
- android中onMeasure初看,深入理解布局之一!
- android中onMeasure初看,深入理解布局之一!
- 【转】android中onMeasure初看,深入理解布局之一!
- 【view】android中onMeasure初看,深入理解布局之一!
- android中onMeasure初看,深入理解布局之一!
- android中onMeasure初看,深入理解布局之一!
- android中onMeasure初看,深入理解布局之一!
- android 自定义view中onMeasure()理解
- android 自定义view中onMeasure()理解
- android 自定义view中onMeasure()理解
- Android中对于onMeasure()的理解
- Android中onMeasure方法的理解
- Android requestLayout(),invalidate深入理解,最终调用onmeasure()
- android中对View的onMeasure()方法的理解
- android中对自定义View的onMeasure()方法的理解
- android中对View的onMeasure()方法的理解
- android中对View的onMeasure()方法的理解
- 深入理解Android四大组件之一BroadcastReceiver
- 第15周项目 2—洗牌
- 第9周课后实践1-①
- fast 角点实现
- 多旋翼油门量与升力关系
- 第十三周上机实践项目 项目3--形状类族中的纯虚函数
- android中onMeasure初看,深入理解布局之一!
- 计算机视觉及机器学习的一些学习资源(都是干货)
- 第15周项目-1-代码填空
- vector容器
- 一位搬砖工人自学python的开始
- RNA-seq Experimental Design and Quality Control
- 第十二周项目训练2.3
- 搭建DNS服务器
- [李景山php]每天laravel-20160831|EventServiceProvider