Slide 1

Slide 1 text

Mtg ANDROID DRAWABLES wh Cyril Moi

Slide 2

Slide 2 text

Tnk y f comg

Slide 3

Slide 3 text

@cyrilmottier cyrilmottier.com

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

hp://w.capaetra.com/jobs

Slide 6

Slide 6 text

An troduc  ANDROID DRAWABLES?

Slide 7

Slide 7 text

An abstrac   ty  c be drawn  a Cv Drawable „ „ Canvas

Slide 8

Slide 8 text

View Ctry  a Drawable don’t al wh msu/yt,  ly draw(Canvas)

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

getIntrinsicWidth() getIntrinsicHeight()

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

getBounds()

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

setState(int[])

Slide 16

Slide 16 text

setLevel(int)

Slide 17

Slide 17 text

Drawable Usg a from XML ...

Slide 18

Slide 18 text

1

Slide 19

Slide 19 text

 from Java ...

Slide 20

Slide 20 text

1 ImageView imageView = (ImageView) 2 findViewById(R.id.image_view); 3 4 imageView.setImageResource(R.drawable.ic_share);

Slide 21

Slide 21 text

Tt’s a folks!

Slide 22

Slide 22 text

Tt’s a folks! wt k ...

Slide 23

Slide 23 text

T Drawab LOADING MECHANISM

Slide 24

Slide 24 text

A sg meod  ru m a

Slide 25

Slide 25 text

Drawable getDrawable(int) android.content.res.Resources In

Slide 26

Slide 26 text

Drawable object? Wt   turn

Slide 27

Slide 27 text

Drawable

Slide 28

Slide 28 text

Drawable BitmapDrawable ColorDrawable

Slide 29

Slide 29 text

Drawable BitmapDrawable DrawableContainer ColorDrawable

Slide 30

Slide 30 text

Drawable BitmapDrawable DrawableContainer ColorDrawable StateListDrawable LayerDrawable

Slide 31

Slide 31 text

Drawable BitmapDrawable DrawableContainer ColorDrawable StateListDrawable LayerDrawable

Slide 32

Slide 32 text

getDrawable(int) impmtn T

Slide 33

Slide 33 text

File ends with «.xml»

Slide 34

Slide 34 text

Inflate from XML File ends with «.xml» YES

Slide 35

Slide 35 text

Inflate from XML File ends with «.xml» Create from stream to jpg/png YES NO

Slide 36

Slide 36 text

Inflate from XML File ends with «.xml» Create from stream to jpg/png Return the new Drawable YES NO

Slide 37

Slide 37 text

Inflate from XML File ends with «.xml» Create from stream to jpg/png Return the new Drawable YES NO

Slide 38

Slide 38 text

Let’s py!

Slide 39

Slide 39 text

1 @Override 2 public void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 5 Resources r = getResources(); 6 Drawable d1 = r.getDrawable(R.drawable.ic_launcher); 7 Drawable d2 = r.getDrawable(R.drawable.ic_launcher); 8 9 Log.d(LOG_TAG, "d1: " + d1); 10 Log.d(LOG_TAG, "d2: " + d2); 11 }

Slide 40

Slide 40 text

03-23 16:39:29.745: DEBUG/Activity(19742): d1: android.graphics.drawable.BitmapDrawable@417e70c8 03-23 16:39:29.745: DEBUG/Activity(19742): d2: android.graphics.drawable.BitmapDrawable@417e7130 tputs

Slide 41

Slide 41 text

03-23 16:39:29.745: DEBUG/Activity(19742): d1: android.graphics.drawable.BitmapDrawable@417e70c8 03-23 16:39:29.745: DEBUG/Activity(19742): d2: android.graphics.drawable.BitmapDrawable@417e7130 tputs

Slide 42

Slide 42 text

03-23 16:39:29.745: DEBUG/Activity(19742): d1: android.graphics.drawable.BitmapDrawable@417e70c8 03-23 16:39:29.745: DEBUG/Activity(19742): d2: android.graphics.drawable.BitmapDrawable@417e7130 tputs

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

getDrawable(int) always turns a new Drawab

Slide 45

Slide 45 text

but ...

Slide 46

Slide 46 text

