当前位置: 首页 > news >正文

自定义View之重写onMeasure

一、重写onMeasure()来修改已有的View的尺寸

步骤

  1. 重写 onMeasure(),并调用 super.onMeasure() 触发原先的测量
  2. 用 getMeasuredWidth() 和 getMeasuredHeight() 取到之前测得的尺寸,利用这两个尺寸来计算出最终尺寸
  3. 使用 setMeasuredDimension() 保存尺寸

代码:

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//先执行原测量算法super.onMeasure(widthMeasureSpec, heightMeasureSpec);//获取原先的测量结果int measureWidth=getMeasuredWidth();int measureHeight=getMeasuredHeight();//利用原先的测量结果计算出新的尺寸if(measureWidth>measureHeight){measureWidth=measureHeight;}else{measureHeight=measureWidth;}//保存计算后的结果setMeasuredDimension(measureWidth,measureHeight);}
​

二、重写onMeasure()来全新计算自定义View的尺寸

步骤:

  1. 重写 onMeasure0) 把尺寸计算出来
  2. 把计算的结果用 resolveSize() 过滤一遍后保存

  @Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {...measuredWidth=...;measuredHeight=...;measuredWidth=resolveSize(measuredWidth,widthMeasureSpec);measuredHeight=resolveSize(measuredHeight,heightMeasureSpec);setMeasuredDimension(measuredWidth,measuredHeight);}

       onMeasure()方法的两个参数 widthMeasureSpec和heightMeasureSpec是父View对子View的尺寸限制,子View在计算自己尺寸的时候,需要遵守这两个参数所包含的限制MeasureSpec。

理解MeasureSpec

在 Android 中,View 的大小是由父容器和 View 自身的测量规格(MeasureSpec)共同决定的。

MeasureSpec 由大小和测量模式组成,测量模式有三种取值:

  1. UNSPECIFIED(未指定):父容器对子 View 没有施加任何限制,子 View 可以任意大小。

  2. EXACTLY(精确):父容器已经为子 View 精确指定了大小,子 View 应该匹配这个大小。

  3. AT_MOST(至多):子 View 可以是任何大小,但不能超过父容器指定的大小。

MeasureSpec 是通过静态方法 MeasureSpec.makeMeasureSpec() 创建的,该方法接受两个参数:大小和测量模式。在自定义 View 或者自定义布局中,我们通常会使用 MeasureSpec 来测量子 View 的大小,并根据测量模式来决定子 View 的大小。

在自定义 View 中,我们通常会在 onMeasure() 方法中使用 MeasureSpec 来测量 View 的大小。在这个方法中,我们可以通过 MeasureSpec.getMode() 和 MeasureSpec.getSize() 方法来获取测量模式和大小,然后根据这些信息来确定 View 的最终大小。

解释resolveSize()这个方法:

//代码简化,不是源码
public static int resolveSize(int size, int measureSpec) {final int specMode = MeasureSpec.getMode(measureSpec);final int specSize = MeasureSpec.getSize(measureSpec);switch (specMode) {case MeasureSpec.AT_MOST:if (specSize < size) {result = specSize | MEASURED_STATE_TOO_SMALL;} else {result = size;}break;case MeasureSpec.EXACTLY:result = specSize;break;case MeasureSpec.UNSPECIFIED:default:result = size;}
}

resolveSize()这个方法,父View传进来的尺寸限制measureSpec是由类型和尺寸值组成的,首先要调用MeasureSpec.getMode(measureSpec)方法和MeasureSpec.getSize(measureSpec)方法获取限制measureSpec的类型mode和size尺寸值。

限制的类型mode:

MeasureSpec.AT_MOST 限制上线

MeasureSpec.EXACTLY 限制固定尺寸

MeasureSpec.UNSPECIFIED 无限制

三、重写onMeasure()和onLayout()来全新计算自定义ViewGroup的内部布局

onMeasure()的重写,对于ViewGroup来说,包含三部分内容:

步骤:

  1. 调用每个子View的measure(),让子View自我测量
  2. 根据子View给出的尺寸,得出子View的位置,并保存它们的位置和尺寸
  3. 根据子View的位置和尺寸计算出自己的尺寸,并用setMeasuredDimension()保存

理解LayoutParams

       在父View里调用子View的getLayoutParams()方法,可以获得一个LayoutParams对象,它包含了xml文件里的layout_打头的参数的对应值,其中它的width和height这两个属性就分别对应了layout_width和layout_height的值,并且是转换过了的值。

    @Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
