Install ViewAnimator
masterAdd the following dependency to your build.gradle file to use ViewAnimator in your Android project.
compile 'com.github.florent37:viewanimator:1.1.0'repository·master·Indexed 25 days ago
https://github.com/florent37/viewanimatorA fluent Android animation library for chaining complex view animations and transitions. It supports DP values, height/width animations, color and rotation changes, custom animation logic, and pre-built enhanced animations like shake, bounceIn, and pulse.
Add the following dependency to your build.gradle file to use ViewAnimator in your Android project.
compile 'com.github.florent37:viewanimator:1.1.0'ViewAnimator allows you to chain animations for multiple views. You can use .animate() to start a sequence, .andAnimate() to add a subsequent animation for a different view, and .thenAnimate() to chain animations for the original view after the sequence. Use .start() to execute the animation set.
ViewAnimator
.animate(image)
.translationY(-1000, 0)
.alpha(0,1)
.andAnimate(text)
.dp().translationX(-20, 0)
.decelerate()
.duration(2000)
.thenAnimate(image)
.scale(1f, 0.5f, 1f)
.accelerate()
.duration(1000)
.start();Use the .custom() method to define a custom animation logic by implementing the AnimationListener.Update<T> interface. The update method provides the view and a float value (typically between 0 and 1) to drive the animation.
ViewAnimator.animate(text)
.custom(new AnimationListener.Update<TextView>() {
@Override public void update(TextView view, float value) {
view.setText(String.format("%.02f", value));
}
}, 0, 1)
.start();To ensure animations use density-independent pixels (DP) instead of raw pixels, call the .dp() method before specifying the property animation.
ViewAnimator
.animate(image)
.dp().translationY(-200, 0)
.start();To cancel an animation, capture the ViewAnimator instance returned by the .start() method and call .cancel() on it.
ViewAnimator viewAnimator = ViewAnimator
.animate(view)
.rotation(360)
.start();
viewAnimator.cancel();To animate height or width, use .waitForHeight() to wait until a ViewTreeObserver notification is received, then use .dp().width() or .dp().height() to define the animation range.
ViewAnimator
.animate(view)
.waitForHeight()
.dp().width(100,200)
.dp().height(50,100)
.start();You can attach lifecycle listeners to your animation sequence using .onStart(Runnable) and .onStop(Runnable).
ViewAnimator
.animate(image)
.scale(0,1)
.onStart(() -> { /* logic */ })
.onStop(() -> { /* logic */ })
.start();ViewAnimator includes several pre-built enhanced animations and support for Path animations:
Enhanced Animations:
.shake().bounceIn().flash().repeatCount(n).flipHorizontal().wave().tada().pulse().standUp().swing().wobble()Path Animations:
Pass a android.graphics.Path object to the .path(path) method.
ViewAnimator provides direct methods for color and rotation animations:
.textColor(startColor, endColor).backgroundColor(startColor, endColor).rotation(degrees)