自定义view(一):onMeasure
2017-04-11 18:25
477 查看
首先呢 自定义view 肯定是继承自View类了,而View中关于我们需要重点关注的也就三个方法:
解释下:
onMeasure是用来测量view的大小的,跟xml布局相关
onLayout是用来计算view的大小的,决定View在ViewGroup中的位置
onDraw是用来绘制view的
举个例子:
就比如你老爹在郑东新区给你买了一块地皮,让你自己搞房地产。
你知道后,首先应该做的是什么?
肯定是先量量这块地皮有多大吧(onMeasure);
然后呢,就是设计在地皮上盖什么建筑吧(onLayout);
最后,肯定是动手开始干呗,盖房子!(onDraw)
这里提示下,今天只研究自定义view,先不管viewGroup。
很多同学看到自定义view,就急急忙忙去onDraw:
运行完之后,发现,可以了,但是有问题,矩形框不能随着layout_width和 layout_height变大变小,且view的宽高并没有包括矩形框
我们回过头看看canvas.drawRect(0,0,200,200,mPaintRect);这个方法:
参数1:矩形左边框与view左边框的距离
参数2:矩形上边框与view上边框的距离
参数3:矩形右边框与view左边框的距离 此处是宽度
参数4:矩形底边框与view上边框的距离 此处是高度
参数5:画笔
要想矩形框能随着layout_width和 layout_height变大变小,只需要将其中的参数3,4换成view的宽和高就行了。
getWidth()和getHeight()?
NO!!此处getWidth()和getHeight()都是0;
那怎么获取view的宽高呢? 三大方法里有一个onMeasure呀!
咱们去看
此方法里面有两个参数widthMeasureSpec,heightMeasureSpec。
别看它是int类型的值,就以为它就是个宽高
这俩参数中藏有玄机
要想获得测量的view宽高,得调用MeasureSpec.getSize
同时还能得到一个mode值
这个mode值,只有三种状态:
①、UNSPECIFIED(未指定)–不清楚;
②、EXACTLY(完全),–当xml中是match_parent时 或者指定值时;
③、AT_MOST(至多),–当xml中是wrap_content;
里面有一个很重要的方法
通过判断mode的三种状态,来给view设置不同的宽高:
xml是指定值或wrap_content —》AT_MOST
xml是match_parent 指定xxdp—->EXACTLY
onMeasure(int widthMeasureSpec, int heightMeasureSpec) onLayout(boolean changed, int left, int top, int right, int bottom) onDraw(Canvas canvas)
解释下:
onMeasure是用来测量view的大小的,跟xml布局相关
onLayout是用来计算view的大小的,决定View在ViewGroup中的位置
onDraw是用来绘制view的
举个例子:
就比如你老爹在郑东新区给你买了一块地皮,让你自己搞房地产。
你知道后,首先应该做的是什么?
肯定是先量量这块地皮有多大吧(onMeasure);
然后呢,就是设计在地皮上盖什么建筑吧(onLayout);
最后,肯定是动手开始干呗,盖房子!(onDraw)
这里提示下,今天只研究自定义view,先不管viewGroup。
很多同学看到自定义view,就急急忙忙去onDraw:
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //外边框 mPaintRect = new Paint(); mPaintRect.setAntiAlias(true);//抗锯齿 mPaintRect.setStyle(Paint.Style.STROKE);//设置描边 mPaintRect.setStrokeWidth(mStrokeWidth); mPaintRect.setColor(Color.BLACK); canvas.drawRect(0,0,200,200,mPaintRect); }
运行完之后,发现,可以了,但是有问题,矩形框不能随着layout_width和 layout_height变大变小,且view的宽高并没有包括矩形框
我们回过头看看canvas.drawRect(0,0,200,200,mPaintRect);这个方法:
参数1:矩形左边框与view左边框的距离
参数2:矩形上边框与view上边框的距离
参数3:矩形右边框与view左边框的距离 此处是宽度
参数4:矩形底边框与view上边框的距离 此处是高度
参数5:画笔
要想矩形框能随着layout_width和 layout_height变大变小,只需要将其中的参数3,4换成view的宽和高就行了。
getWidth()和getHeight()?
NO!!此处getWidth()和getHeight()都是0;
那怎么获取view的宽高呢? 三大方法里有一个onMeasure呀!
咱们去看
onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法:
此方法里面有两个参数widthMeasureSpec,heightMeasureSpec。
别看它是int类型的值,就以为它就是个宽高
这俩参数中藏有玄机
要想获得测量的view宽高,得调用MeasureSpec.getSize
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
同时还能得到一个mode值
int modeWidth = MeasureSpec.getMode(widthMeasureSpec); int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
这个mode值,只有三种状态:
MeasureSpec.AT_MOST,MeasureSpec.EXACTLY,MeasureSpec.UNSPECIFIED:
①、UNSPECIFIED(未指定)–不清楚;
②、EXACTLY(完全),–当xml中是match_parent时 或者指定值时;
③、AT_MOST(至多),–当xml中是wrap_content;
里面有一个很重要的方法
setMeasuredDimension(mWidth,mHeight);用来给设置onMeasure测量后的宽高,也就是说此方法才是决定了onMeasure的测量结果
通过判断mode的三种状态,来给view设置不同的宽高:
xml是指定值或wrap_content —》AT_MOST
xml是match_parent 指定xxdp—->EXACTLY
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int modeWidth = MeasureSpec.getMode(widthMeasureSpec); int modeHeight = MeasureSpec.getMode(heightMeasureSpec); int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); if (modeWidth == MeasureSpec.AT_MOST){ mWidth = defaultWidth; }else { mWidth = sizeWidth; } if (modeHeight == MeasureSpec.AT_MOST){ mHeight = defaultHeight; }else { mHeight = sizeHeight; } setMeasuredDimension(mWidth,mHeight); }
相关文章推荐
- Android 自定义View 中的OnMeasure的用法
- android 自定义view之onMeasure
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- 自定义View之OnMeasure()
- 自定义View之onMeasure()
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- Android学习自定义View(五)——自定义ViewGroup及其onMeasure()的理解
- 自定义View之onMeasure()
- 常用但忽略的anroid知识9- 自定义View之onMeasure()
- android自定义View中onMeasure的使用
- 【转】Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- android 自定义view中onMeasure()理解
- 自定义View之onMeasure()
- 自定义View之onMeasure()
- android 自定义view中onMeasure()理解
- 自定义View中的onMeasure,onLayout,onDraw
- 自定义View中为什么需要重写onMeasure()方法?
- 自定义View之onMeasure()
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例