mpao
11/29/2017 - 4:54 PM

CircleReveal for fragment, nice with FAB

CircleReveal for fragment, nice with FAB

package io.github.mpao.streetmemo.ui.animations;

import android.animation.Animator;
import android.os.Build;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.view.View;
import android.view.ViewAnimationUtils;

import static android.support.v4.app.FragmentManager.POP_BACK_STACK_INCLUSIVE;

/**
 * https://developer.android.com/training/material/animations.html#Reveal
 */
public class CircleReveal {

    private static final int DURATION = 400;

    public static void enterAnimation(final View targetView, final View starterView){

        // solo da Lollipop
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            // quando cambia il layout...
            targetView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    v.removeOnLayoutChangeListener(this);

                    int[] location = new int[2];
                    starterView.getLocationOnScreen(location);
                    int x = location[0] + starterView.getWidth()/2;
                    int radius = (int) Math.hypot(right, bottom);

                    Animator anim = ViewAnimationUtils.createCircularReveal(v, x, targetView.getHeight(), 0, radius);
                    anim.setInterpolator(new FastOutSlowInInterpolator());
                    anim.setDuration(DURATION);
                    anim.start();
                }

            });

        }

    }

    public static void exitAnimation(final View targetView, final View starterView, final FragmentManager fm){

        // solo da Lollipop
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            int[] location = new int[2];
            starterView.getLocationOnScreen(location);
            int x = location[0] + starterView.getWidth()/2;
            int width  = targetView.getWidth();
            int height = targetView.getHeight();
            float radius = (float) Math.sqrt(width * width + height * height);

            Animator anim = ViewAnimationUtils.createCircularReveal(targetView, x, targetView.getHeight(), radius, 0);
            anim.setInterpolator(new FastOutSlowInInterpolator());
            anim.setDuration(DURATION);
            anim.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animator) {}

                @Override
                public void onAnimationEnd(Animator animator) {
                    targetView.setVisibility(View.INVISIBLE); // resolve flashing
                    fm.popBackStack("list", POP_BACK_STACK_INCLUSIVE);
                }

                @Override
                public void onAnimationCancel(Animator animator) {}

                @Override
                public void onAnimationRepeat(Animator animator) {}
            });
            anim.start();

        }

    }

}