• Stars
    star
    3,716
  • Rank 11,882 (Top 0.3 %)
  • Language
    Java
  • License
    MIT License
  • Created almost 8 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Spruce Animation Library

Spruce Logo

Spruce Android Animation Library (and iOS)

CircleCI Build Status License MIT Public Yes

What is it?

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.

Gradle

Add the following to your project's root build.gradle file

repositories {
	maven {
		url  "https://dl.bintray.com/bfears/maven"
	}
}

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

dependencies {
    implementation 'com.willowtreeapps.spruce:spruce-android:1.1.0'
}

Documentation

For javadocs checkout the documentation for more information.

Basic Usage

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

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
        .SpruceBuilder(parentViewGroup)
        .sortWith(new DefaultSort(/*interObjectDelay=*/50L))
        .animateWith(animators)
        .start();

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
        .SpruceBuilder(parentViewGroup)
        .sortWith(new LinearSort(/*interObjectDelay=*/100L, /*reversed=*/false, LinearSort.Direction.TOP_TO_BOTTOM))
        .animateWith(DefaultAnimations.shrinkAnimator(parentViewGroup, /*duration=*/800))
        .start();

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 https://developer.android.com/reference/android/animation/Animator.html

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 SortFunction classes 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

View Exclusion Feature

Spruce Animate all the views inside the view group. One of the key tips for pulling the best performance out of an Android app is to maintain a flat hierarchy. Spruce is now Introducing a new Exclusion feature.
This work in 2 modes:

  • NORMAL_MODE: This mode should be used when you have view groups like Constraint/Frame/Relative/Linear Layouts. We feed a list of ids to be excluded to the SpruceBuilder.
  • R_L_MODE: This mode is used when we have ListView/RecyclerView. The only difference with the first mode is that we pass in the positions to be excluded instead of Ids.
Animator spruceAnimator = new Spruce
        .SpruceBuilder(parentViewGroup)
        .sortWith(new LinearSort(/*interObjectDelay=*/100L, /*reversed=*/false, LinearSort.Direction.TOP_TO_BOTTOM))
        .excludeViews(getExcludedViewIds(), NORMAL_MODE)
        //or 
       .excludeViews(getExcludedViewPosition(), R_L_MODE)
        .start();

Sort Function Interpolators

Spruce now allows the user to control the overall flow of sort function using Interpolators.

Animator spruceAnimator = new Spruce
        .SpruceBuilder(parentViewGroup)
        .sortWith(new LinearSort(/*interObjectDelay=*/100L, /*reversed=*/false, LinearSort.Direction.TOP_TO_BOTTOM))
        .addInterpolator(new LinearInterpolator())
        .start();

Spruce gives you a wide variety of stock interpolators to choose from.

  • SpruceInterpolators.EASE
  • SpruceInterpolators.EASE_IN
  • SpruceInterpolators.EASE_OUT
  • SpruceInterpolators.EASE_IN_OUT
  • SpruceInterpolators.EASE_IN_QUAD
  • SpruceInterpolators.EASE_IN_CUBIC
  • SpruceInterpolators.EASE_IN_QUART
  • SpruceInterpolators.EASE_IN_QUINT
  • SpruceInterpolators.EASE_IN_SINE
  • SpruceInterpolators.EASE_IN_EXPO
  • SpruceInterpolators.EASE_IN_CIRC
  • SpruceInterpolators.EASE_IN_BACK
  • SpruceInterpolators.EASE_OUT_QUAD
  • SpruceInterpolators.EASE_OUT_CUBIC
  • SpruceInterpolators.EASE_OUT_QUART
  • SpruceInterpolators.EASE_OUT_QUINT
  • SpruceInterpolators.EASE_OUT_SINE
  • SpruceInterpolators.EASE_OUT_EXPO
  • SpruceInterpolators.EASE_OUT_CIRC
  • SpruceInterpolators.EASE_OUT_BACK
  • SpruceInterpolators.EASE_IN_OUT_QUAD
  • SpruceInterpolators.EASE_IN_OUT_CUBIC
  • SpruceInterpolators.EASE_IN_OUT_QUART
  • SpruceInterpolators.EASE_IN_OUT_QUINT
  • SpruceInterpolators.EASE_IN_OUT_SINE
  • SpruceInterpolators.EASE_IN_OUT_EXPO
  • SpruceInterpolators.EASE_IN_OUT_CIRC
  • SpruceInterpolators.EASE_IN_OUT_BACK