1 @Override 2 public void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 5 Resources r = getResources(); 6 Drawable d1 = r.getDrawable(R.drawable.ic_launcher); 7 Drawable d2 = r.getDrawable(R.drawable.ic_launcher); 8 9 Log.d(LOG_TAG, "d1.cs: " + d1.getConstantState()); 10 Log.d(LOG_TAG, "d2.cs: " + d2.getConstantState()); 11 }

Slide 47

Slide 47 text

03-23 16:42:51.345: DEBUG/Activity(19742): d1.cs: android.graphics.drawable.BitmapDrawable$BitmapState@418078a8 03-23 16:42:51.345: DEBUG/Activity(19742): d2.cs: android.graphics.drawable.BitmapDrawable$BitmapState@418078a8 tputs

Slide 48

Slide 48 text

03-23 16:42:51.345: DEBUG/Activity(19742): d1.cs: android.graphics.drawable.BitmapDrawable$BitmapState@418078a8 03-23 16:42:51.345: DEBUG/Activity(19742): d2.cs: android.graphics.drawable.BitmapDrawable$BitmapState@418078a8 tputs

Slide 49

Slide 49 text

03-23 16:42:51.345: DEBUG/Activity(19742): d1.cs: android.graphics.drawable.BitmapDrawable$BitmapState@418078a8 03-23 16:42:51.345: DEBUG/Activity(19742): d2.cs: android.graphics.drawable.BitmapDrawable$BitmapState@418078a8 tputs

Slide 50

Slide 50 text

Drawables ConstantState s ir

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

See http://www.curious-creature.org/2009/05/02/drawable-mutations/ for more information ConstantState may be a probm somem

Slide 53

Slide 53 text

Drawable.mutate() Cag ConstantState copi 

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Some NICE GOTCHAS

Slide 56

Slide 56 text

StateListDrawable LevelListDrawable LayerDrawable TransitionDrawable ColorDrawable GradientDrawable ScaleDrawable ClipDrawable RotateDrawable AnimationDrawable InsetDrawable BitmapDrawable NinePatchDrawable

Slide 57

Slide 57 text

StateListDrawable LevelListDrawable LayerDrawable TransitionDrawable ColorDrawable GradientDrawable ScaleDrawable ClipDrawable RotateDrawable AnimationDrawable InsetDrawable BitmapDrawable NinePatchDrawable Resources.NotFoundException

Slide 58

Slide 58 text

1 2

Slide 59

Slide 59 text

1 2

Slide 60

Slide 60 text

1 2

Slide 61

Slide 61 text

1 2

Slide 62

Slide 62 text

1 2

Slide 63

Slide 63 text

1 2 4 5 6 7 8 9 10

Slide 64

Slide 64 text

1 2 4 5 6 7 8 9 10 On Jly Bn:

Slide 65

Slide 65 text

1 2 4 5 6 7 8 9 10 On Ggbad:

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

ColorDrawable  not cli prr  Heycomb

Slide 68

Slide 68 text

1 2 4 5 6 7 8 9 10 11 12 13 Use a solid col GraditDrawab std

Slide 69

Slide 69 text

android.graphics.drawable Have a lk  Wt me?

Slide 70

Slide 70 text

Advc DRAWABLE USAGE

Slide 71

Slide 71 text

Usg a Drawable  a custom View  sy

Slide 72

Slide 72 text

1 public class DrawableView extends View { 2 private Drawable mDrawable; 3 4 public DrawableView(Context context) { super(context); } 5 6 public void setDrawable(Drawable d) { 7 if (d != mDrawable) { 8 mDrawable = d; 9 if (d != null) { 10 updateDrawableBounds(); 11 } 12 } 13 } 14 15 /** Update Drawable bounds with Drawable.setBounds(int, int, int, int) */ 16 private void updateDrawableBounds() { /* ... */ } 17 18 @Override 19 protected void onDraw(Canvas canvas) { 20 super.onDraw(canvas); 21 if (mDrawable != null) { 22 mDrawable.draw(canvas); 23 } 24 } 25 }

Slide 73

Slide 73 text

AnimationDrawable Let’s use  wh  new View

Slide 74

Slide 74 text

C e y wi ...

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

Drawable.Callback T sw:

Slide 77

Slide 77 text

1 public interface Callback { 2 public void invalidateDrawable(Drawable who); 3 4 public void scheduleDrawable(Drawable who, 5 Runnable what, 6 long when); 7 8 public void unscheduleDrawable(Drawable who, 9 Runnable what); 10 }

