您好!欢迎来到爱源码

爱源码

热门搜索: 抖音快手短视频下载   

Android的测量 《影视源码》

  • 时间:2022-09-07 01:44 编辑: 来源: 阅读:300
  • 扫一扫,手机访问
摘要:Android的测量 《影视源码》
Android的视图是树形结构,视图的大小和视图组的大小会相互影响。有两个重要的参数:1。LayoutParams布局参数:1。了解LayoutParamsLayoutParams是布局参数,子视图告诉父容器(ViewGroup)如何通过布局参数放置自己。 ViewGroup的每个子类都有自己对应的LayoutParams类,比如LinearLayout。布局参数和框架布局。LayoutParams等。可以看出LayoutParams是对应ViewGroup子类的内部类。 MarginLayoutParams与外部间距有关。 与LayoutParams相比,MarginLayoutParams只增加了对上下左右外间距的支持。 实际上,LayoutParams的大部分实现类都是继承自MarginLayoutParams,因为基本上所有的父容器都支持设置子视图的外间距。 Image.png2优先级问题属性优先级问题MarginLayoutParams主要增加了四种外间距,上下左右。 在构造方法中,首先获取边距属性;如果值非法,获取horizontalMargin;如果值非法,则获取leftMargin和rightMargin属性(对于verticalMargin、topMargin和bottomMargin也是如此)。 据此,我们可以总结出这些属性的优先级。优先边距>:水平边距和垂直边距>:左边距和右边距、上边距和下边距3。LayoutParams参数在xml中定义视图,在Java代码3.1中直接生成视图对应的实例对象。如果XML布局中有特定的dp,那么layoutParams的值大于0。 3.2.如果是match _ parent公共静态final int match _ parent =-1;3.3.如果是wrap _ content public static final int wrap _ content =-2;LayoutParams.png4.addView/** *重载方法1:添加一个子视图*如果这个子视图还没有LayoutParams,则为子视图设置当前视图组的默认layout params * * @ param child */@ override public void add View(View child){ add View(child,-1);}/* * *重载方法二:在指定位置添加子视图*如果这个子视图还没有LayoutParams,则为子视图设置当前ViewGroup中的默认布局params * * @ param child * @ param index View(-1表示添加到末尾)*/@ override public void Add View(View child,int index){ If(child = = null){ throw new IllegalArgumentException("不能向ViewGroup添加null子视图");} layout params params = child . getlayout params();if(params = = null){ params = generateDefaultLayoutParams();if(params = = null){ throw new IllegalArgumentException(" generateDefaultLayoutParams()不能返回null ");} } addView(child,index,params);}/* * *重载方法3:添加子视图*使用当前ViewGroup的默认LayoutParams * * @ param child * @ param width作为layoutparams的宽度* @param height参数作为layout params的height */@ override public void Add View(View child,int width,int height){ finallayoutparams = generateddefaultlayout params();params.width =宽度;params.height = heightaddView(child,-1,params);}/* * *重载方法4:添加子视图* * @ param child * @ param param params使用传递的layout params */@ override public void addview(view child,layout params params){ addview(child,-1,params);}/* * *重载方法5:添加子视图,* * * @ param child * @ param index视图将被添加到ViewGroup * @ paramparamparams使用传递的layout params */@ override public void Add View(View child,int index,layout params params){ super . Add View(child,index,params);}5.generateDefaultLayoutParams方法addView源代码查找generateDefaultLayoutParams方法,获取默认LayoutParamsViewGroup的generateDefaultLayoutParams方法,protected layout params generateDefaultLayoutParams(){ return new layout params(layout params;wrap _ contentlayoutparamswrap _ content);}6.checkLayoutParams方法addView源代码继续往下看,可以找到ViewGroup的checkLayoutParams方法,受保护的布尔checkLayoutParams的checkLayoutParams方法(view group . layout params p){ return p!= null}非ViewGroup的checkLayoutParams方法。具体源代码请参见@ override protected boolean checklayoutparams(view group . layout params p){ return p linear layout . layout params实例;}2.MeasureSpec1 .测量视图大小(onMeasure)视图的大小不仅由自身决定,还受父控件的影响。它自身的大小也可能受到子视图的影响,on measure方法会被重复调用。 2.MeasureSpec参数MeasureSpec是一个32位的int值。高2位存储测量模式,低30位存储具体的测量尺寸。手机像素超过这个数值几乎是不可能的。 在源代码中我们可以看到MeasureSpec.png3的定义Get参数@ override protected void on measure(intwidthMeasureSpec,inthightmeasurespec){//取出width size measurespec . getsize(widethMeasureSpec)的精确正切值;//取出宽度测量模式int widthmodemeasurespec . get mode(widthmeasurespec);//取出height int heightsizemeasurespec . getsize的精确正切值(heightsmeasurespec);//取出高度的测量模式,int HeightModeMeasureSpec。get mode(heightmmeasurespec);}4.三种测量模式有三种测量模式。二进制值描述了未指定00的默认值。父控件不限制子视图,子视图可以设置为任意大小。 EXACTLY01表示父控件已经精确指定了子视图的大小。 AT_MOST10表示子视图的具体大小没有大小限制,但是有上限,一般是父视图的大小。 5.获取子视图的MeasureSpec (GetChildEasureSpec)。当要度量视图组时,如果受子视图影响,则需要度量子视图、getChildEasureSpec方法和度量子视图。 子视图的测量需要父布局视图组的MeasureSpec,加上它自己的LayoutParams布局参数来测量。 如果修改了视图的宽度和高度,就不要调用Super了。On Measure (widethmeasurespec,heightsmeasurespec);调用setmeasureddimension(widethsize,heightsize);该功能 public static int getchildmeasurespec(int spec,int padding,int child dimension){//父控件的度量模式int spec mode = measure spec . getmode(spec);//父控件的测量大小intspecsize = measure spec . getsize(spec);//减去内边距int size = math.max (0,Specsize-padding);int resultSize = 0;int result mode = 0;Switch (spec mode) {//parent对us case measure spec . exact://parent layout accurate mode if(child dimension >:= 0){//子视图的布局参数也由size决定,所以子视图模式是精确模式resultSize = childDimensionresultMode = MeasureSpec。完全正确;} else if(child dimension = = layout params。MATCH _ PARENT){//孩子想要和我们一样大,那就这样吧。//子视图是match _ parent,那也是精确模式。父布局有多大,子视图就有多大。resultSize = sizeresultMode = MeasureSpec。完全正确;} else if(child dimension = = layout params。WRAP_CONTENT) { //子级想要确定自己的大小。不可能//比我们大。//子视图是WRAP_CONTENT,最多不能超过父布局的大小,所以最多resultSize = sizeresultMode = MeasureSpec。最多_个;}破; 返回measure spec . makemasurespec(resultSize,result mode);}大家应该在书里看到过image . png 6 } Android开发艺术.当View onSizeChanged时调用这个函数。 由于视图的大小不仅受视图本身的控制,还受父控件的影响,所以在确定视图大小时,我们最好使用系统提供的onSizeChanged回调函数。 7.获取测量的大小(getMeasuredWidth)和相应的值可以在measure()过程后得到;由setMeasuredDimension()方法设置。8.获取layout()过程结束后的宽度和高度(getWidth);通过从视图右侧的坐标中减去左侧的坐标来计算。3.具体使用@ override protected void on measure(int width measure spec,int high measure spec){//父布局的宽度和高度intviewgroupwidth = measure spec . getsize(widethmeasurespec);int view group height = measure spec . getsize(heightmesspec);//遍历(int i = 0的所有子视图;我& ltget child count();i++){ View child View = get child at(I);//如果视图可见If(child view . get visibility()= = view . visible){//子视图的布局参数Layout params = child view . getlayout params();//子视图的measureSpec、传入父布局的measureSpec以及父布局设置的内部边距。子视图的大小(大于0为确定大小,-1为match_parent,-2为wrap _ content)int childmeasurespec = getchildmeasurespec(widethmeasurespec,leftpadding+rightpadding,childlayoutparams . width);int childheightsmeasurespec = getChildMeasureSpec(heightsmeasurespec,topPadding + bottomPadding,childlayoutparams . height);//子视图调用measure获取childview.measure的准确宽度和高度(childwidth measurespec,child height measure spec);//子视图的宽度和高度,int child View = child View . getmeasure bandwidth();int childMeasureHeight = child view . getmeasuredheight();}//Re-measure view group int width mode = measure spec . get mode(widthmeasurespec);int height mode = measure spec . get mode(heightmaurespec);int real width = width mode = = measure spec。确实如此。view group width:view group needwidth;int real height = height mode = = measure spec。确实如此。view group height:viewgroupneeheight;setMeasuredDimension(realWidth,real height);}4.参考《Android开发艺术探索》Android自行开发的集合视图


  • 全部评论(0)
资讯详情页最新发布上方横幅
最新发布的资讯信息
【技术支持|常见问题】1556原创ng8文章搜索页面不齐(2024-05-01 14:43)
【技术支持|常见问题】1502企业站群-多域名跳转-多模板切换(2024-04-09 12:19)
【技术支持|常见问题】1126完美滑屏版视频只能显示10个(2024-03-29 13:37)
【技术支持|常见问题】响应式自适应代码(2024-03-24 14:23)
【技术支持|常见问题】1126完美滑屏版百度未授权使用地图api怎么办(2024-03-15 07:21)
【技术支持|常见问题】如何集成阿里通信短信接口(2024-02-19 21:48)
【技术支持|常见问题】算命网微信支付宝产品名称年份在哪修改?风水姻缘合婚配对_公司起名占卜八字算命算财运查吉凶源码(2024-01-07 12:27)
【域名/主机/服务器|】帝国CMS安装(2023-08-20 11:31)
【技术支持|常见问题】通过HTTPs测试Mozilla DNS {免费源码}(2022-11-04 10:37)
【技术支持|常见问题】别告诉我你没看过邰方这两则有思想的创意广告! (2022-11-04 10:37)

联系我们
Q Q:375457086
Q Q:526665408
电话:0755-84666665
微信:15999668636
联系客服
企业客服1 企业客服2 联系客服
86-755-84666665
手机版
手机版
扫一扫进手机版
返回顶部