Animation / Button / Floating Action Button / VectorDrawable

Floating Action Button – Part 2

In the previous article we constructed a simple implementation of a Floating Action Button which conformed to the material design guidelines in terms of being elevated above the background (with the appropriate shadow) and rising to meet your finger when it is touched. In this article we’ll turn our attention to animating the icon to toggle between two states.

The FAB that we created contains a plus symbol which we created using a VectorDrawable. What we’re going to do is toggle the button between two states, and animate between the ‘plus’ symbol that we currently have, and a ‘minus’ symbol.

Let’s start by creating the VectorDrawable for the minus symbol:

This is identical to the plus symbol with the exception of the pathData, I have added a couple of extra commands which effectively do nothing, but match the type and number of drawing commands in the plus VectorDrawable. This will enable us to perform a path animation to transition between the two. For more information on animating path data have a read of Vector Drawables – Part 4

What I have done here is to simply draw the vertical element of the plus, but with zero height so that we can animate this element in and out when we transition.

Next we need to create a few property animators. The first will perform a clockwise rotation through 90 degrees:

The next will reverse this and perform an anti-clockwise rotation through 90 degrees:

The next will perform a pathData animation from the plus to minus:

And the finl one will reverse this and perform a pathData animation from the the minus to the plus:

Everything that we’ve done up to this point is creating the components that we require for two AnimatedVectorDrawables (more information on this can be found in the VectorDrawables series) which will perform the transitions between the two button states. The first will apply the clockwise rotation to the group within the plus drawable, and the plus to minus pathData animation to the path:

The second will perform the anti-clockwise animation and minus to plus animation:

All that remains is to hook these up:

When the FAB is tapped we toggle the state, switch the drawable, and begin the animation and we get an extremely smooth transition between the two FAB states:

With the transiitions of the FAB itself in place, in the next article we’ll turn our attention to adding some mini FAB actions which will appear and disappear as we toggle the FAB state.

The source code for this article is available here.

© 2015, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Floating Action Button – Part 2 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

4 Comments

  1. I really don’t like the fact that you are using v21 for these sample, since its no what we are going to use (We need backward compatibility, you know).

    Please could you fix these post using the the AppCompact, thanks!

    1. No.

      I explained in Part 1 why I made this decision, and I further explain in Part 3 that AppCompat simply does not contain the tools to properly adhere to the material design guidelines. When it does then I’ll look at porting it, but not before then.

    1. I explained in the article that it was specific to Lollipop and later and that the Compat libraries are currently insufficient to implement this well pre-Lollipop.

Leave a Reply

Your email address will not be published. Required fields are marked *