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

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 .

原网站

版权声明
本文为[Cattle in the yard]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070227495401.html