A MeasureSpec encapsulates the layout requirements passed from parent to child. Each MeasureSpec represents a requirement

for either the width or the height. A MeasureSpec is comprised of a size and a mode. There are three possible modes:

UNSPECIFIED
  The parent has not imposed any constraint on the child. It can be whatever size it wants.
EXACTLY
  The parent has determined an exact size for the child. The child is going to be given those bounds regardless of how big it wants to be.
AT_MOST
  The child can be as large as it wants up to the specified size.

1.使用 View.resolveSize(int size,int measureSpec)

public static int resolveSize(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
case MeasureSpec.UNSPECIFIED:
result = size;
break;
case MeasureSpec.AT_MOST:
result = Math.min(size, specSize);
break;
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}

2.源码

    public static class MeasureSpec {
private static final int MODE_SHIFT = 30;
private static final int MODE_MASK = 0x3 << MODE_SHIFT; public static final int UNSPECIFIED = 0 << MODE_SHIFT; public static final int EXACTLY = 1 << MODE_SHIFT; public static final int AT_MOST = 2 << MODE_SHIFT; public static int makeMeasureSpec(int size, int mode) {
return size + mode;
} public static int getMode(int measureSpec) {
return (measureSpec & MODE_MASK);
} public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
} }

示例:

int measureSpec = makeMeasureSpec(4, EXACTLY);
getSize(measureSpec);
getMode(measureSpec);

-----------------------makeMeasureSpec --------------------- mode+size

mode:            1000  0000  0000  0000  0000  0000  0000  000
size:                                       100
measureSpec: 1000  0000  0000  0000  0000  0000  0000  100

---------------getMode  ---------------- MODE_MASK & measureSpec

MODE_MASK:  1100  0000  0000  0000  0000  0000  0000  0000
measureSpec:  1000  0000  0000  0000  0000  0000  0000  100

  

3.示例ListView.measureItem(View child)

private void measureItem(View child) {
ViewGroup.LayoutParams p = child.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
} int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
mListPadding.left + mListPadding.right, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
child.measure(childWidthSpec, childHeightSpec);
}

 4.重写ListView与ScrollView 兼容:

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Parameters

  • widthMeasureSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
  • heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

重写:

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
  super.onMeasure(widthMeasureSpec, expandSpec); }

ListView

   @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Sets up mListPadding
super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec); int childWidth = 0;
int childHeight = 0; mItemCount = mAdapter == null ? 0 : mAdapter.getCount();
if (mItemCount > 0 && (widthMode == MeasureSpec.UNSPECIFIED ||
heightMode == MeasureSpec.UNSPECIFIED)) {
final View child = obtainView(0); measureScrapChild(child, 0, widthMeasureSpec); childWidth = child.getMeasuredWidth();
childHeight = child.getMeasuredHeight(); if (recycleOnMeasure()) {
mRecycler.addScrapView(child);
}
} if (widthMode == MeasureSpec.UNSPECIFIED) {
widthSize = mListPadding.left + mListPadding.right + childWidth +
getVerticalScrollbarWidth();
} if (heightMode == MeasureSpec.UNSPECIFIED) {
heightSize = mListPadding.top + mListPadding.bottom + childHeight +
getVerticalFadingEdgeLength() * 2;
} if (heightMode == MeasureSpec.AT_MOST) {
// TODO: after first layout we should maybe start at the first visible position, not 0
heightSize = measureHeightOfChildren(widthMeasureSpec, 0, NO_POSITION, heightSize, -1);
} setMeasuredDimension(widthSize, heightSize);
mWidthMeasureSpec = widthMeasureSpec;
}