​for(int i=0;i<getChildCount();i++){View childView=getChildAt(i);LayoutParams lp=childView.getLayoutParams();//lp.height   lp.width}}

结合自己的可用空间来计算出对子View的宽度和高度的限制

可以根据layout_width和layout_height的值,分成三种情况:

第一种情况:固定值

不需要考虑可用空间的问题,直接用EXACTLY把子View尺寸限制为这个固定值就可以了。

第二种情况:match_parent

把子View的尺寸限制为固定值可用宽度或者高度

可用空间的判断方法:

根据自己的MeasureSpec中mode的不同:

1.EXACTLY/AT_MOST   可用空间:MeasureSpec中的size

2.UNSPECIFIED     可用空间:无限大

第三种情况:wrap_content

不能超过父View的边界的情况下,子View自己测量

public class SomeView extends ViewGroup {
...@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {...for(int i=0;i<getChildCount();i++){View childView=getChildAt(i);LayoutParams lp=childView.getLayoutParams();int selfwidthSpecMode=MeasureSpec.getMode(widthMeasureSpec);int selfwidthSpecSize=MeasureSpec.getSize(widthMeasureSpec);switch (lp.width){case MATCH_PARENT:if(selfwidthSpecMode==EXACTLY||selfwidthSpecMode==MeasureSpec.AT_MOST){childWidthSpec=MeasureSpec.makeMeasureSpec(selfwidthSpecSize-usedWidth,EXACTLY);}else{childWidthSpec=MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);}break;case  WRAP_CONTENT:if(selfwidthSpecMode==EXACTLY||selfwidthSpecMode==MeasureSpec.AT_MOST){childWidthSpec=MeasureSpec.makeMeasureSpec(selfwidthSpecSize-usedWidth,MeasureSpec.AT_MOST);}else{childWidthSpec=MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);}break;default:childWidthSpec=MeasureSpec.makeMeasureSpec(lp.width, EXACTLY);break;}}}
}

关于保存子View位置的两点说明

1.不是所有的Layout都需要保存子View的位置(因为有的Layout可以在布局阶段实时推导出子View的位置,例如LinearLayout)

2.有时候对某些子View需要重复测量两次或多次才能得到正确的尺寸和位置

重写onLayout()来摆放子View

    @Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {for(int i=0;i<getChildCount();i++){View childView=getChildAt(i);childView.layout(childLeft[i],childTop[i],childRight[i],childBottom[i]);}}

 

http://www.lryc.cn/news/275407.html

相关文章:

  • 专为Mac用户设计的思维导图软件MindNode 2023 for Mac助您激发创意!
  • Linux命令——用户和权限相关
  • linux反汇编工具: ida pro、rizinorg/cutter; ubuntu 22 flameshot延迟截图 以应对下拉菜单
  • 【INTEL(ALTERA)】使用NiosV/m 处理器,niosv-download 为什么会失败?
  • 【无线通信专题】NFC通信模式及可能的应用方式
  • pyinstaller生成的exe文件启动时间漫长的原因
  • C语言基本语句介绍
  • 【QT】QString类型中,Empty和NULL有什么区别在qt里,对比C#
  • 破壳而出:运维工程师在新科技热潮下的崛起与转型
  • 静态网页设计——贵州美食(HTML+CSS+JavaScript)
  • imgaug库指南(六):从入门到精通的【图像增强】之旅
  • stable diffusion 人物高级提示词(五)场景、特效、拍摄手法、风格
  • 智能分析网关V4智慧港口码头可视化视频智能监管方案
  • docker部署kibana
  • 【AI视野·今日CV 计算机视觉论文速览 第283期】Thu, 4 Jan 2024
  • sort实现自定义排序方法详解
  • 【攻防世界】Reverse——secret-galaxy-300 writeup
  • Github Copilot 快速入门
  • c# wpf 的触发器,触发器Trigger种类,每个触发器的使用说明
  • 计算机毕业设计 SpringBoot的乡村养老服务管理系统 Javaweb项目 Java实战项目 前后端分离 文档报告 代码讲解 安装调试
  • AMP 通讯RPMsg
  • 【ECMAScript】WebSocket模拟HTTP功能的实践:Promise+WebSocket+EventEmitter+Queue
  • Linux 软raid - - Barrier
  • 航空公司管理系统(迷你版12306)
  • 嵌入式硬件电路原理图之跟随电路
  • 学习录
  • MongoDB索引详解
  • 一文搞定JVM内存模型
  • 月报总结|Moonbeam 12月份大事一览
  • 现有网络模型的使用及修改(VGG16为例)