How to use Android Spruce Animation Library

Android Spruce is a lightweight animation library that helps choreograph the animations on the screen. With so many different animation libraries out there, developers need to make sure that each view is animating at the appropriate time. Spruce can help designers request complex multi-view animations and not have the developers cringe at the prototype.

’cause we just love our animations, don’t we? 🙂 When designers set up complex multi-view animations, this is the lib to call out for help. Spruce provides 8 SortFunction (change the order in which views animate) implementations, but there is a possibility to make more. For example:

How to use Android Spruce Animation Library
How to use Android Spruce Animation Library



Add the following to your project’s build.gradle file

dependencies {
    compile 'com.willowtreeapps.spruce:spruce-android:1.0.0'


For javadocs checkout the documentation for more information.

How to use Android Spruce Animation Library

Animator spruceAnimator = new Spruce
        .sortWith(new Default(/*interObjectDelay=*/50L))
        .animateWith(new Animator[] {DefaultAnimations.shrinkAnimator(parentViewGroup, /*duration=*/800)})


Checkout the builder documentation for more information.

Preparing for Animation

Spruce comes packed with Animator options within the DefaultAnimations class meant to make your life easier when calling an animation. Let’s say we want to have your views fade in. For example, we would create an animators = new Animator[] {}and add DefaultAnimations.fadeInAnimator(parentViewGroup, /*duration=*/800) as an array item. If you want a view to fade in, then you need to make sure that it is already faded out. To do that, we need to set the alpha to 0 on the views or you could first use a fade out animator.

Running the Animation

Use the following command to run a basic animation on your view.

Animator spruceAnimator = new Spruce
        .sortWith(new DefaultSort(/*interObjectDelay=*/50L))


Checkout default animation documentation for more information.

Using a SortFunction

Luckily, Spruce comes with 8 SortFunction implementations with a wide open possibility to make more! Use the SortFunction to change the order in which views animate. Consider the following example:

LinearSort sort = new LinearSort(/*interObjectDelay=*/100L, /*reversed=*/false, LinearSort.Direction.TOP_TO_BOTTOM);


In this example we have created a LinearSort which will have views animate in from the top to bottom. We can change the look and feel of the animation by using a RadialSort instead which will have the views animate in a circular fashion. If we wanted to use this sort in an actual Spruce start() call then that would look something like:


Animator spruceAnimator = new Spruce
        .sortWith(new LinearSort(/*interObjectDelay=*/100L, /*reversed=*/false, LinearSort.Direction.TOP_TO_BOTTOM))
        .animateWith(DefaultAnimations.shrinkAnimator(parentViewGroup, /*duration=*/800))


Definitely play around with the stock SortFunction implementations until you find the one that is perfect for you! Check out the example app if you want to get previews of what each SortFunction will look like.

The Animators

The animations used in Spruce are produced by leveraging the Animator class. You may provide your own custom animations by creating your own Animator and provide it to the as part of an Animator[] to SpruceBuilder.animateWith(Animator... animators). For more information on using the Animator class please check out

Standard Animation

The DefaultAnimation class provides simple Animator methods to apply the change Animator to the views. Use this class if you want to have a stock linear movement of the changes.

Sort Functions

With all different types of animations, especially those dealing with subviews, we have to consider a way in which we want to animate them. Some views can have 0 subviews while others may have hundreds. To handle this, we have the notion of a SortFunction. What this will do is take each of the subviews in the ViewGroup, and apply a mapping from the specific subview to the exact delay that it should wait before animating. Some of these will sort in a radial formation while others may actually sort randomly. One of the cool features of Spruce, is that you can actually define your own SortFunction and then the animation will look completely different. Luckily, Spruce also comes jam packed with a ton of default SortFunctionclasses to make everything easier on you as the developer. Take a look at some of the default SortFunction classes we have and see if you can use them or branch off of them for your cool and custom animations!

The SortFunction Interface

A very simple interface that requires classes to extend the following class

public abstract class SortFunction {
    public abstract List<SpruceTimedView> getViewListWithTimeOffsets(ViewGroup parent, List<View> children);


What the above class needs to do is take in a ViewGroup parent and a List of View children or subviews to generate a list of subviews and their animation offsets. Once the list of subviews has been generated, you can define your own sort metric to determine in which order the View‘s should animate. To do so, you need to create a List of SpruceTimedView‘s. This special class has two properties: (1) View view and (2) long timeOffset. Your SortFunction can define the timeOffset however it likes, but the animators will use this to determine how long it should delay the start of that specific view from animating. The best way to learn, is to play around. So why not have some fun and make your own SortFunction!

About Sort Functions

To make sure that developers can use Spruce out of the box, we included about 8 stock SortFunction implementations. These are some of the main functions we use at WillowTree and are so excited to see what others come up with!

  • DefaultSort
  • LinearSort
  • CorneredSort
  • RadialSort
  • RandomSort
  • InlineSort
  • ContinousSort
  • ContinuousWeightedSort

Check out the docs here for more information

Stock Animators

To make everybody’s lives easier, the stock animators perform basic View animations that a lot of apps use today. Mix and match these animators to get the core motion you are looking for.

  • DefaultAnimations.growAnimator(View view, long duration)
  • DefaultAnimations.shrinkAnimator(View view, long duration)
  • DefaultAnimations.fadeAwayAnimator(View view, long duration)
  • DefaultAnimations.fadeInAnimator(View view, long duration)
  • DefaultAnimations.spinAnimator(View view, long duration)

Experiment which ones work for you! If you think of anymore feel free to add them to the library yourself!


Example Code

private class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

       List<RelativeLayout> placeholderList;

       class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

           RelativeLayout placeholderView;

           ViewHolder(View itemView) {
               placeholderView = (RelativeLayout) itemView.findViewById(;

           public void onClick(View v) {
               if (spruceAnimator != null) {

       RecyclerAdapter(List<RelativeLayout> placeholderList) {
           this.placeholderList = placeholderList;

       public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
           RelativeLayout view = (RelativeLayout) LayoutInflater.from(parent.getContext())
                   .inflate(R.layout.recycler_placeholder, parent, false);

           return new ViewHolder(view);

       public void onBindViewHolder(ViewHolder holder, int position) {
           holder.placeholderView = placeholderList.get(position);

       public int getItemCount() {
           return placeholderList.size();



Example App

Use the example app to find the right SortFunction. In the app you will be able to see the affects of each SortFunction.

Contributing to Spruce

Contributions are more than welcome! Please see the Contributing Guidelines and be mindful of our Code of Conduct.

Issues or Future Ideas

If part of Spruce is not working correctly be sure to file a Github issue. In the issue provide as many details as possible. This could include example code or the exact steps that you did so that everyone can reproduce the issue. Sample projects are always the best way :). This makes it easy for our developers or someone from the open-source community to start working!

If you have a feature idea submit an issue with a feature request or submit a pull request and we will work with you to merge it in!


By Ponglang Petrung

Administrator and PJ at Kamibit Thailand, Android Developer at CodeGears Co., Ltd. and Android Developer, and iOS Application Developer at Appdever

Share your thoughts

Leave a Reply

Loading Facebook Comments ...
Loading Disqus Comments ...