开始之前不得不说到view的绘制流程,,,请参考:绘制流程 。。。
1. onMeasure a. 属于View的方法,用来测量自己和内容的来确定宽度和高度 b. view的measure方法体中会调用onMeasure
2. onLayout a. 属于ViewGroup的方法,用来为当前ViewGroup的子元素的位置和大小 b. View的layout方法体中会调用onLayout
3.onMeasure和onLayout onMeasure在onLayout之前调用
4. 设置background后,会重新调用onMeasure和onLayout
onMeasure测量子VIEW大小后调用LAYOUT布局 所以初始化的时候会多次调用onlayout方法
(一)onMeasure:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onMeasure方法在控件的父元素正要放置它的子控件时调用.它会问一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec.
它们指明控件可获得的空间以及关于这个空间描述的元数据.
比返回一个结果要好的方法是你传递View的高度和宽度到setMeasuredDimension方法里.
接下来的代码片段给出了如何重写onMeasure.注意,调用的本地空方法是来计算高度和宽度的.它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值.
@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 封装了父布局传递给子布局的布局要求,每个MeasureSpec 代表了一组宽度和高度的要求。一个MeasureSpec 由大小和模式组成。
它有三种模式:
UNSPECIFIED( 未指定), 父元素不对自元素施加任何束缚,子元素可以得到任意想要的大小;
EXACTLY( 完全) ,父元素决定自元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小;
AT_MOST( 至多) ,子元素至多达到指定大小的值。
它常用的三个函数:
1.static int getMode(int measureSpec): 根据提供的测量值( 格式) 提取模式( 上述三个模式之一)
2.static int getSize(int measureSpec): 根据提供的测量值( 格式) 提取大小值( 这个大小也就是我们通常所说的大小)
3.static int makeMeasureSpec(int size,int mode): 根据提供的大小值和模式创建一个测量值( 格式)
这个类的使用呢,通常在view 组件的onMeasure 方法里面调用但也有少数例外
在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getMode和getSize来译解,如下面的片段所示:
int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec);
依据specMode的值,如果是AT_MOST,specSize 代表的是最大可获得的空间;如果是EXACTLY,specSize 代表的是精确的尺寸;如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。
当以EXACT方式标记测量尺寸,父元素会坚持在一个指定的精确尺寸区域放置View。在父元素问子元素要多大空间时,AT_MOST指示者会说给我最大的范围。在很多情况下,你得到的值都是相同的。
在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。
接下来的框架代码给出了处理View测量的典型实现:
@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; }
(二)onLayout:
在onLayout之中尽量不要做onMeasure的事情,,,是看到这篇文章,,并没有验证: 文章。。。
直接看例子吧 ,,讲的很清楚了。。。
HelloViewGroup:
public class HelloViewGroup extends ViewGroup { private float mTouchStartX; private float mTouchStartY; private int x = 0; private int y = 0; private int mSelectView = -1; Context mContext; public HelloViewGroup(Context context) { super(context); mContext = context; // TODO Auto-generated constructor stub } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (mSelectView != -1) { View v = getChildAt(mSelectView); if (v != null) v.layout(x, y, x + 300, y + 500); return; } View v = getChildAt(0); if (v != null) v.layout(x, y, x + 300, y + 500); View v1 = getChildAt(1); if (v1 != null) v1.layout(x + 300, y, x + 600, y + 500); View v2 = getChildAt(2); if (v2 != null) v2.layout(x + 600, y, x + 900, y + 500); } public boolean onTouchEvent(MotionEvent event) { float down_x = event.getX(); float down_y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (mSelectView == -1) { for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); if ((child.getLeft() < down_x && down_x < child.getRight()) && (child.getTop() < down_y && down_y < child .getBottom())) { mSelectView = i; break; } } } break; case MotionEvent.ACTION_MOVE: x = (int) event.getRawX() - 100; y = (int) event.getRawY() - 100; this.requestLayout(); break; case MotionEvent.ACTION_UP: mSelectView = -1; break; } return true; } }
MainActivity:
public class MainActivity extends Activity { Context mContext; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); HelloViewGroup my = new HelloViewGroup(this); mContext = this; ImageView mIcon = new ImageView( mContext); ImageView mIcon1 = new ImageView( mContext); ImageView mIcon2 = new ImageView( mContext); mIcon.setImageResource(R.drawable.nvshen1); mIcon1.setImageResource(R.drawable.nvshen2); mIcon2.setImageResource(R.drawable.nvshen5); my.addView(mIcon); my.addView(mIcon1); my.addView(mIcon2); setContentView(my); } }