SlideShare a Scribd company logo
1 of 62
GDG Taichung
UI新時代
Jetpack Compose
En-Ping Hsieh
2019 | Confidential and Proprietary GDG Taichung
Agenda
什麼是 Compose
Compose 想要解決什麼問題?
Jetpack Compose & Android Studio 4.2
示範
注意事項
參考資料
GDG Taichung
什麼是Compose
GDG Taichung
用Kotlin寫UI
GDG Taichung
GDG Taichung
只需要給 function 加上 @Composable,
就可以使用各式各樣的Composable元件,如Text、
Divider、Image、Card、Column、Row...
GDG Taichung
沒有淚類別,沒有傷害
GDG Taichung
一個稍微複雜的Layout長怎樣呢?
GDG Taichung
CardView
Linear Layout
ImageView
Text View
GDG Taichung
如果需求是:
如果有副標題就顯示,沒有就不顯示。
GDG Taichung
GDG Taichung
你可能會疑問
難道不需要初始化TextView的Visibility屬性嗎?
GDG Taichung
Composable function (UI) 不存在狀態,
而是作為資料的一種變換。
𝒇( )→Data UI
GDG Taichung
想像一個沒有狀態的Checkbox
GDG Taichung
GDG Taichung
但完全沒有狀態,也很不方便。
GDG Taichung
GDG Taichung
Compose想要解決什麼問題?
GDG Taichung
UI Toolkit 應該提供什麼能力?
GDG Taichung
根據給定的資料定義畫面該呈現什麼
畫面該如何反應事件改變
根據當前畫面狀態去調整?
GDG Taichung
根據給定的資料定義畫面該呈現什麼
畫面該如何反應事件改變
根據當前畫面狀態去調整?
GDG Taichung
Compose是一套Declarative UI toolkit
GDG Taichung
Declarative vs. Imperative
GDG Taichung
告訴Compose希望怎樣的畫面(What)
但交給Compose去決定如何達成(How)
GDG Taichung
為何Composable元件沒有狀態?
GDG Taichung
如無必要,勿增實體
Entities should not be multiplied unnecessarily
Ockham's Razor
GDG Taichung
Single Source of Truth (SSOT)
GDG Taichung
當資料來源發生變動時,
調用所有需要更新的Composable來重新建構UI,
此過程稱作Recompose 。
GDG Taichung
資料更新
資料 事件
點擊
GDG Taichung
資料總是由上而下傳遞,事件總是由下向上委派
(Lambda)
GDG Taichung
Compose是一套用Kotlin Code開發UI的工具
GDG Taichung
Jetpack Compose & Android
Studio
० 屬於Jetpack的一部分,免除UI元件Patch太慢的問題
० 元件不再具備狀態,可達成Single Source of Truth
० 全部Kotlin,不再使用findViewById,也不需要為Custom widget 設置style.xml
、color.xml和layout.xml,增加元件的內聚性,提高重構的可能性。
० 支援Material Design
० 可即時預覽多種螢幕畫面,還可進行互動(Android Studio 4.2 Canary)
GDG Taichung
多種畫面預覽
通過@Preview 標籤即可不同螢幕尺寸、文字尺寸、深色模式即時預覽。
GDG Taichung
測試畫面互動
畫面不僅可以預覽,還可以進行一定程度的互動,你可以測試你的動畫效果。
GDG Taichung
用Compose建構畫面
GDG Taichung
GDG Taichung
GDG Taichung
新Constraint Layout
GDG Taichung
GDG Taichung
GDG Taichung
重構一下
GDG Taichung
GDG Taichung
加入style和padding
GDG Taichung
GDG Taichung
Animation
GDG Taichung
GDG Taichung
GDG Taichung
GDG Taichung
新RecyclerView
AdapterList
GDG Taichung
GDG Taichung
GDG Taichung
互動一下
GDG Taichung
GDG Taichung
測試呢?
GDG Taichung
GDG Taichung
GDG Taichung
之前辛辛苦苦做好的UI都要重來嗎?
GDG Taichung
GDG Taichung
我想踹踹Compose,要注意什麼?
GDG Taichung
注意事項
० 屬於開發階段(0.1.0-dev13),請勿使用於正式產品
仍然有許多bug,像是preview經常無法正確生成,需要refresh,或是互動點擊
區域跑版。
० Android Studio 4.2+(Canary)
० Android SDK 21+
० Testing only for AndroidTest
https://mvnrepository.com/artifact/androidx.ui/ui-test
GDG Taichung
參考資料
० Code Lab
https://codelabs.developers.google.com/codelabs/jetpack-compose-basics/#0
० Tutorial
https://developer.android.com/jetpack/compose/tutorial
० Sample Repo
JetNews
https://github.com/android/compose-samples/tree/master/JetNews
० Speech
Jetpack Compose
https://www.youtube.com/watch?v=U5BwfqBpiWU
Declarative UI patterns (Google I/O'19)
https://www.youtube.com/watch?v=VsStyq4Lzxo
What's New in Jetpack Compose (Android Dev Summit '19)
https://www.youtube.com/watch?v=dtm2h-_sNDQ
Understanding Compose (Android Dev Summit '19)
https://www.youtube.com/watch?v=Q9MtlmmN4Q0
GDG Taichung
FAQ

More Related Content

What's hot

Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction Sarmad Ali
 
Embedded system custom single purpose processors
Embedded system custom single  purpose processorsEmbedded system custom single  purpose processors
Embedded system custom single purpose processorsAiswaryadevi Jaganmohan
 
Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming languageVasavi College of Engg
 
golden rules of user interface design
golden rules of user interface designgolden rules of user interface design
golden rules of user interface designgadige harshini
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design patternMindfire Solutions
 
Software project estimation
Software project estimationSoftware project estimation
Software project estimationinayat khan
 
Human Computer Interaction Chapter 2
Human Computer Interaction Chapter 2Human Computer Interaction Chapter 2
Human Computer Interaction Chapter 2syxf10
 
Real Time Systems & RTOS
Real Time Systems & RTOSReal Time Systems & RTOS
Real Time Systems & RTOSVishwa Mohan
 
Embedded System Tools ppt
Embedded System Tools  pptEmbedded System Tools  ppt
Embedded System Tools pptHalai Hansika
 
Structural Design pattern - Adapter
Structural Design pattern - AdapterStructural Design pattern - Adapter
Structural Design pattern - AdapterManoj Kumar
 
Displaying information within a window.68
Displaying information within a window.68Displaying information within a window.68
Displaying information within a window.68myrajendra
 
Unit 1-introduction to scripts
Unit 1-introduction to scriptsUnit 1-introduction to scripts
Unit 1-introduction to scriptssana mateen
 
The Golden Rules by Theo Mandel - Software Engineering
The Golden Rules by Theo Mandel - Software EngineeringThe Golden Rules by Theo Mandel - Software Engineering
The Golden Rules by Theo Mandel - Software EngineeringAmit Baghel
 
Concept of Failure, error, fault and defect
Concept of Failure, error, fault and defectConcept of Failure, error, fault and defect
Concept of Failure, error, fault and defectchaklee191
 
Introduction to java
Introduction to  javaIntroduction to  java
Introduction to javaKalai Selvi
 
Vertical vs Horizontal Scaling
Vertical vs Horizontal Scaling Vertical vs Horizontal Scaling
Vertical vs Horizontal Scaling Mark Myers
 
Software project management- Software Engineering
Software project management- Software EngineeringSoftware project management- Software Engineering
Software project management- Software EngineeringMuhammad Yousuf Abdul Qadir
 
Object oriented testing
Object oriented testingObject oriented testing
Object oriented testingHaris Jamil
 

What's hot (20)

Software Sizing
Software SizingSoftware Sizing
Software Sizing
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 
Embedded system custom single purpose processors
Embedded system custom single  purpose processorsEmbedded system custom single  purpose processors
Embedded system custom single purpose processors
 
Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming language
 
golden rules of user interface design
golden rules of user interface designgolden rules of user interface design
golden rules of user interface design
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
Software project estimation
Software project estimationSoftware project estimation
Software project estimation
 
Human Computer Interaction Chapter 2
Human Computer Interaction Chapter 2Human Computer Interaction Chapter 2
Human Computer Interaction Chapter 2
 
Real Time Systems & RTOS
Real Time Systems & RTOSReal Time Systems & RTOS
Real Time Systems & RTOS
 
Embedded System Tools ppt
Embedded System Tools  pptEmbedded System Tools  ppt
Embedded System Tools ppt
 
Structural Design pattern - Adapter
Structural Design pattern - AdapterStructural Design pattern - Adapter
Structural Design pattern - Adapter
 
Displaying information within a window.68
Displaying information within a window.68Displaying information within a window.68
Displaying information within a window.68
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Unit 1-introduction to scripts
Unit 1-introduction to scriptsUnit 1-introduction to scripts
Unit 1-introduction to scripts
 
The Golden Rules by Theo Mandel - Software Engineering
The Golden Rules by Theo Mandel - Software EngineeringThe Golden Rules by Theo Mandel - Software Engineering
The Golden Rules by Theo Mandel - Software Engineering
 
Concept of Failure, error, fault and defect
Concept of Failure, error, fault and defectConcept of Failure, error, fault and defect
Concept of Failure, error, fault and defect
 
Introduction to java
Introduction to  javaIntroduction to  java
Introduction to java
 
Vertical vs Horizontal Scaling
Vertical vs Horizontal Scaling Vertical vs Horizontal Scaling
Vertical vs Horizontal Scaling
 
Software project management- Software Engineering
Software project management- Software EngineeringSoftware project management- Software Engineering
Software project management- Software Engineering
 
Object oriented testing
Object oriented testingObject oriented testing
Object oriented testing
 

Android Jetpack Compose 簡介