`
zhangfy068
  • 浏览: 143667 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

(转)Android中自定义View的MeasureSpec使用

阅读更多

1.如果没有再配置文件中精确指定多大,而是由子VIEW的大小所决定的。可以测通过测量。

 

 

当调用View.measure(0,0)意思是 测量该measure的大小。 如果会递归调用子View的onmeasure()方法(该方法可被override,在此方法中调用setMeasureWidth来设置宽,高。)

测量的值与实际的值不一样。。比如一张1024*768的图,测量出来,需要这么大位置,但实际上只有540*768的显示位置。getWidth()与getMeasureWidth()的区别。。

 View 绘制流程http://blog.csdn.net/qinjuning/article/details/7110211

 

有时,Android系统控件无法满足我们的需求,因此有必要自定义View。具体方法参见官方开发文档:http://developer.android.com/guide/topics/ui/custom-components.html

 

一般来说,自定义控件都会去重写View的onMeasure方法,因为该方法指定该控件在屏幕上的大小。

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

onMeasure传入的两个参数是由上一层控件传入的大小,有多种情况,重写该方法时需要对计算控件的实际大小,然后调用setMeasuredDimension(int, int)设置实际大小。

 

onMeasure传入的widthMeasureSpec和heightMeasureSpec不是一般的尺寸数值,而是将模式和尺寸组合在一起的数值。我们需要通过int mode = MeasureSpec.getMode(widthMeasureSpec)得到模式,用int size = MeasureSpec.getSize(widthMeasureSpec)得到尺寸。

 

mode共有三种情况,取值分别为MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY, MeasureSpec.AT_MOST。

 

MeasureSpec.EXACTLY是精确尺寸,当我们将控件的layout_width或layout_height指定为具体数值时如andorid:layout_width="50dip",或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。

 

MeasureSpec.AT_MOST是最大尺寸,当控件的layout_width或layout_height指定为WRAP_CONTENT时,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸即可。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。

 

MeasureSpec.UNSPECIFIED是未指定尺寸,这种情况不多,一般都是父控件是AdapterView,通过measure方法传入的模式。

 

因此,在重写onMeasure方法时要根据模式不同进行尺寸计算。下面代码就是一种比较典型的方式:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
setMeasuredDimension(getMeasuredLength(widthMeasureSpec, true), getMeasuredLength(heightMeasureSpec, false)); 
} 


private int getMeasuredLength(int length, boolean isWidth) { 
           int specMode = MeasureSpec.getMode(length); 
           int specSize = MeasureSpec.getSize(length); 
           int size; 
           int padding = isWidth ? getPaddingLeft() + getPaddingRight() 
                                : getPaddingTop() + getPaddingBottom(); 
           if (specMode == MeasureSpec.EXACTLY) { 
                    size = specSize; 
           } else { 
                   size = isWidth ? padding + mWave.length / 4 : DEFAULT_HEIGHT + padding; 
                  if (specMode == MeasureSpec.AT_MOST) { 
                         size = Math.min(size, specSize); 
                  } 
          } 
    return size; 
}

 

@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(measureWidth(widthMeasureSpec),
				measureHeight(heightMeasureSpec));
	}

	private int measureWidth(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		} else {
			result = (int) (getPaddingLeft() + getPaddingRight()
					+ (count * 2 * radius) + (count - 1) * radius + 1);
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			}
		}
		return result;
	}

	private int measureHeight(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		} else {
			result = (int) (2 * radius + getPaddingTop() + getPaddingBottom() + 1);
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			}
		}
		return result;
	}

}

 

分享到:
评论

相关推荐

    Android自定义View

    介绍如何自定义View,并重写onMeasure方法重新测量高度和宽度, 讲解MeasureSpec类的使用方法 详见博文 http://blog.csdn.net/a87b01c14/article/details/50359226

    android 实现FlowLayout 流线布局(自定义ViewGroup)

    1、深化自定义View的概念 2、将MeasureSpec、View的绘制流程、Layoutparams等分散的知识点整合成一个demo。 #项目灵感 笔者经验也是有限,此文章主要借鉴张鸿洋前辈的博客。 原文地址:...

    Android开发艺术探索.任玉刚(带详细书签).pdf

    4.4.1 自定义View的分类 200 4.4.2 自定义View须知 201 4.4.3 自定义View示例 202 4.4.4 自定义View的思想 217 第5章 理解RemoteViews 218 5.1 RemoteViews的应用 218 5.1.1 RemoteViews在通知栏上的应用 219 ...

    Android开发艺术探索

     4.4.1 自定义View的分类 / 200  4.4.2 自定义View须知 / 201  4.4.3 自定义View示例 / 202  4.4.4 自定义View的思想 / 217  第5章 理解RemoteViews / 218  5.1 RemoteViews的应用 / 218  5.1.1 RemoteViews...

    Android开发之自定义CheckBox

    要实现的效果如下 考虑到关键是动画效果,所以直接继承View。...View measure流程的MeasureSpec: /** * A MeasureSpec encapsulates the layout requirements passed from parent to child. * Each MeasureSp

    Android视图控件架构分析之View、ViewGroup

    在Android中,视图控件大致被分为两类,即ViewGroup和View,ViewGroup控件作为父控件,包含并管理着子View,通过ViewGroup和View便形成了控件树,各个ViewGoup对象和View对象就是控件树中的节点。在控件树中,以树的...

    Android实现Z轴布局效果

    如果需要在布局中创造...layoutParams的作用是向父布局请求布局参数(MeasureSpec),这个参数会在View inflate时添加到布局中,我们如果使用LayoutParams将会得到很大的方便 // 这里继承FrameLayout的LayoutParams即可 p

    android异步生成图片的示例代码

    下面来说说在Android上如果异步生成图片,通过xml布局用View排版好图片样式,在子线程生成一张图片,以满足生成用来分享的图片等需求(生成图片前设置可变元素,如用户的头像,昵称等)。 效果 点击按钮生成图片: ...

    浅析Android中getWidth()和getMeasuredWidth()的区别

    结论:getMeasuredWidth()获取的是view原始的大小,也就是这个view在XML文件中配置或者是代码中设置的大小。getWidth()获取的是这个view最终显示的大小,这个大小有可能等于原始的大小也有可能不等于原始大小。 1....

    WelcomeVideoPager-仿蚂蜂窝自由行和慕课网视频欢迎页.zip

    自定义播放视频的CustomVideoView 在这个自定义View里面提供一个播放视频的方法。用户只需要传入播放路径就可以了,并且可一循环播放。package cn.bluemobi.dylan.welcomevideopager; import android.content....

Global site tag (gtag.js) - Google Analytics