自定义view的measureSpec是谁的mode和size
2016-04-09 15:58
495 查看
一、简单背景介绍
在自定义view(不是ViewGroup)时,需要重写父类的onMeausre(), onLayout(), onDraw():protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
看了很多书和文章,都介绍了widthMeasureSpec是什么东西。他是一个
MeasureSpec,32位的int,前两位记录了他的mode,后30位记录了他的size。
mode如下:
Mode | Name | Cool |
---|---|---|
00 | UNSPECIFIED | 0 |
01 | EXACTLY | 1073741824 |
10 | AT_MOST | -2147483648 |
二、探索onMeasure中的widthMeasureSpec
自定义view的xml文件如下:<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.wangxuan.customviewheight.CustomView android:layout_width="300px" android:layout_height="400px" android:background="@color/colorPrimary" /> </RelativeLayout>
自定义view的java代码:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSpec = MeasureSpec.getMode(widthMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int heightSpec = MeasureSpec.getMode(heightMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); Log.e(TAG, "before wspec:" + widthSpec + ",w:" + width + ",hspec:" + heightSpec + ",h:" + height); if (widthSpec == MeasureSpec.EXACTLY) { Log.e(TAG, "width EXACTLY"); } else if (widthSpec == MeasureSpec.AT_MOST) { Log.e(TAG, "width AT_MOST"); } else { Log.e(TAG, "width UNSPECIFIED"); } if (heightSpec == MeasureSpec.EXACTLY) { Log.e(TAG, "height EXACTLY"); } else if (heightSpec == MeasureSpec.AT_MOST) { Log.e(TAG, "height AT_MOST"); } else { Log.e(TAG, "height UNSPECIFIED"); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); Log.e(TAG, "after wspec:" + widthSpec + ",w:" + width + ",hspec:" + heightSpec + ",h:" + height); if (widthSpec == MeasureSpec.EXACTLY) { Log.e(TAG, "width EXACTLY"); } else if (widthSpec == MeasureSpec.AT_MOST) { Log.e(TAG, "width AT_MOST"); } else { Log.e(TAG, "width UNSPECIFIED"); } if (heightSpec == MeasureSpec.EXACTLY) { Log.e(TAG, "height EXACTLY"); } else if (heightSpec == MeasureSpec.AT_MOST) { Log.e(TAG, "height AT_MOST"); } else { Log.e(TAG, "height UNSPECIFIED"); } }
运行结果:
E/CustomView: before wspec:1073741824,w:300,hspec:-2147483648,h:1038 E/CustomView: width EXACTLY E/CustomView: height AT_MOST E/CustomView: after wspec:1073741824,w:300,hspec:-2147483648,h:1038 E/CustomView: width EXACTLY E/CustomView: height AT_MOST E/CustomView: before wspec:1073741824,w:300,hspec:1073741824,h:400 E/CustomView: width EXACTLY E/CustomView: height EXACTLY E/CustomView: after wspec:1073741824,w:300,hspec:1073741824,h:400 E/CustomView: width EXACTLY E/CustomView: height EXACTLY
三、解释
在RelativeLayout中的onMeasure中会对其所有的子view进行测量。因为RelativeLayout的水平和竖直方向都是相对的,所以在进行测量其子view的大小时,需要分水平和竖直两个方向分别测量,即先测量width,在测量height。下面只给出了测量width的源代码:int count = views.length; for (int i = 0; i < count; i++) { View child = views[i]; if (child.getVisibility() != GONE) { LayoutParams params = (LayoutParams) child.getLayoutParams(); int[] rules = params.getRules(layoutDirection); applyHorizontalSizeRules(params, myWidth, rules); measureChildHorizontal(child, params, myWidth, myHeight); if (positionChildHorizontal(child, params, myWidth, isWrapContentWidth)) { offsetHorizontalAxis = true; } } }
可以看到,RelativeLayout在测量子view的时候,是调用了
child.getLayoutParams()。而myWidth和myHeight则是RelativeLayout的width和height。所以可知自定义view的widthMeasureSpec是自身xml规定的size和mode。那传进去的父view的myWidth和myHeight实际是为了帮助子 view进行一些测量,比如match_parent时,需要通过父亲的大小来界定子 view的大小。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories