当前位置:网站首页>Jetpack compose is much more than a UI framework~
Jetpack compose is much more than a UI framework~
2022-07-07 06:51:00 【Cattle in the yard】

Jetpack Compose Is used to build native Android UI A modern toolkit . Jetpack Compose Use less code , Powerful tools and intuitive Kotlin API, Simplifies and accelerates Android Upper UI Development . This is a Android Developers The official website describes it .
This article is not to teach you Jetpack Compose Some basic ways to use , But why we need Jetpack Compose Some conciseness of , Let's be right Jetpack Compose Have a deeper understanding of .
1. Why do we need a new UI Tools ?
stay Android in ,UI The history of the toolkit can be traced back to at least 10 Years ago . Since then , Things have changed a lot , For example, the equipment we use , User expectations , And developers' expectations of the development tools and languages they use .
The above is just that we need new UI One reason for the tool , Another important reason is View.java This class is too big , There's too much code , It's so big that you can't even Githubs View the file on , Because it actually contains 30000 Line code , It's crazy , And almost every one we use Android UI Components need to inherit from View.
GogleAndroid Team Anna-Chiara Express , They are interested in what has been achieved API I'm sorry , Because they can't recover without destroying the function 、 Fix or extend these API, So now is a good time to make a new start .
That's why Jetpack Compose Let's see the dawn .
2. Jetpack Compose Focus on
Including the following aspects :
- Accelerate development
- Powerful UI Tools
- Intuitive Kotlin API
2.1 Accelerate development
If you are a junior development engineer , You always want to have more time to write business logic , Instead of spending time on things like : Animation 、 Color changes and other things , Look at this View:
This Material Edit-text It seems very simple , But in fact, there are many things to pay attention to , such as : Animation 、 color changing 、 State management and so on .
and Jetpack Compose It provides us with a lot of out of the box Material Components , If APP It is used. material Design words , So use Jetpack Compose It can save you a lot of energy .
2.2 Powerful UI Tools
Without the right tools UI Tool kits are useless , So from the past 10 You can learn a lot from years of experience ,Jetpack Compose The team began to work with JetBrains cooperation , To provide developers with a powerful toolkit , stay Android Studio Large scale support on Compose Ability .
Take a look at it Android Studio Performance on :
The picture above shows the use of Jetpack Compose Development UI when , stay Android Studio Preview on , You can see , When coding on the left , On the right you can show UI Instant preview , For example, in Ming Dynasty / State switching in dark mode , Can be displayed on the right in time .
It's similar to what we use now Android Studio Medium text/Design be similar , But it's more advanced , It's easy to use , This function can only be used in Android Studio4.0 Preview version above , Development compose When using .
2.3 Intuitive Kotlin API
For developers ,Jetpack Compose Its purpose is not just Android UI, So with Kotlin To write them and open source . Of course , all Android The code is open source , But here's the thing Compose Code . therefore , You can view and use the code , You can also provide feedback here .
because Compose Still under development , So feedback from every developer is important .
3. API Design
For more than 10 years ,Android The team is creating API And review API I have a lot of experience in , But there is a harvest - They use Java As a programming language . But there's a problem - They use Java As a development language .
Jetpack Compose It's the first one to use Kotlin Large projects under development , therefore Android The team is exploring Kotlin API The new world of the guide , To create a group specific to Compose API Guide to , The work is still in progress , There's still a long way to go .
4. Compose API Principles
4.1 Everything is a function
As I said in my previous article ,Compose It's a statement UI System , among , We use a set of functions to declare UI, And one Compose Functions can be nested with another Compose function , And use the tree structure to construct the required UI.
stay Compose in , We call the tree UI chart , When UI This will be refreshed when changes are needed UI chart , such as Compose In function if sentence , that Kotlin The compiler needs to pay attention to .
4.2 Top level function (Top-level function)
stay Compose In the world of , There is no concept of a class , It's all functions , And they are all top-level functions , Therefore, there will be no inheritance and hierarchy problems .
@Composable
fun checkbox ( ... )
@Composable
fun TextView ( ... )
@Composable
fun Edittext ( ... )
@Composable
fun Image ( ... )
In the process ,Compose The function always generates the same... Based on the input received UI, therefore , There's no harm in giving up class structures . Build from class structure UI Transition to top-level function construction UI For developers and Android The team is a huge change , Top level functions are still under discussion , Not yet released release edition .
4.3 Combination over inheritance
Jetpack Compose Prefer composition to inheritance . Compose Will build on other parts UI, But will not inherit behavior .
If you always pay attention to Android Or right Android Know something about , You will know ,Android Almost all components in are inherited from View class ( Inherit directly or indirectly ). such as EidtText Inherited from TextView, At the same time TextView Inherited from others View, Such an inheritance organization will eventually point to View namely View.java. also View.java And a lot of functions .
and Compose The team transferred the whole system from inheritance to the top-level function .Textview,EditText, Check box And all UI Components are their own Compose function , And they constitute what you want to create UI Other functions of , Instead of inheriting from another class .
4.4. Trust a single source
Trust a single source Is to build the whole Jetpack Compose A very important feature . If you are used to the existing UI tool kit , Then you may know how clicking works . As shown in the following code :
@Override
public boolean performClick(){
setChecked(!mChecked);
final boolean handled = super.performClick();
...
}
First , It changes view The state of , And then perform the action , This can lead to many bug, For example, check box , Because it starts with Selected status Turn into Not selected , vice versa , Then for some reason , If the operation fails , The developer must manually assign the previous status .
And in the Compose What about China? , The function is just the opposite . Here it is , Functions such as check boxes have two parameters . One is in UI Display status in , The other is lambda function , Used to observe UI State changes that should be changed accordingly .
@Composable
fun Checkbox(checked : Boolean,
onCheckedChange : ((Boolean) -> Unit)),
....)
5. Deepen understanding Compose

