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

I Can Animate and So Can You (GDG)

Daniel Lew
February 08, 2014

I Can Animate and So Can You (GDG)

A medium-length version of a talk I gave about Android animations at GDG Devfest MN 2014.

Keynote version: http://goo.gl/CFWCll

Daniel Lew

February 08, 2014
Tweet

More Decks by Daniel Lew

Other Decks in Programming

Transcript

  1. Building Animations • Figure out what attributes to animate •

    Design View hierarchy • Measure attribute start/ end values • Setup and run animators
  2. Determining Attributes • Alpha - every element • Translate -

    every row • Translate - details header/footer • Scale - details rows (a hardcore fake) • Fading background - not needed!
  3. Measuring Views • Problem: Views not measured until sometime after

    inflation • ViewTreeObserver - lets you know when a View has been measured • Two options: OnGlobalLayoutListener, OnPreDrawListener
  4. ViewTreeObserver Pattern ! final View view = findViewById(R.id.view); view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener()

    { public boolean onPreDraw() { view.getViewTreeObserver().removeOnPreDrawListener(this); // Measure here! return true; } });
  5. Measuring "After" • Align to other existing Views • Create

    invisible "dummy" Views to animate towards • Start in the correct position, animate towards it
  6. Running Animators • ObjectAnimator - animate! • AnimatorSet - synchronize

    multiple Animators • AnimatorListener - setup and cleanup • Canceling Animators w/ reverse()
  7. Property Animators • Generic animation framework • Animates *any* value

    over time • Basic building block for most animations
  8. ObjectAnimator • Type of property animator • Used for animating

    essential View properties • Position (or translation) • Rotation • Scale • Alpha • Can be used on custom View properties
  9. ObjectAnimator // Uses setAlpha() and getAlpha() to animate ObjectAnimator.ofFloat(myView, "alpha",

    0, 1).start(); ! ! ! // Uses built-in property to avoid reflection ObjectAnimator.ofFloat(myView, View.ALPHA, 1).start();
  10. AnimatorSet ! ObjectAnimator one = ObjectAnimator.ofFloat(view1, "alpha", 1); ObjectAnimator two

    = ObjectAnimator.ofFloat(view2, "alpha", 1); ! AnimatorSet set = new AnimatorSet(); set.playTogether(one, two); set.start();
  11. Animator Listeners ! ObjectAnimator anim = ObjectAnimator.ofFloat(view, "alpha", 1); anim.addListener(new

    AnimatorListenerAdapter() { public void onAnimationEnd(Animator animation) { // Clean-up code here } }); anim.start();
  12. Reversing Course • ValueAnimator.reverse() • Useful for smoothly canceling an

    animator • Can recursively traverse AnimatorSet for ValueAnimators
  13. Performance • Choppy animation == painful • Performance cannot be

    an afterthought • Smooth animations informs View structure
  14. Performance Breakdown • Layout - size/location of Views • Draw

    - what's inside Views • Render - pushing it on screen
  15. Render Performance • Initiated every time pixels are put on

    screen • Varies with pixel count/ speed of device • Try to reduce overdraw
  16. Profile GPU Rendering • Blue - Draw • Red -

    Process • Orange - Execute • Green line - your goal
  17. Show GPU Overdraw • Shows overdrawn pixels • Best to

    worst: nothing, blue, green, red, dark red • Some overdraw necessary, but reduce if possible
  18. Show Hardware Layer Updates • Flashes green on HW layer

    redraw • If constantly flashing, you're doing something wrong!
  19. Profile Notes • Other tools: traceview, systrace, OpenGL traces •

    Profilers can be inaccurate • One profiling tool at a time, please • Profile multiple devices
  20. Optimization: Layers • Layers store drawn Views for reuse •

    Reduces drawing during animation • Reduces overdraw during animation
  21. Layer Pattern ! final View myView = findViewById(R.id.my_view); myView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

    myView.animate() .translationX(50) .setListener(new AnimatorListenerAdapter() { public void onAnimationEnd(Animator animation) { myView.setLayerType(View.LAYER_TYPE_NONE, null); } }) .start();
  22. Layer Caveats • Warning: redrawing a layer is *slower* than

    non-layer • DisplayLists will rebuild layers when necessary • Layering simple Views just hurts performance
  23. ImageView Blocking Layout ! public void setIsFixedSize(boolean isFixedSize) { mIsFixedSize

    = isFixedSize; } ! public void setImageBitmap(Bitmap bm) { mBlockMeasurement = true; super.setImageBitmap(bm); mBlockMeasurement = false; } ! public void requestLayout() { if (!mBlockMeasurement || !mIsFixedSize) { super.requestLayout(); } }
  24. SurfaceView and TextureView • Great for constantly animated View •

    SurfaceView is fast but is weird • TextureView is 4.0+, but can be moved around
  25. Activity Window Animations • Custom tween animations between Activities •

    Limited to full-screen transitions • Activity.overridePendingTransition() • android:windowAnimationStyle
  26. Fragment Animations • Animations during Fragment transactions • Limited to

    full-Fragment animations • Fragment.onCreateAnimation() • FragmentTransaction.setCustomAnimations() • FragmentTransaction.setTransition() and setTransitionStyle()
  27. set directions • View methods: setLeft(), setRight(), setTop(), setBottom() •

    Change layout without requesting layout! • Parent layout blows away values, may have to block
  28. Drawable Animations • AnimationDrawable - frame animations • Custom animated

    Drawable in a View • Drawable.setCallback() • View.verifyDrawable()
  29. Resources • Android: goo.gl/4aZIzM • DevBytes: goo.gl/QeTM9B • Chet Haase:

    graphics-geek.blogspot.com/ • Romain Guy: curious-creature.org • Google IO Sessions (esp. with Haase/Guy)
  30. Thank You! • Blog: http://daniel-codes.blogspot.com/ • Movies App Source: http://goo.gl/WiRTlQ

    • Twitter: @danlew42 • Plus: https://plus.google.com/+DanielLew/