Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Performance Tips

Romain Guy
November 12, 2013

Android Performance Tips

This presentation explains in detail some of the performance improvements provided by Android 4.4. You will also learn about tools and tips you can use to improve performance of your applications.

Romain Guy

November 12, 2013
Tweet

More Decks by Romain Guy

Other Decks in Programming

Transcript

  1. Android Performance Tips
    Romain Guy
    Chet Haase
    google.com/+RomainGuy
    google.com/+ChetHaase
    @romainguy
    @chethaase

    View full-size slide

  2. Performance Improvements

    View full-size slide

  3. #DV13 #AndroidPerf
    Lazy Measurement
    • Introduced in Android 4.4
    - targetSdkVersion must be >= 19
    • Views cache measured dimensions
    - Prior to 4.4 only the last measure was cached
    - In 4.4 all measures are cached until next requestLayout()
    • Can greatly reduce calls to onMeasure()

    View full-size slide

  4. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View

    View full-size slide

  5. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    measure(wSpec1, hSpec1)

    View full-size slide

  6. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    onMeasure(wSpec1, hSpec1)

    View full-size slide

  7. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    setDimensions(w1, h1)
    onMeasure(wSpec1, hSpec1)

    View full-size slide

  8. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    measure(wSpec2, hSpec2)

    View full-size slide

  9. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    measure(wSpec2, hSpec2)
    onMeasure(wSpec2, hSpec2)

    View full-size slide

  10. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    measure(wSpec2, hSpec2)
    setDimensions(w2, h2)
    onMeasure(wSpec2, hSpec2)

    View full-size slide

  11. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View

    View full-size slide

  12. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    requestLayout()

    View full-size slide

  13. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View

    View full-size slide

  14. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    measure(wSpec1, hSpec1)

    View full-size slide

  15. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    cached measure:
    (wSpec1, hSpec1) → (w1, h1)

    View full-size slide

  16. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    setDimensions(w1, h1)
    cached measure:
    (wSpec1, hSpec1) → (w1, h1)

    View full-size slide

  17. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    measure(wSpec2, hSpec2)

    View full-size slide

  18. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    measure(wSpec2, hSpec2)
    onMeasure(wSpec2, hSpec2)

    View full-size slide

  19. #DV13 #AndroidPerf
    Before 4.4
    ViewGroup
    View View
    measure(wSpec2, hSpec2)
    setDimensions(w2, h2)
    onMeasure(wSpec2, hSpec2)

    View full-size slide

  20. #DV13 #AndroidPerf
    Single measure cache
    • Works great in common cases
    • Breaks with multi-pass layouts
    - LinearLayout and weight
    - RelativeLayout
    - Custom layouts

    View full-size slide

  21. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View

    View full-size slide

  22. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec1, hSpec1)

    View full-size slide

  23. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    onMeasure(wSpec1, hSpec1)

    View full-size slide

  24. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    setDimensions(w1, h1)
    onMeasure(wSpec1, hSpec1)

    View full-size slide

  25. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec2, hSpec2)

    View full-size slide

  26. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec2, hSpec2)
    onMeasure(wSpec2, hSpec2)

    View full-size slide

  27. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec2, hSpec2)
    setDimensions(w2, h2)
    onMeasure(wSpec2, hSpec2)

    View full-size slide

  28. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View

    View full-size slide

  29. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec3, hSpec3)

    View full-size slide

  30. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec3, hSpec3)
    onMeasure(wSpec3, hSpec3)

    View full-size slide

  31. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec3, hSpec3)
    setDimensions(w3, h3)
    onMeasure(wSpec3, hSpec3)

    View full-size slide

  32. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec4, hSpec4)

    View full-size slide

  33. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec4, hSpec4)
    onMeasure(wSpec4, hSpec4)

    View full-size slide

  34. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec4, hSpec4)
    setDimensions(w4, h4)
    onMeasure(wSpec4, hSpec4)

    View full-size slide

  35. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View

    View full-size slide

  36. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    requestLayout()

    View full-size slide

  37. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View

    View full-size slide

  38. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec1, hSpec1)

    View full-size slide

  39. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    cached measure:
    (wSpec3, hSpec3) → (w3, h3)

    View full-size slide

  40. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    onMeasure(wSpec3, hSpec3)

    View full-size slide

  41. #DV13 #AndroidPerf
    Multi-pass layout
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    setDimensions(w1, h1)
    onMeasure(wSpec3, hSpec3)

    View full-size slide

  42. #DV13 #AndroidPerf
    Multi-pass layouts
    • Siblings can cause extra onMeasure() calls
    • Nested multi-pass layouts can wreck havoc
    - A leaf View calling requestLayout() can relayout the entire tree!
    • Solution < 4.4
    - Avoid multi-pass layouts
    - Use fewer Views
    • Solution >= 4.4
    - Framework caches multiple measure pairs
    - Cache is cleared on requestLayout()

    View full-size slide

  43. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View

    View full-size slide

  44. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec1, hSpec1)

    View full-size slide

  45. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    onMeasure(wSpec1, hSpec1)

    View full-size slide

  46. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    setDimensions(w1, h1)
    onMeasure(wSpec1, hSpec1)

    View full-size slide

  47. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec2, hSpec2)

    View full-size slide

  48. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec2, hSpec2)
    onMeasure(wSpec2, hSpec2)

    View full-size slide

  49. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec2, hSpec2)
    setDimensions(w2, h2)
    onMeasure(wSpec2, hSpec2)

    View full-size slide

  50. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View

    View full-size slide

  51. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec3, hSpec3)

    View full-size slide

  52. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec3, hSpec3)
    onMeasure(wSpec3, hSpec3)

    View full-size slide

  53. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec3, hSpec3)
    setDimensions(w3, h3)
    onMeasure(wSpec3, hSpec3)

    View full-size slide

  54. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec4, hSpec4)

    View full-size slide

  55. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec4, hSpec4)
    onMeasure(wSpec4, hSpec4)

    View full-size slide

  56. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec4, hSpec4)
    setDimensions(w4, h4)
    onMeasure(wSpec4, hSpec4)

    View full-size slide

  57. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View

    View full-size slide

  58. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    requestLayout()

    View full-size slide

  59. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View

    View full-size slide

  60. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec1, hSpec1)

    View full-size slide

  61. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    cached measures:
    (wSpec3, hSpec3) → (w3, h3)
    (wSpec1, hSpec1) → (w1, h1)

    View full-size slide

  62. #DV13 #AndroidPerf
    With 4.4
    ViewGroup
    View View
    measure(wSpec1, hSpec1)
    setDimensions(w1, h1)
    cached measures:
    (wSpec3, hSpec3) → (w3, h3)
    (wSpec1, hSpec1) → (w1, h1)

    View full-size slide

  63. #DV13 #AndroidPerf
    Example
    RelativeLayout
    LinearLayout
    LinearLayout
    RelativeLayout
    Button
    TextView
    TextView TextView TextView
    RelativeLayout
    TextView TextView TextView

    View full-size slide

  64. #DV13 #AndroidPerf
    Example
    RelativeLayout
    LinearLayout
    LinearLayout
    RelativeLayout
    Button
    TextView
    TextView TextView TextView
    RelativeLayout
    TextView TextView TextView
    requestLayout()

    View full-size slide

  65. #DV13 #AndroidPerf
    Results
    targetSdk 18 19
    measure time
    measure count

    View full-size slide

  66. #DV13 #AndroidPerf
    Results
    targetSdk 18 19
    measure time
    measure count
    6.5ms
    72

    View full-size slide

  67. #DV13 #AndroidPerf
    Results
    targetSdk 18 19
    measure time
    measure count
    6.5ms 0.7ms
    72 14

    View full-size slide

  68. #DV13 #AndroidPerf
    DisplayList Reordering and Merging
    • Batches similar commands
    • Minimizes state changes
    • Minimizes GL operations

    View full-size slide

  69. Save Cancel
    Maximize compatibility
    Include metadata

    View full-size slide

  70. Include metadata Maximize compatibility Cancel
    Save
    Order of drawing commands

    View full-size slide

  71. Include metadata Maximize compatibility Cancel
    Save
    1. Re-ordering

    View full-size slide

  72. Include metadata
    Maximize compatibility
    Cancel
    Save
    2. Merging

    View full-size slide

  73. #DV13 #AndroidPerf

    View full-size slide

  74. #DV13 #AndroidPerf
    Without reordering & merging
    48 draw calls

    View full-size slide

  75. #DV13 #AndroidPerf
    Without reordering & merging
    48 draw calls
    With reordering & merging
    18 draw calls

    View full-size slide

  76. #DV13 #AndroidPerf

    View full-size slide

  77. #DV13 #AndroidPerf
    Without reordering & merging
    576 OpenGL driver calls

    View full-size slide

  78. #DV13 #AndroidPerf
    Without reordering & merging
    576 OpenGL driver calls
    With reordering & merging
    216 OpenGL driver calls

    View full-size slide

  79. #DV13 #AndroidPerf
    Asset Atlas

    View full-size slide

  80. #DV13 #AndroidPerf
    Overdraw Removal
    • Detects coverage, areas hidden by opaque elements
    • Avoids drawing them
    • Eliminates some common overdraw situations
    • But you should still avoid it in your apps

    View full-size slide

  81. #DV13 #AndroidPerf
    Overdraw Removal
    • Detects coverage, areas hidden by opaque elements
    • Avoids drawing them
    • Eliminates some common overdraw situations
    • But you should still avoid it in your apps
    Overdraw
    Window Background 1x

    View full-size slide

  82. #DV13 #AndroidPerf
    Overdraw Removal
    • Detects coverage, areas hidden by opaque elements
    • Avoids drawing them
    • Eliminates some common overdraw situations
    • But you should still avoid it in your apps
    Overdraw
    Container Background 2x

    View full-size slide

  83. #DV13 #AndroidPerf
    Overdraw Removal
    • Detects coverage, areas hidden by opaque elements
    • Avoids drawing them
    • Eliminates some common overdraw situations
    • But you should still avoid it in your apps
    Overdraw
    Button Button
    2.1x
    Views

    View full-size slide

  84. #DV13 #AndroidPerf
    Overdraw Removal
    • Detects coverage, areas hidden by opaque elements
    • Avoids drawing them
    • Eliminates some common overdraw situations
    • But you should still avoid it in your apps

    View full-size slide

  85. #DV13 #AndroidPerf
    Overdraw Removal
    • Detects coverage, areas hidden by opaque elements
    • Avoids drawing them
    • Eliminates some common overdraw situations
    • But you should still avoid it in your apps
    Overdraw

    View full-size slide

  86. #DV13 #AndroidPerf
    Overdraw Removal
    • Detects coverage, areas hidden by opaque elements
    • Avoids drawing them
    • Eliminates some common overdraw situations
    • But you should still avoid it in your apps
    Overdraw
    Window Background 0x

    View full-size slide

  87. #DV13 #AndroidPerf
    Overdraw Removal
    • Detects coverage, areas hidden by opaque elements
    • Avoids drawing them
    • Eliminates some common overdraw situations
    • But you should still avoid it in your apps
    Overdraw
    Container Background 1x

    View full-size slide

  88. #DV13 #AndroidPerf
    Overdraw Removal
    • Detects coverage, areas hidden by opaque elements
    • Avoids drawing them
    • Eliminates some common overdraw situations
    • But you should still avoid it in your apps
    Overdraw
    Button Button
    1.1x
    Views

    View full-size slide

  89. #DV13 #AndroidPerf
    Bitmap Reuse
    • Reuse in 3.0 required Bitmaps of same size/configuration
    • Bitmaps can now be reconfigured/resized
    - Can create scratch buffer and load smaller bitmaps into it

    View full-size slide

  90. Performance Tools

    View full-size slide

  91. #DV13 #AndroidPerf
    Non-Rectangular Clipping Detection

    View full-size slide

  92. #DV13 #AndroidPerf

    View full-size slide

  93. #DV13 #AndroidPerf
    Overdraw debug

    View full-size slide

  94. #DV13 #AndroidPerf

    View full-size slide

  95. #DV13 #AndroidPerf

    View full-size slide

  96. procstats: UI

    View full-size slide

  97. procstats: command line
    $ adb shell dumpsys procstats com.google.android.apps.maps
    COMMITTED STATS FROM 2013-11-05-18-04-58:
    * com.google.android.apps.maps / u0a60:
    TOTAL: 1.1%
    Service: 1.1%
    (Cached): 99% (98MB-98MB-99MB/96MB-97MB-97MB over 7)
    Run time Stats:
    Screen Off / Norm / +1h19m25s22ms
    Screen On / Norm / +10m43s963ms
    TOTAL: +1h30m8s985ms
    Start time: 2013-11-05 18:04:58
    Total elapsed time: +5h9m53s44ms (complete) libdvm.so chromeview

    View full-size slide

  98. #DV13 #AndroidPerf
    GL Tracer

    View full-size slide

  99. #DV13 #AndroidPerf
    Systrace
    • Introduced in Jellybean 4.1
    • Recent improvements
    - More framework tags
    - More categories
    - Easier to launch
    - App-level tags

    View full-size slide

  100. #DV13 #AndroidPerf
    Systrace
    • Introduced in Jellybean 4.1
    • Recent improvements
    - More framework tags
    - More categories
    - Easier to launch
    - App-level tags
    Trace.beginSection("someLabel");
    // stuff
    Trace.endSection();

    View full-size slide

  101. #DV13 #AndroidPerf
    Android Studio System Information

    View full-size slide

  102. #DV13 #AndroidPerf

    View full-size slide

  103. #DV13 #AndroidPerf

    View full-size slide

  104. Performance Tips

    View full-size slide

  105. #DV13 #AndroidPerf
    Bitmap Reuse
    mBitmapCache = Bitmap.createBitmap(500, 500,
    Bitmap.Config.ARGB_8888);
    mBitmapOptions.inBitmap = mBitmapCache;
    mBitmapOptions.inSampleSize = 1;
    Bitmap newBitmap;
    if (mCheckBox.isChecked()) {
    newBitmap = BitmapFactory.decodeResource(
    getResources(), images[index],
    mBitmapOptions);
    } else {
    newBitmap = BitmapFactory.decodeResource(
    getResources(), images[index]);
    }
    mImageView.setImageBitmap(newBitmap);

    View full-size slide

  106. #DV13 #AndroidPerf
    Atlas
    • Popular in games, on web sites, etc…
    - Also called “sprite sheets“
    • Can increase performance
    - Reduce number of OpenGL driver calls
    - Reduce number of GPU state changes
    - Better merging of draw calls

    View full-size slide

  107. #DV13 #AndroidPerf
    Atlas example
    10 photos
    Drawn column by column

    View full-size slide

  108. #DV13 #AndroidPerf
    Atlas source

    View full-size slide

  109. #DV13 #AndroidPerf
    Loading the atlas 1/4
    • res/raw/atlas.txt
    • Format:
    atlas_name
    ImageName,left,top,right,bottom
    ImageName,left,top,right,bottom
    ImageName,left,top,right,bottom

    View full-size slide

  110. #DV13 #AndroidPerf
    Loading the atlas 2/4
    atlas
    Mountain,1,1,301,201
    Gecko,302,1,602,201
    Seahorse,603,1,903,201
    Camera,904,1,1204,201
    Fish,1205,1,1505,201
    Plasma,1,202,301,402
    Grass,302,202,602,402
    Photographer,603,202,903,402
    Lights,904,202,1204,402
    Flower,1205,202,1505,402

    View full-size slide

  111. #DV13 #AndroidPerf
    Loading the atlas 3/4
    String line = reader.readLine();
    Resources res = getContext().getResources();
    int atlasId = res.getIdentifier(line, "drawable",
    getContext().getPackageName());
    mAtlas = BitmapFactory.decodeResource(res, atlasId);

    View full-size slide

  112. #DV13 #AndroidPerf
    Loading the atlas 3/4
    String line = reader.readLine();
    Resources res = getContext().getResources();
    int atlasId = res.getIdentifier(line, "drawable",
    getContext().getPackageName());
    mAtlas = BitmapFactory.decodeResource(res, atlasId);

    View full-size slide

  113. #DV13 #AndroidPerf
    Loading the atlas 3/4
    String line = reader.readLine();
    Resources res = getContext().getResources();
    int atlasId = res.getIdentifier(line, "drawable",
    getContext().getPackageName());
    mAtlas = BitmapFactory.decodeResource(res, atlasId);

    View full-size slide

  114. #DV13 #AndroidPerf
    Loading the atlas 4/4
    static class Entry {
    String name;
    final Rect src = new Rect();
    }
    while ((line = reader.readLine()) != null) {
    String[] fields = line.split(",");
    Entry entry = new Entry();
    entry.name = fields[0];
    entry.src.set(parseInt(fields[1]), parseInt(fields[2]),
    parseInt(fields[3]), parseInt(fields[4]));
    mEntries.add(entry);
    }

    View full-size slide

  115. #DV13 #AndroidPerf
    Drawing the atlas
    protected void onDraw(Canvas canvas) {
    final int count = mEntries.size();
    for (int i = 0; i < count; i++) {
    Entry entry = mEntries.get(i);
    int x = computeX(entry);
    int y = computeY(entry);
    mDst.set(x, y, x + entry.src.width(),
    y + entry.src.height());
    canvas.drawBitmap(mAtlas, entry.src, mDst, null);
    }
    }

    View full-size slide

  116. #DV13 #AndroidPerf
    Drawing the atlas
    protected void onDraw(Canvas canvas) {
    final int count = mEntries.size();
    for (int i = 0; i < count; i++) {
    Entry entry = mEntries.get(i);
    int x = computeX(entry);
    int y = computeY(entry);
    mDst.set(x, y, x + entry.src.width(),
    y + entry.src.height());
    canvas.drawBitmap(mAtlas, entry.src, mDst, null);
    }
    }

    View full-size slide

  117. #DV13 #AndroidPerf
    Drawing the atlas
    protected void onDraw(Canvas canvas) {
    final int count = mEntries.size();
    for (int i = 0; i < count; i++) {
    Entry entry = mEntries.get(i);
    int x = computeX(entry);
    int y = computeY(entry);
    mDst.set(x, y, x + entry.src.width(),
    y + entry.src.height());
    canvas.drawBitmap(mAtlas, entry.src, mDst, null);
    }
    }

    View full-size slide

  118. #DV13 #AndroidPerf
    Atlas tips 1/2
    • 1px transparent border around entries
    - Allows proper bilinear filtering
    - For scaling, rotation, perspective, etc.
    • Watch out for max texture size
    - Especially at xxhdpi
    - 2048x2048 is common
    - 4096x4096 on recent devices

    View full-size slide

  119. #DV13 #AndroidPerf

    View full-size slide

  120. #DV13 #AndroidPerf
    Atlas tips 2/2
    • Disable blending for opaque entries
    • Use a Paint with the SRC blend mode
    - mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC))
    • Store this information in atlas file
    - Name,left,top,right,bottom,1 // 1=opaque, 0= transparent

    View full-size slide

  121. #DV13 #AndroidPerf
    Layout
    • Don’t use multi-pass layouts at the top of the tree
    - Very expensive
    • LinearLayout is multi-pass… sometimes
    - For children with a layout_weight
    • RelativeLayout is always multi-pass

    View full-size slide

  122. #DV13 #AndroidPerf
    Tips for LinearLayout
    • Vertical layouts
    - Set height to 0dp for children with a weight
    • Horizontal layouts
    - Set width to 0dp for children with a weight
    - And set baselineAligned=”false” on LinearLayout
    • These tips don’t always apply

    View full-size slide

  123. #DV13 #AndroidPerf
    Tips for RelativeLayout
    • Don’t use it

    View full-size slide

  124. #DV13 #AndroidPerf
    Tips for RelativeLayout
    • Alright you can use it
    • But be careful

    View full-size slide

  125. Non-Rectangular Clipping
    59
    @Override
    protected void onDraw(Canvas canvas) {
    // Keep the jellybeans
    canvas.clipRect(l, t, r, b);
    // Rotate the jar
    canvas.rotate(-30.0f, pX, pY);
    // Draw the jar
    canvas.drawBitmap(mJellyBeans, x, y, null);
    }

    View full-size slide

  126. 61
    @Override
    protected void onDraw(Canvas canvas) {
    // Rotate the jar
    canvas.rotate(-30.0f, pX, pY);
    // Keep the jellybeans
    canvas.clipRect(l, t, r, b);
    // Draw the jar
    canvas.drawBitmap(mJellyBeans, x, y, null);
    }

    View full-size slide

  127. Stencil buffer
    63

    View full-size slide

  128. Stencil buffer
    63

    View full-size slide

  129. #DV13 #AndroidPerf
    Don’t Update Layers
    • Avoid invalidating layered views/containers
    - Don’t change view or child views, remove children, add children, ...
    view.setLayerType(View.LATER_TYPE_HARDWARE, null);
    // run animation
    view.setLayerType(View.LAYER_TYPE_NONE, null);
    // or
    view.animate().alpha(0).withLayer();

    View full-size slide

  130. Using the Tools

    View full-size slide

  131. #DV13 #AndroidPerf
    Systrace
    • Overall system performance

    View full-size slide

  132. #DV13 #AndroidPerf
    For More Information
    • Google I/O talks
    • Parleys.com talks
    • Devbytes on YouTube
    Chet
    graphics-geek.blogspot.com
    google.com/+ChetHaase
    @chethaase
    Romain:
    curious-creature.org
    google.com/+RomainGuy
    @romainguy

    View full-size slide