by.gdgminsk.animationguide.util.AnimUtils.java Source code

Java tutorial

Introduction

Here is the source code for by.gdgminsk.animationguide.util.AnimUtils.java

Source

/*
 * Copyright 2015 GDG Minsk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package by.gdgminsk.animationguide.util;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.os.Build;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.animation.PathInterpolatorCompat;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Interpolator;

import java.util.ArrayList;
import java.util.List;

import by.gdgminsk.animationguide.R;

public final class AnimUtils {
    public static final Interpolator EASE_IN_INTERPOLATOR = PathInterpolatorCompat.create(0.0f, 0.0f, 0.2f, 1.0f);
    public static final Interpolator EASE_OUT_INTERPOLATOR = PathInterpolatorCompat.create(0.4f, 0.0f, 1.0f, 1.0f);

    private AnimUtils() {
    }

    @SuppressWarnings("unchecked")
    public static ActivityOptionsCompat makeSharedViewOptions(Activity activity, View... views) {
        List<Pair<View, String>> sharedPairs = new ArrayList<>();
        for (View view : views) {
            sharedPairs.add(new Pair<>(view, ViewCompat.getTransitionName(view)));
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // always add status bar and navigation bar to shared element transition to play shared views animation
            // without UI glitch described here https://plus.google.com/+AlexLockwood/posts/RPtwZ5nNebb
            View decor = activity.getWindow().getDecorView();
            View statusBar = decor.findViewById(android.R.id.statusBarBackground);
            if (statusBar != null) {
                sharedPairs.add(new Pair<>(statusBar, ViewCompat.getTransitionName(statusBar)));
            }
            View navBar = decor.findViewById(android.R.id.navigationBarBackground);
            if (navBar != null) {
                sharedPairs.add(new Pair<>(navBar, ViewCompat.getTransitionName(navBar)));
            }
        }

        return ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
                sharedPairs.toArray(new Pair[sharedPairs.size()]));
    }

    public static void scaleIn(final View view, int delay) {
        view.setAlpha(0.0f);
        view.setScaleX(0.0f);
        view.setScaleY(0.0f);
        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
        ObjectAnimator scaleInAnimation = ObjectAnimator.ofPropertyValuesHolder(view, alpha, scaleX, scaleY);
        scaleInAnimation.setDuration(view.getResources().getInteger(R.integer.duration_fab_scale_in));
        scaleInAnimation.setStartDelay(delay);
        scaleInAnimation.setInterpolator(EASE_IN_INTERPOLATOR);
        scaleInAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                view.setAlpha(1.0f);
                view.setScaleX(1.0f);
                view.setScaleY(1.0f);
            }
        });
        scaleInAnimation.start();
    }

    public static void scaleOut(final View view, int delay) {
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.0f);
        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.0f);
        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0.4f);
        ObjectAnimator scaleOutAnimation = ObjectAnimator.ofPropertyValuesHolder(view, alpha, scaleX, scaleY);
        scaleOutAnimation.setInterpolator(EASE_OUT_INTERPOLATOR);
        scaleOutAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                view.setScaleX(0.0f);
                view.setScaleY(0.0f);
                view.setVisibility(View.GONE);
            }
        });
        scaleOutAnimation.setDuration(view.getResources().getInteger(R.integer.duration_fab_scale_out));
        scaleOutAnimation.setStartDelay(delay);

        scaleOutAnimation.start();
    }

    public static class AnimationListenerAdapter implements Animation.AnimationListener {

        @Override
        public void onAnimationStart(Animation animation) {
            // NO-OP
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // NO-OP
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // NO-OP
        }
    }
}