当前位置:网站首页>One of the series of composition principle analysis, the design principle of composition
One of the series of composition principle analysis, the design principle of composition
2022-07-23 10:15:00 【Lost summer】
Preface :
compose At present, it is more and more popular ,19 year
Its principle is far from complete in an article , So I'm going to make a series .
Today's first article , Let's talk about Compose Design principle , Talk in the most popular way .
One . Native XML Disadvantages of layout
speak compose Before principle , We can think about it first , Native XML What are the problems with the layout of ?
1. Performance issues .
Although than H5 The first kind is much better , But its own disadvantages also exist . I think the biggest problem is when nesting multiple layers , The computational complexity of the plot increases exponentially . So this leads to when our layout nesting levels are too many , You will find that there will be a jam in the refresh , Rendering efficiency is particularly low .
2. Loading problem .
Layout file XML Finally packed into APK When , Still XML In the form of . But the XML convert to ViewGroup In fact, it is in inflate perhaps setContentView When ( In fact, the second method is also called in the first way ). And read XML Belong to IO operation , analysis XML become ViewGroup There is also a lot of logic , So this time-consuming is unavoidable . General page loading XML All the time 30ms about , This leads to startup Activity When , It will cost more 30ms Time for .
3. Cross platform issues .
The reason is relatively simple , If H5 You only need to develop one , Android /IOS/ Web pages can be used . But if the three terminals are developed separately , It takes three times as much work to develop .
Two . How to solve :
I thought about this problem a long time ago , So I have also made some attempts in personal projects , Some works well , Others failed .
Solve performance problems
For the first performance problem mentioned above , I think the biggest problem is to ensure absolute safety , So there are too many useless calculations , And it's an exponential calculation . So I sometimes have some very complex layouts or layouts with many elements in my personal projects , Will try to return directly to the width and height , Instead of counting its children View The width and height of . Wait until the execution draw When , Calculate at one time , Each child View Calculate only once , Then use it directly canvas draw . The final effect is actually good , See my other article for details :
Solve the loading problem :
For the second loading problem above , My friend gave me a hint before , Insert customization when compiling and packaging gradle Mission , Convert the layout file to compiled class Package files . This is a simple implementation , The effect is actually very good . But the problem is that there will be a lot of customization View Also loaded xml file , So there are too many points to be compatible . and XML Replace with View after , The code at the call also needs to be modified , Then recompile , It will become very complicated . Of course , It works in theory , It's just a matter of complexity .
Solve cross platform problems :
java It's actually cross platform , Then based on java In fact, Android can also achieve cross platform . At least cross platform for a certain function . and java The principle of cross platform is similar , Layout is responsible for logical calculation , Finally, just issue the drawing command to Native layer , Then forward it to the hardware side .
3、 ... and .Compose How to solve
I've seen it preliminarily compose Principle , I was stunned , Its design idea coincides with my optimization idea above . Of course , There must be differences .
Compose The calculation area in the middle is based on LayoutNode The way , Every view Corresponding to one LayoutNode. Simply speaking , Namely Compose Optimized for measurement , Each node will only calculate once , Efficiency is greatly improved .
The problem of loading does not exist , Because the layout itself is to use KT Written , So there is no problem of parsing .
Cross platform , As long as the virtual machine is supported, its parsing logic can be executed , Draw the final product hardware . The rendering carrier on Android is still canvas. in other words , In fact, on Android , Just write the layout in a different way , The final rendering method has not changed .
边栏推荐
- ARP Spoofing protection of network security
- Leetcode-99. restore binary search tree
- [node middle tier practice (III)] --- template engine Pug
- Ten year structure five year Life-05 first business trip
- 数据库安全性和数据的完整性
- Learn and understand Architecture Design from business development
- 软考 系统架构设计师 简明教程 | 需求工程
- SSH超市进销存管理系统
- How to build and use redis fragment cluster
- C language flexible array
猜你喜欢
![[learning notes] node -- from 0 foundation to actual enterprise official website](/img/5e/c20e0aa0ad3f71fa5064ec9aca4ce7.jpg)
[learning notes] node -- from 0 foundation to actual enterprise official website

技术分享 | 大事务阻塞 show master status

在线问题反馈模块实战(十一):实现图片下载功能

Seven sorts -- detailed explanation of ten thousand words

数学向量基本知识

Anaconda 换源以及安装opencv

Use modern development methods and thinking to get rid of the "stumbling block" of legacy systems

【循环语句】
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VII)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VII)

拓扑排序 & 关键路径
随机推荐
目标检测xml文件实现mixup数据增强(修改文件路径直接能用,非常方便)
leetcode-99.恢复二叉搜索树
Arcgis 计算两个栅格图层相关性
QT error: error c2039 "value": not a member of "`global namespace"
【Azure 事件中心】Azure Event Hub 新功能尝试 -- 异地灾难恢复 (Geo-Disaster Recovery)
华泰证劵开户有骗局吗安全吗
Moment get week, month, quarter, year
[node middle layer practice (IV)] ---- logical processing of express middle layer
十年磨一剑,云原生分布式数据库PolarDB-X的核心技术演化
博世BOSCH EDI项目案例
What level is GF futures? Is the account opening safe and reliable?
Android development learning diary - content provider (cross application database modification)
Use modern development methods and thinking to get rid of the "stumbling block" of legacy systems
Time series dataset: power transformer dataset (etdataset)
switch语句的工作原理
拓扑排序 & 关键路径
[node middle tier practice (III)] --- template engine Pug
ssm框架外卖订餐系统
SSH超市进销存管理系统
智能建造试点城市通知的三大目标和八项任务