android自定义控件(一)MeasureSpec 与 ListView.onMeasure的更多相关文章

  1. android自定义控件(6)-详解在onMeasure()方法中如何测量一个控件尺寸

    今天的任务就是详细研究一下protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法.如果只是说要重写什么方法有什么 ...

  2. Android自定义控件——有弹性的ListView,ScrollView

    上一次我们试验了有弹性的ScrollView.详情 这一次,我们来试验有弹性的ScrollView. 国际惯例,效果图: 主要代码: [java] view plaincopy import andr ...

  3. android 自定义控件之NetWorkImageView 处理listview等控件中的图片加载乱序问题

    0.调用: BaseAdapter中设置方法 holder.iv.loadImage(url); adapter_xxx.xml 中 控件需要用 xxx.NetWorkImageView 1 NetW ...

  4. android自定义控件(8)-利用onMeasure测量使图片拉伸永不变形,解决屏幕适配问题

    使用ImageView会遇到的问题 在Android应用中,都少不了图片的显示,ImageView,轮播图,ViewPager等等,很多都是来显示图片的,很多时候,我们都希望图片能够在宽度上填充父窗体 ...

  5. Android自定义控件之自定义ViewGroup实现标签云

    前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...

  6. Android自定义控件之自定义属性

    前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...

  7. Android自定义控件之基本原理

    前言: 在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理. 自 ...

  8. Android自定义控件1

    概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...

  9. 一起来学习Android自定义控件1

    概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...

随机推荐

  1. CCNA2.0笔记_子网划分

    http://files.cnblogs.com/files/airoot/%E5%AD%90%E7%BD%91%E5%88%92%E5%88%86.zip 网络 默认子网掩码 A类 255.0.0. ...

  2. Oracle基础(三)数据库管理

    上篇介绍了Oracle数据库的基本操作指令:增.删.改.查以下针对数据库的管理进行介绍 数据库角色介绍 数据管理员:  至少有一个数据库管理员dba. 职责:安装和升级oracel数据库 建库,表空间 ...

  3. Android Studio无法启动,gradle下载不了 提示“building “ 项目名”gradle project info”

    Google在2013年I/O大会上发布了Android Studio,AndroidStudio是一个基于IntelliJ思想的新的Android开发工具.下面介绍一下Android Studio安 ...

  4. Python-Scrapy创建第一个项目

    创建项目 在开始爬取之前,您必须创建一个新的Scrapy项目.进入您打算存储代码的目录中,运行下列命令: scrapy startproject tutorial 1 该命令行将会创建包含下列内容的t ...

  5. 移动端上下滑动事件之--坑爹的touch.js

    原文   http://blog.csdn.net/minidrupal/article/details/39611605 移动端页面的盛行,微信的便利的页面推广等等,让越来越多的css3效果和htm ...

  6. java web学习笔记-jsp篇

    1.java web简介 1.1静态页面与动态页面   表现形式 所需技术 静态网页 网页内容固定,不会更新 html,css 动态网页 网页内容由程序动态显示,自动更新 html,css,DB,ja ...

  7. Ubuntu上安装与配置JDK1.8

    Ubuntu上安装与配置JDK1.8 一.下载 下载JDK,由于是Ubuntu. 所以去官网下载tar.gz格式的就可以(ubuntu使用浏览器下载网速比較慢,所以推荐到window上下载好). ht ...

  8. cobbler 修改 distro_signatures.json

    edit file in /var/lib/cobbler/distro_signatures.json and restart cobblerd service 转自: https://lists. ...

  9. ARM汇编语言(3)(寄存器读写控制外设)

    DAY4:ARM汇编(4) 一,GPIO编程     连接内存(二级cache),用来寻址:连接外设,用来控制:   1,GPIO,General-Purpose IO ports,通用输入输出端口, ...

  10. 蓝桥杯 C/C++参考题目 开平方(数学题,迭代法求开方)

    开平方 如果没有计算器,我们如何求2的平方根?可以先猜测一个数,比如1.5,然后用2除以这个数字.如果我们猜对了,则除法的结果必然与我们猜测的数字相同.我们猜测的越准确,除法的结果与猜测的数字就越接近 ...