Last Updated: September 27, 2021
·
9.778K
· dmytrodanylyk

Android Animations – Code Snippets

View Animations API 1+

View animations are used when you want to modified where the View was drawn, and not the actual View itself.

Picture

public void alpha(View view) {
    AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
    alphaAnimation.setDuration(1000); // 1 second
    view.startAnimation(alphaAnimation);
}

public void rotate(View view) {
    RotateAnimation rotateAnimation = new RotateAnimation(90, 180);
    rotateAnimation.setDuration(1000); // 1 second
    view.startAnimation(rotateAnimation);
}

public void scale(View view) {
    ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
    scaleAnimation.setDuration(1000); // 1 second
    view.startAnimation(scaleAnimation);
}

public void translate(View view) {
    TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f);
    translateAnimation.setDuration(1000); // 1 second
    view.startAnimation(translateAnimation);
}

public void animationSet(View view) {
    ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
    scaleAnimation.setDuration(1000); // 1 second

    AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
    alphaAnimation.setDuration(1000); // 1 second

    AnimationSet animationSet = new AnimationSet(true);
    animationSet.addAnimation(scaleAnimation);
    animationSet.addAnimation(alphaAnimation);

    view.startAnimation(animationSet);
}

If you want animation to apply its transformation after it ends use following code.

Animation.setFillAfter(true);

Object Animator API 11+

The object animator system can animate Views on the screen by changing the actual properties in the View objects.

public void rotate(View view) {

PropertyValuesHolder rotateY = PropertyValuesHolder.ofFloat(View.ROTATION_Y, 0, 90);
PropertyValuesHolder rotateX = PropertyValuesHolder.ofFloat(View.ROTATION_X, 0, 90);

ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, rotateX, rotateY);
animator.setDuration(1000); // 1 second
animator.start();

}

public void alpha(View view) {

    PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat(View.ALPHA, 0, 1);

    ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, alpha);
    animator.setDuration(1000); // 1 second
    animator.start();
}

public void scale(View view) {

    PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 0, 1);
    PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat(View.SCALE_X, 0, 1);

    ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, scaleY, scaleX);
    animator.setDuration(1000); // 1 second
    animator.start();
}

public void translate(View view) {

    PropertyValuesHolder translateX = PropertyValuesHolder.ofFloat(View.TRANSLATION_X, 0, 100);
    PropertyValuesHolder translateY = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, 0, 100);

    ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, translateX, translateY);
    animator.setDuration(1000); // 1 second
    animator.start();
}

Property Animator API 12+

Behaves much like an Object Animator, because it modifies the actual values of the view’s properties, but code is more simple and is more efficient when animating many properties at once.

public void rotate(View view) {
    view.animate().rotationX(90).rotationY(90).start();
}

public void alpha(View view) {
    view.animate().alpha(0).start();
}

public void scale(View view) {
    view.animate().scaleX(0).scaleY(0).start();
}

public void translate(View view) {
    view.animate().translationX(100).translationY(100).start;
}

Interpolator

To make animation not linear you can use Interpolator. You can check demonstration video of various effect of Interpolator here.

Picture