-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
424 lines (377 loc) · 15.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Android Custom Controls and Canvas</title>
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="reveal.js/css/theme/black.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<link rel="stylesheet" href="reveal.js/lib/css/zenburn-nohighlight.css">
<!-- FontAwesome http://fontawesome.io/examples/ and http://fontawesome.io/icons/ -->
<link rel="stylesheet" href="font-awesome-4.7.0/css/font-awesome.min.css">
<style>
.reveal h1,
.reveal h2 {
text-transform: none !important;
font-variant-caps: small-caps !important;
}
.reveal .small-column-left {
float: left;
width: 30%;
}
.reveal .big-column-right {
float: left;
width: 58%;
padding-left: 5% !important;
}
.reveal .big-column-left {
float: left;
width: 68%;
}
.reveal .small-column-right {
float: left;
width: 20%;
padding-left: 5% !important;
text-align: left;
}
.reveal .left {
text-align: left;
width: 90%;
}
.reveal .join-with-previous {
margin-top: -0.8em;
}
</style>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<!-- Introduction -->
<section style="font-size: 0.8em">
<h1>Android Custom Controls and Canvas</h1>
<p>Xavier Rubio Jansana</p>
<p style="font-size: 0.8em">
<a href="https://twitter.com/teknik_tdr"><i class="fa fa-twitter" aria-hidden="true"></i> @teknik_tdr</a><br>
<a href="https://xrubio.com"><i class="fa fa-globe" aria-hidden="true"></i> https://xrubio.com</a><br>
<a href="https://github.com/xrubioj/"><i class="fa fa-github" aria-hidden="true"></i> https://github.com/xrubioj/</a>
</p>
</section>
<section>
<h1>What?</h1>
<ul>
<li class="fragment">Controls that don't exist in Android</li>
<li class="fragment">Compound Controls ("Group of <code>Views</code>")</li>
<li class="fragment">Customization of controls</li>
</ul>
</section>
<section>
<h1>When?</h1>
<ul>
<li class="fragment">No similar control exists</li>
<li class="fragment">Same groups of controls repeats</li>
<li class="fragment">Theming is not enough</li>
</ul>
</section>
<section>
<h1>Benefits</h1>
<ul>
<li class="fragment">Encapsulate and simplify common controls</li>
<li class="fragment">Create a design language</li>
<li class="fragment">Simplify maintenance</li>
</ul>
</section>
<!-- Compound controls -->
<section>
<h1 style="font-size: 2.3em">Compound Controls</h1>
<ol>
<li class="fragment">Extend a <code>ViewGroup</code> (e.g. <code>LinearLayout</code>, <code>ConstraintLayout</code>...)</li>
<li class="fragment">Inflate the layout and attach it</li>
<li class="fragment">...</li>
<li class="fragment">Profit!</li>
</ol>
</section>
<section>
<h1 style="font-size: 1.6em">Compound Controls - Class</h1>
<pre><code class="kotlin" data-trim data-noescape style="font-size: 0.92em">
class MyCompoundControlView @JvmOverloads constructor(
context: Context?,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
init {
View.inflate(context, R.layout.my_compount_control_layout,
this)
}
}
</code></pre>
</section>
<section>
<h1 style="font-size: 1.6em">Compound Controls - Layout</h1>
<pre><code class="xml" data-trim data-noescape style="font-size: 0.82em; max-height: 560px">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:text="Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/subtitle"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:text="Subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</code></pre>
</section>
<section>
<h1 style="font-size: 1.6em">Compound Controls - Result</h1>
<img style="border: 0" src="img/compount_control_bad_layout.png">
</section>
<section>
<h1 style="font-size: 1.6em">Compound Controls - Layout (better)</h1>
<pre><code class="xml" data-trim data-noescape style="font-size: 0.82em; max-height: 560px">
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tool="http://schemas.android.com/tools"
tool:parentTag="android.widget.LinearLayout"
tool:orientation="vertical"
tool:layout_width="wrap_content"
tool:layout_height="wrap_content">
<TextView .../>
<TextView .../>
</merge>
</code></pre>
<small class="fragment">Notice <code><merge></code> and <code>tool</code></small>
</section>
<section>
<h1 style="font-size: 1.6em">Compound Controls - Updated class</h1>
<pre><code class="kotlin" data-trim data-noescape style="font-size: 0.92em">
class MyCompoundControlView @JvmOverloads constructor(
context: Context?,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
init {
View.inflate(context, R.layout.my_compount_control_layout,
this)
this.orientation = VERTICAL
}
}
</code></pre>
<small class="fragment">Initialization of root tag moved here</small>
</section>
<section>
<h1 style="font-size: 1.6em">Compound Controls - Result</h1>
<img style="border: 0" src="img/compount_control_good_layout.png">
</section>
<section>
<h1 style="font-size: 1.6em">Compound Controls - Using</h1>
<pre><code class="xml" data-trim data-noescape style="font-size: 0.82em; max-height: 560px">
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.xrubio.customcontrols.MyCompoundControlView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</code></pre>
<small>Notice we're using the fully qualified class name</small>
</section>
<!-- Fully customized controls -->
<section>
<h1>UI Drawing Steps</h1>
<ol>
<li class="fragment" data-fragment-index="1">
Measure<span class="fragment" data-fragment-index="2">: calculate dimensions based in constraints</span>
<span class="fragment" data-fragment-index="7"><i class="fa fa-check" style="color: green"></i></span>
</li>
<li class="fragment" data-fragment-index="3">
Layout<span class="fragment" data-fragment-index="4">: layout children</span>
<span class="fragment" data-fragment-index="8">→ we don't need it</span>
</li>
<li class="fragment" data-fragment-index="5">
Draw<span class="fragment" data-fragment-index="6">: use <code>Canvas</code> to draw</span>
<span class="fragment" data-fragment-index="7"><i class="fa fa-check" style="color: green"></i></span>
</li>
</ol>
</section>
<section>
<h1>Measure</h1>
<pre><code class="kotlin" data-trim data-noescape>
val specMode: Int = MeasureSpec.getMode(measureSpec)
val specSize: Int = MeasureSpec.getSize(measureSpec)
</code></pre>
<small>We have a measureSpec per axis (X & Y)</small>
<p class="fragment" data-fragment-index="1">
<ul class="fragment" data-fragment-index="1">
<li>UNSPECIFIED</li>
<li>EXACTLY</li>
<li>AT_MOST</li>
</ul>
</section>
<section>
<h1>Measure</h1>
<pre><code class="kotlin" data-trim data-noescape>
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val minW: Int = (paddingStart + paddingEnd + _radius * 2.0f).toInt()
val w: Int = resolveSizeAndState(minW, widthMeasureSpec, 0)
val minH: Int = (paddingTop + paddingBottom + _radius * 2.0f).toInt()
val h: Int = resolveSizeAndState(minH, heightMeasureSpec, 0)
setMeasuredDimension(w, h)
}
</code></pre>
</section>
<section>
<h1>Measure</h1>
<ul>
<li class="fragment">Units are <b>pixels</b></li>
<li class="fragment"><b>Must</b> call <code>setMeasuredDimension()</code></li>
<li class="fragment">Use <code>resolveSizeAndState(size, measureSpec, childMeasuredState)</code> as helper</li>
</ul>
</section>
<section>
<h1>Canvas</h1>
<ul>
<li class="fragment">Units are <b>pixels</b></li>
<li class="fragment">Top-left is (0, 0)</li>
<li class="fragment">Draws in order ("on top of")</li>
</ul>
</section>
<section>
<h1>Canvas vs. Paint</h1>
<ul>
<li class="fragment">Canvas is where things are drawn</li>
<li class="fragment">Paint is how things are drawn</li>
<li class="fragment">Canvas can get a backing Bitmap</li>
</ul>
</section>
<section>
<h1>Canvas commands</h1>
<ul>
<li class="fragment">drawBitmap()</li>
<li class="fragment">drawRect(), drawCircle(), etc.</li>
<li class="fragment">drawColor()</li>
<li class="fragment">drawText()</li>
<li class="fragment">clip*()</li>
</ul>
</section>
<section>
<h1>Properties</h1>
<p>After changind a property, we need to either invalidate or relayout:</p>
<ul>
<li class="fragment"><code>invalidate()</code>: triggers a redraw. Use when property doesn't changes size.</li>
<li class="fragment"><code>requestLayout()</code>: triggers the whole cycle (measure, layout, draw). Use when property changes size.</li>
</ul>
</section>
<section>
<h1>Canvas and KTX</h1>
<pre class="fragment"><code class="gradle" data-trim data-noescape>
implementation 'androidx.core:core-ktx:1.1.0'
</code></pre>
<ul>
<li class="fragment">Helps cleanup code (avoiding save() and restore(), for instance)</li>
<li class="fragment"><code>withClip()</code>, <code>withMatrix()</code>, <code>withRotation</code>, etc.</li>
<li class="fragment">See: <small><a href="https://developer.android.com/reference/kotlin/androidx/core/graphics/package-summary">
https://developer.android.com/reference/kotlin/androidx/core/graphics/package-summary</a></small></li>
</ul>
</section>
<!-- References -->
<section>
<h1>References</h1>
<ul>
<li>
Custom View Components at developer.android.com
<a href="https://developer.android.com/guide/topics/ui/custom-components">https://developer.android.com/guide/topics/ui/custom-components</a>
</li>
<li>
"Tips for Building Custom Views on Android with Canvas APIs" slides, Rebecca Franks
<a href="https://speakerdeck.com/riggaroo/tips-for-building-custom-views-on-android-with-canvas-apis">
https://speakerdeck.com/riggaroo/tips-for-building-custom-views-on-android-with-canvas-apis</a>
</li>
<li>
Android Canvas APIs with Kotlin and KTX
<a href="https://riggaroo.co.za/android-canvas-apis-with-kotlin-and-ktx/">
https://riggaroo.co.za/android-canvas-apis-with-kotlin-and-ktx/</a>
</li>
</ul>
</section>
<section>
<h1>Other References</h1>
<ul style="font-size: 0.8em">
<li>
Source code of<br><code>View#resolveSizeAndState(int, int, int)</code> in AOSP
<a href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/View.java#24704">
https://android.googlesource.com/platform/frameworks/base/+/<br>refs/heads/master/core/java/android/view/View.java#24704
</a>
</li>
<li>
What's the utility of the third argument of View.resolveSizeAndState()?
<a href="https://stackoverflow.com/questions/13650903/whats-the-utility-of-the-third-argument-of-view-resolvesizeandstate/13651513#13651513">
https://stackoverflow.com/questions/13650903/<br>whats-the-utility-of-the-third-argument-of-view-resolvesizeandstate/<13651513#13651513
</a>
</li>
</ul>
</section>
<!-- End -->
<section>
<h1>Questions? 🤔</h1>
</section>
<section>
<h1>Thanks! 🎉</h1>
<p>Xavier Rubio Jansana</p>
<p style="font-size: 0.8em">
<a href="https://twitter.com/teknik_tdr"><i class="fa fa-twitter" aria-hidden="true"></i> @teknik_tdr</a><br>
<a href="https://xrubio.com"><i class="fa fa-globe" aria-hidden="true"></i> https://xrubio.com</a><br>
<a href="https://github.com/xrubioj/"><i class="fa fa-github" aria-hidden="true"></i> https://github.com/xrubioj/</a>
</p>
<p style="line-height: 0.6em"><small>This talk is available at:</small><br>
<a href="https://xrubio.com/talks/talk-android-custom-controls-and-canvas/" style="font-size: 0.6em">
https://xrubio.com/talks/talk-android-custom-controls-and-canvas/</a>
</p>
</section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.js"></script>
<script>
// More info https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
history: true,
// More info https://github.com/hakimel/reveal.js#dependencies
dependencies: [
{ src: 'reveal.js/plugin/markdown/marked.js' },
{ src: 'reveal.js/plugin/markdown/markdown.js' },
{ src: 'reveal.js/plugin/notes/notes.js', async: true },
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>