当前位置:网站首页>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 .


边栏推荐
- Basic introduction of JWT
- 请问如何查一篇外文文献的DOI号?
- How to find the literature of a foreign language journal?
- MySQL的安装
- This article introduces you to the characteristics, purposes and basic function examples of static routing
- Basic DOS commands
- MATLAB小技巧(30)非线性拟合 lsqcurefit
- MOS管参数μCox得到的一种方法
- Problems and precautions about using data pumps (expdp, impdp) to export and import large capacity tables in Oracle migration
- MySQL卸载文档-Windows版
猜你喜欢

Apache ab 压力测试

Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition

反射(二)

MySQL的主从复制原理

2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第一阶段答案
![Stack and queue-p79-10 [2014 unified examination real question]](/img/7f/e5bba2f64bb77e1781076361c32f01.jpg)
Stack and queue-p79-10 [2014 unified examination real question]

Go straight to the 2022ecdc fluorite cloud Developer Conference: work with thousands of industries to accelerate intelligent upgrading

FPGA课程:JESD204B的应用场景(干货分享)

2022 Android interview essential knowledge points, a comprehensive summary

What books can greatly improve programming ideas and abilities?
随机推荐
MYSQL binlog相关命令
Overview of FlexRay communication protocol
企業如何進行數據治理?分享數據治理4個方面的經驗總結
Linear algebra (1)
ESXI挂载移动(机械)硬盘详细教程
循环肿瘤细胞——Abnova 解决方案来啦
Redis (II) - redis General Command
from .onnxruntime_pybind11_state import * # noqa ddddocr运行报错
C language interview to write a function to find the first occurrence of substring m in string n.
ViewModelProvider.of 过时方法解决
oracle如何备份索引
SolidWorks的GB库(钢型材库,包括铝型材、铝管等结构)安装及使用教程(生成铝型材为例)
数据资产管理与数据安全国内外最新趋势
【luogu P1971】兔兔与蛋蛋游戏(二分图博弈)
Comment les entreprises gèrent - elles les données? Partager les leçons tirées des quatre aspects de la gouvernance des données
如何给目标机器人建模并仿真【数学/控制意义】
学术报告系列(六) - Autonomous Driving on the journey to full autonomy
C interview 24 (pointer) define a double array with 20 elements a
Prompt for channel security on the super-v / device defender side when installing vmmare
网络基础 —— 报头、封装和解包