SlideShare a Scribd company logo
How to become the MacGyver of
android custom views
Angus MacGyver: The man that
can kill someone with a paperclip
@fernando_cejas!
http://www.android10.org/!
http://github.com/android10
Who I am…
Software engineer!
GDG Barcelona organizer
Android lover!
Geek
How android draws view hierarchy
Drawing begins with the root node of the
layout.!
!
The layout hierarchy is traversed in the order
of declaration.!
!
Parents are drawn before their children and
children are drawn in the order of
declaration.
Drawing the layout is a three pass process:!
Measure!
Layout!
Draw
In terms of methods in View class:!
requestLayout(): measure + layout!
invalidate(): draw
How android draws view hierarchy
View!
19.572 lines
ImageView!
1.260 lines
TextView!
9.220 lines
ViewGroup!
6.771 lines
Button!
121 lines
LinearLayout!
1.898 lines
RelativeLayout!
1.812 lines
…!
… lines
View framework
View.onAttachedToWindow()
View.onDetachedFromWindow()
View added
Animate View
View.onMeasure()
View.onLayout()
View.onDraw()
View removed
Rendering
loop
View lifecycle
Many reasons to go custom:!
!
Performance!
!
Flexibility!
!
Innovation!
!
Reusability
To remember: there is no custom view composition :(
Views: going custom…
Views: going custom…
Measuring!
!
Layouting!
!
Drawing!
!
Saving state!
!
Handling events
View responsibilities
Encapsulates the layout requirements passed
from parent to child.!
!
Represents a requirement for either the width or
the height. !
!
Is comprised of a size and a mode.
There are 3 possible modes:!
UNSPECIFIED !
EXACTLY !
AT_MOST
MeasureSpec
It tells Android how big you want your custom view
to be, taking into consideration the layout
constraints provided by the parent.
onMeasure()
Called from layout when the view should assign a
size and position to each of its children. Used
frequently when extending ViewGroup.
onLayout()
Called by Android when the view needs to draw itself.
Here is the place for the logic related with drawing the
different components or content of our view.
onDraw()
You need to request a new layout if a property
changes that might affect the size or shape of the
view.
You have to invalidate the view after any change to
its properties that might change its appearance.
requestLayout()
invalidate()
There are 3 different ways:!
!
Compound Views!
!
Custom Compound Views!
!
Flat Custom Views
Implementing custom views
These are usually our starting point.!
!
Perform pretty well in many situations.!
!
Simple to implement.
1. Subclass one of the built-in layouts.!
2. Inflate a merge layout in the constructor.!
3. Initialize members to point to inner views with findViewById().!
4.Add your own APIs to query and update the view state.
Compound views
Compound views
Compound views
Is a compound view which overrides onMeasure(),
onLayout() and extends ViewGroup.
Custom compound views
It is a fully custom view that measures, arranges,
and draws all its elements. Extends from View.
https://github.com/android10/Android-DonutViews
Flat custom views
To define additional attributes it is a must to create
an attrs.xml file in your res/values folder and
define them like the following example:
Declaring custom attributes
To use custom attributes already defined, in your
layout file you have to declare them in the XML
header as following:
Using custom attributes
Reading custom attributes in code
We implement onTouchEvent() to handle touch
screen motion events.
Making our view to react…
For persisting view state you want to have a look
at View.BasedSavedState class.
The canvas API allows to create complex
graphical effects: Canvas, Paint and Shader
classes will help with this.
Saving view state
Custom and advance drawing
The View class supports the creation of an
image of its current display.
Creating screenshots of views
Enabling “show layout bounds” options on the
developer options screen.
Real world examples
Avoid custom views if they are not extremely
necessary.!
Creating objects ahead of time is an important
optimisation. !
Initialise your stuff in OnAttachToWindow() method
when possible.!
If you do not need complex measurement just use
onSizeChanged() method.!
If you define own views, ensure you review the
ViewConfiguration class.!
When using custom attributes always recycle your
TypedArray.
Tips and tricks
The more custom, the less features for free.!
!
Avoid premature optimisation.!
!
Go custom only on core components.!
!
Start with stock widgets and compound views.!
!
Do not reinvent the wheel.
Wrapping up
Questions?
Thanks!!!
@fernando_cejas!
http://www.android10.org/!
http://github.com/android10
Java Developers never RIP,
they just get GARBAGE
COLLECTED…

More Related Content

Similar to How to Become the MacGyver of Android Custom Views

04 user interfaces
04 user interfaces04 user interfaces
04 user interfaces
C.o. Nieto
 
Android Tutorial
Android TutorialAndroid Tutorial
Android Tutorial
Fun2Do Labs
 
Mobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdfMobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdf
AbdullahMunir32
 
Android Application Development - Level 1
Android Application Development - Level 1Android Application Development - Level 1
Android Application Development - Level 1
Isham Rashik
 
Android Custom views
Android Custom views   Android Custom views
Android Custom views
Matej Vukosav
 
07_UIAndroid.pdf
07_UIAndroid.pdf07_UIAndroid.pdf
07_UIAndroid.pdf
ImranS18
 
View groups containers
View groups containersView groups containers
View groups containers
Mani Selvaraj
 
Compose In Practice
Compose In PracticeCompose In Practice
Compose In Practice
Kelvin Harron
 
Top Tips for Android UIs - Getting the Magic on Tablets
Top Tips for Android UIs - Getting the Magic on TabletsTop Tips for Android UIs - Getting the Magic on Tablets
Top Tips for Android UIs - Getting the Magic on Tablets
Motorola Mobility - MOTODEV
 
Telerik Kendo UI Overview
Telerik Kendo UI OverviewTelerik Kendo UI Overview
Telerik Kendo UI Overview
Ed Musters
 
Step by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView TutorialsStep by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView Tutorials
Nilesh kumar Jadav
 
android layouts
android layoutsandroid layouts
android layouts
Deepa Rani
 
Android app material design from dev's perspective
Android app material design from dev's perspectiveAndroid app material design from dev's perspective
Android app material design from dev's perspective
DeSmart Agile Software House
 
WMP_MP02_revd(10092023).pptx
WMP_MP02_revd(10092023).pptxWMP_MP02_revd(10092023).pptx
WMP_MP02_revd(10092023).pptx
fahmi324663
 
Android UI
Android UI Android UI
Android UI
mailalamin
 
Android Ui
Android UiAndroid Ui
Android Ui
Jetti Chowdary
 
iOS Development (Part 3) - Additional GUI Components
iOS Development (Part 3) - Additional GUI ComponentsiOS Development (Part 3) - Additional GUI Components
iOS Development (Part 3) - Additional GUI Components
Asim Rais Siddiqui
 
WMP_MP02_revd_03(10092023).pptx
WMP_MP02_revd_03(10092023).pptxWMP_MP02_revd_03(10092023).pptx
WMP_MP02_revd_03(10092023).pptx
fahmi324663
 
Yeoman AngularJS and D3 - A solid stack for web apps
Yeoman AngularJS and D3 - A solid stack for web appsYeoman AngularJS and D3 - A solid stack for web apps
Yeoman AngularJS and D3 - A solid stack for web apps
climboid
 
Different types of sticker apps
Different types of sticker appsDifferent types of sticker apps
Different types of sticker apps
Jelena Krmar
 

Similar to How to Become the MacGyver of Android Custom Views (20)

04 user interfaces
04 user interfaces04 user interfaces
04 user interfaces
 
Android Tutorial
Android TutorialAndroid Tutorial
Android Tutorial
 
Mobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdfMobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdf
 
Android Application Development - Level 1
Android Application Development - Level 1Android Application Development - Level 1
Android Application Development - Level 1
 
Android Custom views
Android Custom views   Android Custom views
Android Custom views
 
07_UIAndroid.pdf
07_UIAndroid.pdf07_UIAndroid.pdf
07_UIAndroid.pdf
 
View groups containers
View groups containersView groups containers
View groups containers
 
Compose In Practice
Compose In PracticeCompose In Practice
Compose In Practice
 
Top Tips for Android UIs - Getting the Magic on Tablets
Top Tips for Android UIs - Getting the Magic on TabletsTop Tips for Android UIs - Getting the Magic on Tablets
Top Tips for Android UIs - Getting the Magic on Tablets
 
Telerik Kendo UI Overview
Telerik Kendo UI OverviewTelerik Kendo UI Overview
Telerik Kendo UI Overview
 
Step by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView TutorialsStep by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView Tutorials
 
android layouts
android layoutsandroid layouts
android layouts
 
Android app material design from dev's perspective
Android app material design from dev's perspectiveAndroid app material design from dev's perspective
Android app material design from dev's perspective
 
WMP_MP02_revd(10092023).pptx
WMP_MP02_revd(10092023).pptxWMP_MP02_revd(10092023).pptx
WMP_MP02_revd(10092023).pptx
 
Android UI
Android UI Android UI
Android UI
 
Android Ui
Android UiAndroid Ui
Android Ui
 
iOS Development (Part 3) - Additional GUI Components
iOS Development (Part 3) - Additional GUI ComponentsiOS Development (Part 3) - Additional GUI Components
iOS Development (Part 3) - Additional GUI Components
 
WMP_MP02_revd_03(10092023).pptx
WMP_MP02_revd_03(10092023).pptxWMP_MP02_revd_03(10092023).pptx
WMP_MP02_revd_03(10092023).pptx
 
Yeoman AngularJS and D3 - A solid stack for web apps
Yeoman AngularJS and D3 - A solid stack for web appsYeoman AngularJS and D3 - A solid stack for web apps
Yeoman AngularJS and D3 - A solid stack for web apps
 
Different types of sticker apps
Different types of sticker appsDifferent types of sticker apps
Different types of sticker apps
 

More from Fernando Cejas

The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on AndroidThe Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
It is about philosophy: culture of a good programmer
It is about philosophy: culture of a good programmerIt is about philosophy: culture of a good programmer
It is about philosophy: culture of a good programmer
Fernando Cejas
 
Material Design for Old Schoolers
Material Design for Old SchoolersMaterial Design for Old Schoolers
Material Design for Old Schoolers
Fernando Cejas
 
Android UX-UI Design for Fun and Profit
Android UX-UI Design for Fun and ProfitAndroid UX-UI Design for Fun and Profit
Android UX-UI Design for Fun and Profit
Fernando Cejas
 
How ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second EditionHow ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second Edition
Fernando Cejas
 
How ANDROID TESTING changed how we think about Death
How ANDROID TESTING changed how we think about DeathHow ANDROID TESTING changed how we think about Death
How ANDROID TESTING changed how we think about Death
Fernando Cejas
 
Dinosaurs and Androids: The Listview Evolution
Dinosaurs and Androids: The Listview EvolutionDinosaurs and Androids: The Listview Evolution
Dinosaurs and Androids: The Listview Evolution
Fernando Cejas
 
Inside Android Testing
Inside Android TestingInside Android Testing
Inside Android Testing
Fernando Cejas
 
Webview: The fifth element
Webview: The fifth elementWebview: The fifth element
Webview: The fifth element
Fernando Cejas
 
Nfc on Android
Nfc on AndroidNfc on Android
Nfc on Android
Fernando Cejas
 
Android Design Patterns
Android Design PatternsAndroid Design Patterns
Android Design Patterns
Fernando Cejas
 
Android Cloud To Device Messaging
Android Cloud To Device MessagingAndroid Cloud To Device Messaging
Android Cloud To Device Messaging
Fernando Cejas
 
Android Quick Introduction
Android Quick IntroductionAndroid Quick Introduction
Android Quick Introduction
Fernando Cejas
 
Desarrollo android almacenamiento de datos
Desarrollo android    almacenamiento de datosDesarrollo android    almacenamiento de datos
Desarrollo android almacenamiento de datos
Fernando Cejas
 
Android simple 2d Layout animation
Android simple 2d Layout animationAndroid simple 2d Layout animation
Android simple 2d Layout animation
Fernando Cejas
 

More from Fernando Cejas (15)

The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on AndroidThe Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
 
It is about philosophy: culture of a good programmer
It is about philosophy: culture of a good programmerIt is about philosophy: culture of a good programmer
It is about philosophy: culture of a good programmer
 
Material Design for Old Schoolers
Material Design for Old SchoolersMaterial Design for Old Schoolers
Material Design for Old Schoolers
 
Android UX-UI Design for Fun and Profit
Android UX-UI Design for Fun and ProfitAndroid UX-UI Design for Fun and Profit
Android UX-UI Design for Fun and Profit
 
How ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second EditionHow ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second Edition
 
How ANDROID TESTING changed how we think about Death
How ANDROID TESTING changed how we think about DeathHow ANDROID TESTING changed how we think about Death
How ANDROID TESTING changed how we think about Death
 
Dinosaurs and Androids: The Listview Evolution
Dinosaurs and Androids: The Listview EvolutionDinosaurs and Androids: The Listview Evolution
Dinosaurs and Androids: The Listview Evolution
 
Inside Android Testing
Inside Android TestingInside Android Testing
Inside Android Testing
 
Webview: The fifth element
Webview: The fifth elementWebview: The fifth element
Webview: The fifth element
 
Nfc on Android
Nfc on AndroidNfc on Android
Nfc on Android
 
Android Design Patterns
Android Design PatternsAndroid Design Patterns
Android Design Patterns
 
Android Cloud To Device Messaging
Android Cloud To Device MessagingAndroid Cloud To Device Messaging
Android Cloud To Device Messaging
 
Android Quick Introduction
Android Quick IntroductionAndroid Quick Introduction
Android Quick Introduction
 
Desarrollo android almacenamiento de datos
Desarrollo android    almacenamiento de datosDesarrollo android    almacenamiento de datos
Desarrollo android almacenamiento de datos
 
Android simple 2d Layout animation
Android simple 2d Layout animationAndroid simple 2d Layout animation
Android simple 2d Layout animation
 

Recently uploaded

AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
Paris Salesforce Developer Group
 
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
sydezfe
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
uqyfuc
 
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
OKORIE1
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
mahaffeycheryld
 
Open Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surfaceOpen Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surface
Indrajeet sahu
 
Zener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and ApplicationsZener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and Applications
Shiny Christobel
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
Kamal Acharya
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
nedcocy
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
um7474492
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
Assistant Engineer (Chemical) Interview Questions.pdf
Assistant Engineer (Chemical) Interview Questions.pdfAssistant Engineer (Chemical) Interview Questions.pdf
Assistant Engineer (Chemical) Interview Questions.pdf
Seetal Daas
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
harshapolam10
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
Levelised Cost of Hydrogen (LCOH) Calculator Manual
Levelised Cost of Hydrogen  (LCOH) Calculator ManualLevelised Cost of Hydrogen  (LCOH) Calculator Manual
Levelised Cost of Hydrogen (LCOH) Calculator Manual
Massimo Talia
 
OOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming languageOOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming language
PreethaV16
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 

Recently uploaded (20)

AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
 
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
 
Open Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surfaceOpen Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surface
 
Zener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and ApplicationsZener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and Applications
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
Assistant Engineer (Chemical) Interview Questions.pdf
Assistant Engineer (Chemical) Interview Questions.pdfAssistant Engineer (Chemical) Interview Questions.pdf
Assistant Engineer (Chemical) Interview Questions.pdf
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
Levelised Cost of Hydrogen (LCOH) Calculator Manual
Levelised Cost of Hydrogen  (LCOH) Calculator ManualLevelised Cost of Hydrogen  (LCOH) Calculator Manual
Levelised Cost of Hydrogen (LCOH) Calculator Manual
 
OOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming languageOOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming language
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 

How to Become the MacGyver of Android Custom Views

  • 1. How to become the MacGyver of android custom views Angus MacGyver: The man that can kill someone with a paperclip @fernando_cejas! http://www.android10.org/! http://github.com/android10
  • 2. Who I am… Software engineer! GDG Barcelona organizer Android lover! Geek
  • 3. How android draws view hierarchy Drawing begins with the root node of the layout.! ! The layout hierarchy is traversed in the order of declaration.! ! Parents are drawn before their children and children are drawn in the order of declaration.
  • 4. Drawing the layout is a three pass process:! Measure! Layout! Draw In terms of methods in View class:! requestLayout(): measure + layout! invalidate(): draw How android draws view hierarchy
  • 5. View! 19.572 lines ImageView! 1.260 lines TextView! 9.220 lines ViewGroup! 6.771 lines Button! 121 lines LinearLayout! 1.898 lines RelativeLayout! 1.812 lines …! … lines View framework
  • 7. Many reasons to go custom:! ! Performance! ! Flexibility! ! Innovation! ! Reusability To remember: there is no custom view composition :( Views: going custom…
  • 10. Encapsulates the layout requirements passed from parent to child.! ! Represents a requirement for either the width or the height. ! ! Is comprised of a size and a mode. There are 3 possible modes:! UNSPECIFIED ! EXACTLY ! AT_MOST MeasureSpec
  • 11. It tells Android how big you want your custom view to be, taking into consideration the layout constraints provided by the parent. onMeasure()
  • 12. Called from layout when the view should assign a size and position to each of its children. Used frequently when extending ViewGroup. onLayout()
  • 13. Called by Android when the view needs to draw itself. Here is the place for the logic related with drawing the different components or content of our view. onDraw()
  • 14. You need to request a new layout if a property changes that might affect the size or shape of the view. You have to invalidate the view after any change to its properties that might change its appearance. requestLayout() invalidate()
  • 15. There are 3 different ways:! ! Compound Views! ! Custom Compound Views! ! Flat Custom Views Implementing custom views
  • 16. These are usually our starting point.! ! Perform pretty well in many situations.! ! Simple to implement. 1. Subclass one of the built-in layouts.! 2. Inflate a merge layout in the constructor.! 3. Initialize members to point to inner views with findViewById().! 4.Add your own APIs to query and update the view state. Compound views
  • 19. Is a compound view which overrides onMeasure(), onLayout() and extends ViewGroup. Custom compound views
  • 20. It is a fully custom view that measures, arranges, and draws all its elements. Extends from View. https://github.com/android10/Android-DonutViews Flat custom views
  • 21. To define additional attributes it is a must to create an attrs.xml file in your res/values folder and define them like the following example: Declaring custom attributes
  • 22. To use custom attributes already defined, in your layout file you have to declare them in the XML header as following: Using custom attributes
  • 24. We implement onTouchEvent() to handle touch screen motion events. Making our view to react…
  • 25. For persisting view state you want to have a look at View.BasedSavedState class. The canvas API allows to create complex graphical effects: Canvas, Paint and Shader classes will help with this. Saving view state Custom and advance drawing
  • 26. The View class supports the creation of an image of its current display. Creating screenshots of views
  • 27. Enabling “show layout bounds” options on the developer options screen. Real world examples
  • 28. Avoid custom views if they are not extremely necessary.! Creating objects ahead of time is an important optimisation. ! Initialise your stuff in OnAttachToWindow() method when possible.! If you do not need complex measurement just use onSizeChanged() method.! If you define own views, ensure you review the ViewConfiguration class.! When using custom attributes always recycle your TypedArray. Tips and tricks
  • 29. The more custom, the less features for free.! ! Avoid premature optimisation.! ! Go custom only on core components.! ! Start with stock widgets and compound views.! ! Do not reinvent the wheel. Wrapping up