As shown in the figure above ,Compose It is divided into four parts at runtime . Let's take a look at each one .
5.1 Core
seeing the name of a thing one thinks of its function , This is a Compose At the heart of , If you don't want to learn more , You can skip it .
Basically , The core consists of four building blocks :
- draw (Draw)
- Layout (Layout)
- Input (Input)
- semantics (Semantics)

1、Draw — Draw Gave you a visit Canvas The ability of , So you can draw any custom you want View
2、Layout — By layout , We can measure things and place views accordingly .
3、Input — Developers can access events and execute gestures by entering
4、Semantics — We can provide semantic information about the tree .
5.2. Foundation
Foundation The core of is to collect all the contents mentioned above , And jointly create a Abstraction layer , To make it easier for developers to invoke .
5.3 Material
In this layer , be-all Material Components will be provided , And we can build complex by providing these components UI.
This is a Compose The best part of the excellent work done by the team , ad locum , All that's available View There are Material Support , therefore , Use Compose To build APP, The default is Material Style , This makes developers a lot less work .
6. slot API
slot API The emergence of is to leave a lot of space for developers , So that they can perform any custom actions they need ,Android The team tried to guess at many of the customizations that developers might think of , But they can't always imagine what developers think , For example, use a belt drawable Of TextView.
therefore ,Compose The team made room for the components , So that developers can do whatever they need , for example Use the buttons . You can Keep the text or Text with icon or Anything you need , As shown below 
Last
Xiaobian collected some on the Internet Android Develop relevant learning documents 、 Interview questions 、Android Core notes and other documents , I hope it can help you learn and improve , If you want to refer to Xiaobian PDF Learning documents can be done if necessary Reply to me by private letter 666 Immediate delivery !!! There are many records in it Android Relevant learning knowledge points .


边栏推荐
- 【mysqld】Can't create/write to file
- 品牌·咨询标准化
- JWT certification
- C language (structure) defines a user structure with the following fields:
- 服装门店如何盈利?
- MySql用户权限
- Etcd database source code analysis -- starting from the start function of raftnode
- jdbc数据库连接池使用问题
- linux系统rpm方式安装的mysql启动失败
- POI export to excel: set font, color, row height adaptation, column width adaptation, lock cells, merge cells
猜你喜欢

Config分布式配置中心

途家、木鸟、美团……民宿暑期战事将起

Etcd database source code analysis -- starting from the start function of raftnode

大促过后,销量与流量兼具,是否真的高枕无忧?

MySQL installation

循环肿瘤细胞——Abnova 解决方案来啦

BindingException 异常(报错)处理

MATLAB小技巧(29)多项式拟合 plotfit

Maze games based on JS

Learning notes | data Xiaobai uses dataease to make a large data screen
随机推荐
MySQL卸载文档-Windows版
企業如何進行數據治理?分享數據治理4個方面的經驗總結
Force deduction 62 different paths (the number of all paths from the upper left to the lower right of the matrix) (dynamic planning)
从零到一,教你搭建「CLIP 以文搜图」搜索服务(二):5 分钟实现原型
Unity C# 函数笔记
Basic DOS commands
docker-compose启动redis集群
常用函数detect_image/predict
MYSQL----导入导出&视图&索引&执行计划
Google Chrome browser released patch 103.0.5060.114 to fix the 0-day vulnerability
dolphinscheduler3. X local startup
C interview 24 (pointer) define a double array with 20 elements a
from . onnxruntime_ pybind11_ State Import * noqa ddddocr operation error
网络基础 —— 报头、封装和解包
工具类:对象转map 驼峰转下划线 下划线转驼峰
毕业设计游戏商城
Programmers' daily | daily anecdotes
多学科融合
leetcode 509. Fibonacci Number(斐波那契数字)
The difference between string constants and string objects when allocating memory