Checkout interpolator documentation for more information.

Spruce Dynamics

Spruce now supports Dynamic Animations. Spruce Dynamics is an extension of the androidx dynamic animations.

These are the option that SpruceDynamics exposes to the developers:

  • Allows start delay for dynamic animations
  • Animation Property is now exposed (developers can set progress of the animations dynamically)

You can create your own Spring/Fling animations from SpruceDynamics and add them to the '.animateWith' function for playing the animations in the respective ViewGroup

Animator spruceAnimator = new Spruce
        .SpruceBuilder(parentViewGroup)
        .sortWith(new LinearSort(/*interObjectDelay=*/100L, /*reversed=*/false, LinearSort.Direction.TOP_TO_BOTTOM))
        .animateWith(DefaultAnimations.dynamicTranslationUpwards(parent))
        .start();

Above all these, With spruce, you can implement a combination of both Android Animations and Spruce Dynamics at the same time.

animators = new Object[]{
        DefaultAnimations.dynamicTranslationUpwards(parent),
        DefaultAnimations.dynamicFadeIn(parent),
        DefaultAnimations.shrinkAnimator(parent,800)
};


Animator spruceAnimator = new Spruce
        .SpruceBuilder(parentViewGroup)
        .sortWith(new LinearSort(/*interObjectDelay=*/100L, /*reversed=*/false, LinearSort.Direction.TOP_TO_BOTTOM))
        .animateWith(animators)
        .start();

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)
  • DefaultAnimations.dynamicTranslationUpwards(View view)
  • DefaultAnimations.dynamicFadeIn(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 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!

Third Party Bindings

React Native

You may now use this library with React Native via the module here

About WillowTree!

WillowTree Logo

We build apps, responsive sites, botsβ€”any digital product that lives on a screenβ€”for the world’s leading companies. Our elite teams challenge themselves to build extraordinary experiences by bridging the latest strategy and design thinking with enterprise-grade software development.

Interested in working on more unique projects like Spruce? Check out our careers page.

More Repositories

1

spruce-ios

Swift library for choreographing animations on the screen.
Swift
3,438
star
2

Hyperion-iOS

In-app design review tool to inspect measurements, attributes, and animations.
Objective-C
2,047
star
3

Hyperion-Android

App Debugging & Inspection Tool for Android
Java
1,946
star
4

assertk

assertions for kotlin inspired by assertj
Kotlin
753
star
5

sign-in-with-apple-button-android

An Android library for Sign In with Apple
Kotlin
280
star
6

vocable-android

Vocable for Android
Kotlin
115
star
7

OAK

Library to address common hurdles in Android development
Java
110
star
8

android-instant-apps-demo

Java
108
star
9

fuzzywuzzy-kotlin

Fuzzy string matching for Kotlin (JVM, native, JS, Web Assembly) - port of Fuzzy Wuzzy Python lib
Kotlin
78
star
10

vocable-ios

Vocable AAC for iOS - Empowering people to communicate with care takers and loved ones.
Swift
78
star
11

cordux

Swift
61
star
12

scratch

Easy app data clearing and relaunching
Java
57
star
13

ukor

A Roku build tool with support for build flavors
Brightscript
49
star
14

ouroboros

Infinitely scrolling carousel for tvOS
Swift
48
star
15

wist

A linter for BrightScript
C++
43
star
16

rocute

beautiful ui components for roku development
Brightscript
37
star
17

rootx

Wrap sqlx in even more convenience
Go
37
star
18

PinkyPromise

A tiny Promises library.
Swift
33
star
19

trafficcop

Monitor your Android apps' data usage so you can take action if it's over a threshold.
Java
27
star
20

BlurredVideo-iOS

Applying a blur to a HTTP live stream
Swift
27
star
21

SimpleRecyclerViewDemo

A simple Android application demonstrating the RecyclerView/Adapter/ViewHolder pattern.
Java
26
star
22

react-formable

React Forms
TypeScript
25
star
23

lottie-player

A mac based player for Lottie
Swift
22
star
24

opentest4k

kotlin multiplatform implementation/bindings of opentest4j
Kotlin
20
star
25

android-svg-test

Android SVG sample project to demonstrate OAK's AnimatedSvgView
Java
17
star
26

hello-shared-kotlin

Shared kotlin lib between android and ios
Kotlin
16
star
27

WillowTreeScrollingTabController

Tab based container implementation for iOS written in Swift
Swift
12
star
28

catalyst-slack-service

Unconscious gender bias has been fueling the gender gap for far too long. We’re releasing the #BiasCorrect code in hopes that coders around the world will adapt it for whatever chat-based platforms they use in order to give more people access to this tool for change.
Java
12
star
29

tablediff

Swift
11
star
30

hinoki

A Language Server Protocol implementation for BrightScript
TypeScript
10
star
31

rokul-runnings

Roku Automation, written in TypeScript
TypeScript
10
star
32

wombats-api

Wombats API
Clojure
10
star
33

grove

Swift
8
star
34

WillowTreeReachability

Simple Swift class for monitoring network reachability.
Swift
6
star
35

fastlane-plugin-msbuild

MSBuild / Xamarin actions for Fastlane
Ruby
6
star
36

conductor-mobile

Conductor Mobile is a port of the Conductor Web Framework for iOS and Android
Java
6
star
37

dockertestapp

basic android app for usage in docker
Kotlin
5
star
38

vscode-ide-brightscript

Brightscript support for vscode
TypeScript
5
star
39

Sweetgum

A simple REST client built in C# using Avalonia
C#
5
star
40

palette-vs-the-dress

Java
4
star
41

wombats-web-client

Web Client for Wombats
Clojure
4
star
42

catalyst-bias-correct-service

A bias correction service
Java
4
star
43

acorn

Provides helper functionality for writing code generators (specifically for Go, in Go)
Go
4
star
44

bark

Swift
4
star
45

namegame_ios

The Name Game for iOS
3
star
46

wat-test-project

A bare skeleton for prospective WAT members to pick at.
3
star
47

xambuild

Build Xamarin projects outside of Visual Studio!
Python
3
star
48

intro-to-core-ml

An introduction to Core ML.
Swift
3
star
49

ios-google-home-demo

Swift
3
star
50

IntroiOSExamples

A group of examples that are used for the WillowTree Intro to iOS Course
Swift
3
star
51

atom-ide-brightscript

TypeScript
3
star
52

iot-android-things-robot-arm

Kotlin
2
star
53

namegame_android

2
star
54

Sweetgum.Client

Sweetgum assists in the development and testing of APIs and applications that utilize APIs.
TypeScript
2
star
55

AXComponentKit

Experimental framework for more reliable iOS automation tests
Swift
2
star
56

capwic_event_info

Swift
1
star
57

slacksnax-server

JavaScript
1
star
58

friday-shots

Friday Shots rules, game runner, and results data
Python
1
star
59

battlebots-swift-front-end

A swift framework for building a BattleBots front-end.
Swift
1
star
60

smart-speaker-detector-sample-android

Finds an assortment of smart speakers
Kotlin
1
star
61

android-bootcamp

Sample app for Android bootcamp and associated issue tracking
Java
1
star
62

wombats-ios

iOS Frontend client for Wombats
Swift
1
star
63

namegame-cs

Name Game skeleton project for C#
1
star
64

slacksnax-slackCommand

JavaScript
1
star
65

wta-lighthouse-logger

JavaScript
1
star
66

code-analysis-web

HTML
1
star
67

maple-mosaic

Our in-office Lego wall - scripts for building out the images and printouts.
JavaScript
1
star
68

wombats-documentation

Wombat's API Documentation
JavaScript
1
star