Slide 78

Slide 78 text

1 public class View implements Drawable.Callback, 2 KeyEvent.Callback, 3 AccessibilityEventSource { 4 5 /** android.view.View code */ 6 7 }

Slide 79

Slide 79 text

verifyDrawable(Drawable) Just use

Slide 80

Slide 80 text

1 public void setDrawable(Drawable d) { 2 if (d != mDrawable) { 3 if (mDrawable != null) { 4 mDrawable.setCallback(null); 5 } 6 mDrawable = d; 7 if (d != null) { 8 d.setCallback(this); 9 updateDrawableBounds(); 10 } 11 } 12 } 13 14 @Override 15 protected boolean verifyDrawable(Drawable who) { 16 return super.verifyDrawable(who) || who == mDrawable; 17 } Ungt  pvus caback  avoid aks

Slide 81

Slide 81 text

1 public void setDrawable(Drawable d) { 2 if (d != mDrawable) { 3 if (mDrawable != null) { 4 mDrawable.setCallback(null); 5 } 6 mDrawable = d; 7 if (d != null) { 8 d.setCallback(this); 9 updateDrawableBounds(); 10 } 11 } 12 } 13 14 @Override 15 protected boolean verifyDrawable(Drawable who) { 16 return super.verifyDrawable(who) || who == mDrawable; 17 } Regt  cut view   new caback

Slide 82

Slide 82 text

1 public void setDrawable(Drawable d) { 2 if (d != mDrawable) { 3 if (mDrawable != null) { 4 mDrawable.setCallback(null); 5 } 6 mDrawable = d; 7 if (d != null) { 8 d.setCallback(this); 9 updateDrawableBounds(); 10 } 11 } 12 } 13 14 @Override 15 protected boolean verifyDrawable(Drawable who) { 16 return super.verifyDrawable(who) || who == mDrawable; 17 } D’t fget  ca  sup meod

Slide 83

Slide 83 text

Fg VIEW HIERARCHY

Slide 84

Slide 84 text

disclaimer: this is an imaginary screen

Slide 85

Slide 85 text

1 5 6 7 8 9 AndroidManifest.xml In

Slide 86

Slide 86 text

1 2 7 8 14 15 20 21 26 27 32 33 34 35

Slide 87

Slide 87 text

1 2 7 8 14 15 20 21 26 27 32 33 34 35 Use  almt uss rt FrameLayt  hold  backgr

Slide 88

Slide 88 text

1 2 7 8 14 15 20 21 26 27 32 33 34 35 T logo c be csid   backgr

Slide 89

Slide 89 text

1 2 7 8 14 15 20 21 26 27 32 33 34 35 «Sign Up/In» bus e  ly actual ctt

Slide 90

Slide 90 text

disclaimer: this is an imaginary screen

Slide 91

Slide 91 text

disclaimer: this is an imaginary screen

Slide 92

Slide 92 text

Drawable A -b be solu ...

Slide 93

Slide 93 text

1 2 9 10 15 16 21 22

Slide 94

Slide 94 text

1 2 4 5 6 7 9 10 11 12 14 17 18 19 Backgr col wh ct logo  p

Slide 95

Slide 95 text

1 5 6 7 8 9 AndroidManifest.xml In

Slide 96

Slide 96 text

1 2 3 4 7 <item name="android:windowBackground">@drawable/login</item> 8 9 10 res/values/themes.xml In

Slide 97

Slide 97 text

disclaimer: this is an imaginary screen

Slide 98

Slide 98 text

disclaimer: this is an imaginary screen

Slide 99

Slide 99 text

+ + + + + + + + + + + + + + + + + + + + + + + + + Mimiz yt & be uhg im

Slide 100

Slide 100 text

T CONCLUSION

Slide 101

Slide 101 text

Sp kg   View v ly

Slide 102

Slide 102 text

DRAWABLES ARE light-weig

Slide 103

Slide 103 text

DRAWABLES ARE system-cacd

Slide 104

Slide 104 text

DRAWABLES ARE cfig-pdt

Slide 105

Slide 105 text

DRAWABLES ARE sy  use

Slide 106

Slide 106 text

DRAWABLES ARE sy awome :)

Slide 107

Slide 107 text

CYRIL MOTTIER @cyrilmoi cyrilmoi.com