android中ViewGroup中onMeasure()方法 .
来源:互联网 发布:网络系统集成结课论文 编辑:程序博客网 时间:2024/06/14 10:40
onMeasure方法在控件的父元素正要放置它的子控件时调用.它会问一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec.
它们指明控件可获得的空间以及关于这个空间描述的元数据.
比返回一个结果要好的方法是你传递View的高度和宽度到setMeasuredDimension方法里.
接下来的代码片段给出了如何重写onMeasure.注意,调用的本地空方法是来计算高度和宽度的.它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值.
java代码:
复制代码
边界参数——widthMeasureSpec和heightMeasureSpec ,效率的原因以整数的方式传入。在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getMode和getSize来译解,如下面的片段所示:
java代码:
复制代码
依据specMode的值,如果是AT_MOST,specSize 代表的是最大可获得的空间;如果是EXACTLY,specSize 代表的是精确的尺寸;如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。
当以EXACT方式标记测量尺寸,父元素会坚持在一个指定的精确尺寸区域放置View。在父元素问子元素要多大空间时,AT_MOST指示者会说给我最大的范围。在很多情况下,你得到的值都是相同的。
在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。
接下来的框架代码给出了处理View测量的典型实现:
java代码:
它们指明控件可获得的空间以及关于这个空间描述的元数据.
比返回一个结果要好的方法是你传递View的高度和宽度到setMeasuredDimension方法里.
接下来的代码片段给出了如何重写onMeasure.注意,调用的本地空方法是来计算高度和宽度的.它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值.
java代码:
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int measuredHeight = measureHeight(heightMeasureSpec);
- int measuredWidth = measureWidth(widthMeasureSpec);
- setMeasuredDimension(measuredHeight, measuredWidth);
- }
- private int measureHeight(int measureSpec) {
- // Return measured widget height.
- }
- private int measureWidth(int measureSpec) {
- // Return measured widget width.
- }
边界参数——widthMeasureSpec和heightMeasureSpec ,效率的原因以整数的方式传入。在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getMode和getSize来译解,如下面的片段所示:
java代码:
- int specMode = MeasureSpec.getMode(measureSpec);
- int specSize = MeasureSpec.getSize(measureSpec);
依据specMode的值,如果是AT_MOST,specSize 代表的是最大可获得的空间;如果是EXACTLY,specSize 代表的是精确的尺寸;如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。
当以EXACT方式标记测量尺寸,父元素会坚持在一个指定的精确尺寸区域放置View。在父元素问子元素要多大空间时,AT_MOST指示者会说给我最大的范围。在很多情况下,你得到的值都是相同的。
在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。
接下来的框架代码给出了处理View测量的典型实现:
java代码:
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int measuredHeight = measureHeight(heightMeasureSpec);
- int measuredWidth = measureWidth(widthMeasureSpec);
- setMeasuredDimension(measuredHeight, measuredWidth);
- }
- private int measureHeight(int measureSpec) {
- int specMode = MeasureSpec.getMode(measureSpec);
- int specSize = MeasureSpec.getSize(measureSpec);
- // Default size if no limits are specified.
- int result = 500;
- if (specMode == MeasureSpec.AT_MOST){
- // Calculate the ideal size of your
- // control within this maximum size.
- // If your control fills the available
- // space return the outer bound.
- result = specSize;
- }
- else if (specMode == MeasureSpec.EXACTLY){
- // If your control can fit within these bounds return that value.
- result = specSize;
- }
- return result;
- }
- private int measureWidth(int measureSpec) {
- int specMode = MeasureSpec.getMode(measureSpec);
- int specSize = MeasureSpec.getSize(measureSpec);
- // Default size if no limits are specified.
- int result = 500;
- if (specMode == MeasureSpec.AT_MOST){
- // Calculate the ideal size of your control
- // within this maximum size.
- // If your control fills the available space
- // return the outer bound.
- result = specSize;
- }
- else if (specMode == MeasureSpec.EXACTLY){
- // If your control can fit within these bounds return that value.
- result = specSize;
- }
- return result;
- }
- android中ViewGroup中onMeasure()方法 .
- 【Tech-Android-View】android中ViewGroup中onMeasure()方法
- Android对ViewGroup中OnMeasure方法的一些个人见解
- 关于Android中onMeasure方法
- Android中onMeasure方法详解
- Android中onMeasure方法的理解
- Android 继承ViewGroup必须重写onMeasure方法和onLayout方法
- 关于Android ViewGroup 中getChildDrawingOrder()方法
- Android ViewGroup中有关测量的方法
- 自定义viewgroup中onMeasure 和onlayout的一点理解
- Android中View的绘制过程 onMeasure方法
- Android中View的绘制过程 onMeasure方法简述
- Android中View的绘制过程 onMeasure和onLayout()方法
- Android中View的绘制过程 onMeasure方法简述
- android中对View的onMeasure()方法的理解
- android中对自定义View的onMeasure()方法的理解
- android中对View的onMeasure()方法的理解
- android中对View的onMeasure()方法的理解
- 虚函数实现多态(笔记整理)
- (JAVA 自学笔记)枚举续
- How to build x264/libx264.dll in Windows
- MFC中CString转换成char数组的问题
- 每天一道算法题1 判断字符串是否是回文字符串或者是否含有回文字符子串
- android中ViewGroup中onMeasure()方法 .
- 设计模式六大原则(1):单一职责原则
- Node.js Web Sockets and iOS
- android framework audio分析
- ViewGroup measure layout
- Spring实现定时启动任务
- 并发学习之:多线程编程中条件变量和虚假唤醒的讨论
- 提升Android应用视觉效果的10个UI设计技巧
- Windows下编译Qt的Mysql驱动(详细测试可用)