当前位置:网站首页>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 .
边栏推荐
- Stack and queue-p78-8 [2011 unified examination true question]
- Bus消息总线
- Can't you really do it when you are 35 years old?
- 2022年全国所有A级景区数据(13604条)
- MOS tube parameters μ A method of Cox
- MySql用户权限
- Programmers' daily | daily anecdotes
- 华为机试题素数伴侣
- Under what circumstances should we consider sub database and sub table
- Which foreign language periodicals are famous in geology?
猜你喜欢
2022 Android interview essential knowledge points, a comprehensive summary
数据资产管理与数据安全国内外最新趋势
如何给目标机器人建模并仿真【数学/控制意义】
MySQL SQL的完整处理流程
精准时空行程流调系统—基于UWB超高精度定位系统
SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
FPGA课程:JESD204B的应用场景(干货分享)
Leite smart home longhaiqi: from professional dimming to full house intelligence, 20 years of focus on professional achievements
Apache ab 压力测试
随机推荐
Abnova循环肿瘤DNA丨全血分离,基因组DNA萃取分析
Leite smart home longhaiqi: from professional dimming to full house intelligence, 20 years of focus on professional achievements
【NOI模拟赛】区域划分(结论,构造)
The difference between string constants and string objects when allocating memory
Abnova 免疫组化服务解决方案
健身房如何提高竞争力?
化工园区危化品企业安全风险智能化管控平台建设四大目标
dolphinscheduler3. X local startup
DB2获取表信息异常:Caused by: com.ibm.db2.jcc.am.SqlException: [jcc][t4][1065][12306][4.25.13]
C language interview to write a function to find the first public string in two strings
请问 flinksql对接cdc时 如何实现计算某个字段update前后的差异 ?
当前发布的SKU(销售规格)信息中包含疑似与宝贝无关的字
Jetpack Compose 远不止是一个UI框架这么简单~
多学科融合
MySQL卸载文档-Windows版
JWT certification
[start from scratch] detailed process of deploying yolov5 in win10 system (CPU, no GPU)
MYSQL binlog相关命令
使用net core优势/为什么使用
Postgresql源码(60)事务系统总结