当前位置:网站首页>View绘制整体流程简析
View绘制整体流程简析
2022-06-13 06:04:00 【Little xian】
View 绘制流程
1、android中实现view的更新有几种方法?
你知道吗?其实android中实现view的更新有两种方法,一种是invalidate,另一种是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用。
怎么说呢?Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中调用。 鉴于此,如果要使用invalidate的刷新,那我们就得配合handler的使用,使异步非ui线程转到ui线程中调用,如果要在非ui线程中直接使用就调用postInvalidate方法即可,这样就省去使用handler的烦恼。
2、requestLayout在什么时候用呢?
当view确定自身已经不再适合现有的区域时,该view本身调用这个方法要求parent view(父类的视图)重新调用他的onMeasure onLayout来重新设置自己位置。特别是当view的layoutparameter发生改变,并且它的值还没能应用到view上时,这时候适合调用这个方法。
Measure 过程
1. 测量过程由上至下,在measure过程的最后,每个视图将存储自己的尺寸大小和测量规格。
2. measure过程会为一个View及其所有子节点的mMeasureWidth和mMeasuredHeight变量赋值, 该值可以通过getMeasuredWidth和getMeasuredHeight方法获得。
3. measure过程的核心方法: measure() - onMeasure() - setMeasuredDimension().
setMeasuredDimension是测量阶段的终极方法,在onMeasure()方法中调用,将计算得到的尺寸,传递给该方法,测量阶段结束。在自定义 视图时,不需要关心系统复杂的Measure过程,只需要调用setMeasuredDimension()设置根据MeasureSpec计算得到的尺寸即可。同时, onMeasure()方法也必须调用setMeasuredDimension()方法来设置重新测量之后的
以measureChildren为例的调用链图:
Layout 过程
1. 子视图的具体位置都是相对于父视图而言的。View的onLayout()方法为空实现,而ViewGroup的onLayout为abstract,因此,自定义的View要继承ViewGroup时,必须实现onLayout函数。
2. 在Layout过程中,子视图会调用getMeasuredWidth()和getMeasuredHeight()方法获取到measure过程得到mMeasuredWidth和mMeasuredHeight,作为自己的width和height。然后调用每一个子视图的layout(),来确定每个子视图在父视图中的位置。
Draw 过程
1. 所有视图最终都是调用View的draw方法进行绘制。 在自定义视图中, 也不应该复写该方法, 而是复写onDraw()方法进行绘制, 如果自定义的视图确实要复写该方法,先调用super.draw()完成系统的绘制,再进行自定义的绘制。
2. onDraw()方法默认是空实现,自定义绘制过程需要复写方法,绘制自身的内容。
3. dispatchDraw()发起对子视图的绘制,在View中默认为空实现,ViewGroup复写了dispatchDraw()来对其子视图进行绘制。自定义的ViewGroup不应该对dispatchDraw()进行复写。
如何对自定义View进行控制
1. 如果想控制View在屏幕上的渲染效果,就在重写onDraw()方法,在里面进行相应的处理。
2. 如果想要控制用户同View之间的交互操作,则在onTouchEvent()方法中对手势进行控制处理。
3. 如果想要控制View中内容在屏幕上显示的尺寸大小,就重写onMeasure()方法中进行处理。
4. 在 XML文件中设置自定义View的XML属性。
5. 如果想避免失去View的相关状态参数的话,就在onSaveInstanceState() 和 onRestoreInstanceState()方法中保存有关View的状态信息。
边栏推荐
- Leetcode- reverse string - simple
- Leetcode- find all missing numbers in the array - simple
- Concurrent programming -- countdownlatch combined with thread pool
- Security baseline check script - the road to dream
- Leetcode- complement of numbers - simple
- Use of Nacos configuration center
- [turn] explain awk (2)_ Combining formatted output with built-in variables to realize requirements
- 【DP之01背包】
- 软件测试——接口常见问题汇总
- USB status error and its cause (error code)
猜你喜欢
AUTOSAR actual combat tutorial pdf version
js将文本转成语言播放
Application virtual directory static resource configuration on tongweb
Software testing - Summary of common interface problems
Add attributes in storyboard and Xib (fillet, foreground...) Ibinspectable and ibdesignable
USB debugging assistant (20191028)
Echart柱状图:堆叠柱状图value格式化显示
[turn] explain awk (1)__ Awk Basics_ Options_ Program segment parsing and examples
Vagrant virtual machine installation, disk expansion and LAN access tutorial
Tongweb card, tongweb card, tongweb card
随机推荐
【自动化测试】Cypress手册
3. Postman easy to use
USB debugging assistant (20191028)
Self summarizing
Leetcode Hamming distance simple
Leetcode- distribute cookies - simple
Interrupt processing
Shardingsphere JDBC < bind table > avoid join Cartesian product
2021.9.29 learning log restful architecture
The technical analysis of ERP systems of the two camps in the world has been picked up many times.
ffmpeg 下载后缀为.m3u8的视频文件
Leetcode- reverse vowels in string - simple
Leetcode- number of words in string - simple
Turn to 2005
Leetcode- keyboard line - simple
自我总结ing
Alibaba cloud OSS file download cannot be resumed at a breakpoint
Security baseline check script - the road to dream
1+1>2,Share Creators可以帮助您实现
Nacos series registry principle and source code analysis