Example usage for android.view View getOverlay

List of usage examples for android.view View getOverlay

Introduction

In this page you can find the example usage for android.view View getOverlay.

Prototype

public ViewOverlay getOverlay() 

Source Link

Document

Returns the overlay for this view, creating it if it does not yet exist.

Usage

From source file:com.bachhuberdesign.deckbuildergwent.util.FabTransform.java

@Override
public Animator createAnimator(final ViewGroup sceneRoot, final TransitionValues startValues,
        final TransitionValues endValues) {
    if (startValues == null || endValues == null)
        return null;

    final Rect startBounds = (Rect) startValues.values.get(PROP_BOUNDS);
    final Rect endBounds = (Rect) endValues.values.get(PROP_BOUNDS);

    final boolean fromFab = endBounds.width() > startBounds.width();
    final View view = endValues.view;
    final Rect dialogBounds = fromFab ? endBounds : startBounds;
    final Interpolator fastOutSlowInInterpolator = AnimUtils.getFastOutSlowInInterpolator();
    final long duration = getDuration();
    final long halfDuration = duration / 2;
    final long twoThirdsDuration = duration * 2 / 3;

    if (!fromFab) {
        // Force measure / layout the dialog back to it's original bounds
        view.measure(makeMeasureSpec(startBounds.width(), View.MeasureSpec.EXACTLY),
                makeMeasureSpec(startBounds.height(), View.MeasureSpec.EXACTLY));
        view.layout(startBounds.left, startBounds.top, startBounds.right, startBounds.bottom);
    }/*from  w  w w .j  a  v a  2  s.  c  o m*/

    final int translationX = startBounds.centerX() - endBounds.centerX();
    final int translationY = startBounds.centerY() - endBounds.centerY();
    if (fromFab) {
        view.setTranslationX(translationX);
        view.setTranslationY(translationY);
    }

    // Add a color overlay to fake appearance of the FAB
    final ColorDrawable fabColor = new ColorDrawable(color);
    fabColor.setBounds(0, 0, dialogBounds.width(), dialogBounds.height());
    if (!fromFab)
        fabColor.setAlpha(0);
    view.getOverlay().add(fabColor);

    // Add an icon overlay again to fake the appearance of the FAB
    final Drawable fabIcon = ContextCompat.getDrawable(sceneRoot.getContext(), icon).mutate();
    final int iconLeft = (dialogBounds.width() - fabIcon.getIntrinsicWidth()) / 2;
    final int iconTop = (dialogBounds.height() - fabIcon.getIntrinsicHeight()) / 2;
    fabIcon.setBounds(iconLeft, iconTop, iconLeft + fabIcon.getIntrinsicWidth(),
            iconTop + fabIcon.getIntrinsicHeight());
    if (!fromFab)
        fabIcon.setAlpha(0);
    view.getOverlay().add(fabIcon);

    // Since the view that's being transition to always seems to be on the top (z-order), we have
    // to make a copy of the "from" view and put it in the "to" view's overlay, then fade it out.
    // There has to be another way to do this, right?
    Drawable dialogView = null;
    if (!fromFab) {
        startValues.view.setDrawingCacheEnabled(true);
        startValues.view.buildDrawingCache();
        Bitmap viewBitmap = startValues.view.getDrawingCache();
        dialogView = new BitmapDrawable(view.getResources(), viewBitmap);
        dialogView.setBounds(0, 0, dialogBounds.width(), dialogBounds.height());
        view.getOverlay().add(dialogView);
    }

    // Circular clip from/to the FAB size
    final Animator circularReveal;
    if (fromFab) {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, startBounds.width() / 2,
                (float) Math.hypot(endBounds.width() / 2, endBounds.height() / 2));
        circularReveal.setInterpolator(AnimUtils.getFastOutLinearInInterpolator());
    } else {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, (float) Math.hypot(startBounds.width() / 2, startBounds.height() / 2),
                endBounds.width() / 2);
        circularReveal.setInterpolator(AnimUtils.getLinearOutSlowInInterpolator());

        // Persist the end clip i.e. stay at FAB size after the reveal has run
        circularReveal.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                final ViewOutlineProvider fabOutlineProvider = view.getOutlineProvider();

                view.setOutlineProvider(new ViewOutlineProvider() {
                    boolean hasRun = false;

                    @Override
                    public void getOutline(final View view, Outline outline) {
                        final int left = (view.getWidth() - endBounds.width()) / 2;
                        final int top = (view.getHeight() - endBounds.height()) / 2;

                        outline.setOval(left, top, left + endBounds.width(), top + endBounds.height());

                        if (!hasRun) {
                            hasRun = true;
                            view.setClipToOutline(true);

                            // We have to remove this as soon as it's laid out so we can get the shadow back
                            view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
                                @Override
                                public boolean onPreDraw() {
                                    if (view.getWidth() == endBounds.width()
                                            && view.getHeight() == endBounds.height()) {
                                        view.setOutlineProvider(fabOutlineProvider);
                                        view.setClipToOutline(false);
                                        view.getViewTreeObserver().removeOnPreDrawListener(this);
                                        return true;
                                    }

                                    return true;
                                }
                            });
                        }
                    }
                });
            }
        });
    }
    circularReveal.setDuration(duration);

    // Translate to end position along an arc
    final Animator translate = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, View.TRANSLATION_Y,
            fromFab ? getPathMotion().getPath(translationX, translationY, 0, 0)
                    : getPathMotion().getPath(0, 0, -translationX, -translationY));
    translate.setDuration(duration);
    translate.setInterpolator(fastOutSlowInInterpolator);

    // Fade contents of non-FAB view in/out
    List<Animator> fadeContents = null;
    if (view instanceof ViewGroup) {
        final ViewGroup vg = ((ViewGroup) view);
        fadeContents = new ArrayList<>(vg.getChildCount());
        for (int i = vg.getChildCount() - 1; i >= 0; i--) {
            final View child = vg.getChildAt(i);
            final Animator fade = ObjectAnimator.ofFloat(child, View.ALPHA, fromFab ? 1f : 0f);
            if (fromFab) {
                child.setAlpha(0f);
            }
            fade.setDuration(twoThirdsDuration);
            fade.setInterpolator(fastOutSlowInInterpolator);
            fadeContents.add(fade);
        }
    }

    // Fade in/out the fab color & icon overlays
    final Animator colorFade = ObjectAnimator.ofInt(fabColor, "alpha", fromFab ? 0 : 255);
    final Animator iconFade = ObjectAnimator.ofInt(fabIcon, "alpha", fromFab ? 0 : 255);
    if (!fromFab) {
        colorFade.setStartDelay(halfDuration);
        iconFade.setStartDelay(halfDuration);
    }
    colorFade.setDuration(halfDuration);
    iconFade.setDuration(halfDuration);
    colorFade.setInterpolator(fastOutSlowInInterpolator);
    iconFade.setInterpolator(fastOutSlowInInterpolator);

    // Run all animations together
    final AnimatorSet transition = new AnimatorSet();
    transition.playTogether(circularReveal, translate, colorFade, iconFade);
    transition.playTogether(fadeContents);
    if (dialogView != null) {
        final Animator dialogViewFade = ObjectAnimator.ofInt(dialogView, "alpha", 0)
                .setDuration(twoThirdsDuration);
        dialogViewFade.setInterpolator(fastOutSlowInInterpolator);
        transition.playTogether(dialogViewFade);
    }
    transition.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            // Clean up
            view.getOverlay().clear();

            if (!fromFab) {
                view.setTranslationX(0);
                view.setTranslationY(0);
                view.setTranslationZ(0);

                view.measure(makeMeasureSpec(endBounds.width(), View.MeasureSpec.EXACTLY),
                        makeMeasureSpec(endBounds.height(), View.MeasureSpec.EXACTLY));
                view.layout(endBounds.left, endBounds.top, endBounds.right, endBounds.bottom);
            }

        }
    });
    return new AnimUtils.NoPauseAnimator(transition);
}

From source file:com.github.takahirom.plaidanimation.transition.FabTransform.java

@Override
public Animator createAnimator(final ViewGroup sceneRoot, final TransitionValues startValues,
        final TransitionValues endValues) {
    if (startValues == null || endValues == null)
        return null;

    final Rect startBounds = (Rect) startValues.values.get(PROP_BOUNDS);
    final Rect endBounds = (Rect) endValues.values.get(PROP_BOUNDS);

    final boolean fromFab = endBounds.width() > startBounds.width();
    final View view = endValues.view;
    final Rect dialogBounds = fromFab ? endBounds : startBounds;
    final Rect fabBounds = fromFab ? startBounds : endBounds;
    final Interpolator fastOutSlowInInterpolator = new FastOutSlowInInterpolator();
    final long duration = getDuration();
    final long halfDuration = duration / 2;
    final long twoThirdsDuration = duration * 2 / 3;

    if (!fromFab) {
        // Force measure / layout the dialog back to it's original bounds
        view.measure(makeMeasureSpec(startBounds.width(), View.MeasureSpec.EXACTLY),
                makeMeasureSpec(startBounds.height(), View.MeasureSpec.EXACTLY));
        view.layout(startBounds.left, startBounds.top, startBounds.right, startBounds.bottom);
    }// ww w. j  ava2s.c  o  m

    final int translationX = startBounds.centerX() - endBounds.centerX();
    final int translationY = startBounds.centerY() - endBounds.centerY();
    if (fromFab) {
        view.setTranslationX(translationX);
        view.setTranslationY(translationY);
    }

    // Add a color overlay to fake appearance of the FAB
    final ColorDrawable fabColor = new ColorDrawable(color);
    fabColor.setBounds(0, 0, dialogBounds.width(), dialogBounds.height());
    if (!fromFab)
        fabColor.setAlpha(0);
    view.getOverlay().add(fabColor);

    // Add an icon overlay again to fake the appearance of the FAB
    final Drawable fabIcon = ContextCompat.getDrawable(sceneRoot.getContext(), icon).mutate();
    final int iconLeft = (dialogBounds.width() - fabIcon.getIntrinsicWidth()) / 2;
    final int iconTop = (dialogBounds.height() - fabIcon.getIntrinsicHeight()) / 2;
    fabIcon.setBounds(iconLeft, iconTop, iconLeft + fabIcon.getIntrinsicWidth(),
            iconTop + fabIcon.getIntrinsicHeight());
    if (!fromFab)
        fabIcon.setAlpha(0);
    view.getOverlay().add(fabIcon);

    // Circular clip from/to the FAB size
    final Animator circularReveal;
    if (fromFab) {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, startBounds.width() / 2,
                (float) Math.hypot(endBounds.width() / 2, endBounds.height() / 2));
        circularReveal.setInterpolator(new FastOutLinearInInterpolator());
    } else {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, (float) Math.hypot(startBounds.width() / 2, startBounds.height() / 2),
                endBounds.width() / 2);
        circularReveal.setInterpolator(new LinearOutSlowInInterpolator());

        // Persist the end clip i.e. stay at FAB size after the reveal has run
        circularReveal.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setOutlineProvider(new ViewOutlineProvider() {
                    @Override
                    public void getOutline(View view, Outline outline) {
                        final int left = (view.getWidth() - fabBounds.width()) / 2;
                        final int top = (view.getHeight() - fabBounds.height()) / 2;
                        outline.setOval(left, top, left + fabBounds.width(), top + fabBounds.height());
                        view.setClipToOutline(true);
                    }
                });
            }
        });
    }
    circularReveal.setDuration(duration);

    // Translate to end position along an arc
    final Animator translate = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, View.TRANSLATION_Y,
            fromFab ? getPathMotion().getPath(translationX, translationY, 0, 0)
                    : getPathMotion().getPath(0, 0, -translationX, -translationY));
    translate.setDuration(duration);
    translate.setInterpolator(fastOutSlowInInterpolator);

    // Fade contents of non-FAB view in/out
    List<Animator> fadeContents = null;
    if (view instanceof ViewGroup) {
        final ViewGroup vg = ((ViewGroup) view);
        fadeContents = new ArrayList<>(vg.getChildCount());
        for (int i = vg.getChildCount() - 1; i >= 0; i--) {
            final View child = vg.getChildAt(i);
            final Animator fade = ObjectAnimator.ofFloat(child, View.ALPHA, fromFab ? 1f : 0f);
            if (fromFab) {
                child.setAlpha(0f);
            }
            fade.setDuration(twoThirdsDuration);
            fade.setInterpolator(fastOutSlowInInterpolator);
            fadeContents.add(fade);
        }
    }

    // Fade in/out the fab color & icon overlays
    final Animator colorFade = ObjectAnimator.ofInt(fabColor, "alpha", fromFab ? 0 : 255);
    final Animator iconFade = ObjectAnimator.ofInt(fabIcon, "alpha", fromFab ? 0 : 255);
    if (!fromFab) {
        colorFade.setStartDelay(halfDuration);
        iconFade.setStartDelay(halfDuration);
    }
    colorFade.setDuration(halfDuration);
    iconFade.setDuration(halfDuration);
    colorFade.setInterpolator(fastOutSlowInInterpolator);
    iconFade.setInterpolator(fastOutSlowInInterpolator);

    // Work around issue with elevation shadows. At the end of the return transition the shared
    // element's shadow is drawn twice (by each activity) which is jarring. This workaround
    // still causes the shadow to snap, but it's better than seeing it double drawn.
    Animator elevation = null;
    if (!fromFab) {
        elevation = ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, -view.getElevation());
        elevation.setDuration(duration);
        elevation.setInterpolator(fastOutSlowInInterpolator);
    }

    // Run all animations together
    final AnimatorSet transition = new AnimatorSet();
    transition.playTogether(circularReveal, translate, colorFade, iconFade);
    transition.playTogether(fadeContents);
    if (elevation != null)
        transition.play(elevation);
    if (fromFab) {
        transition.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // Clean up
                view.getOverlay().clear();
            }
        });
    }
    return new AnimUtils.NoPauseAnimator(transition);
}

From source file:de.dreier.mytargets.utils.transitions.FabTransform.java

@Override
public Animator createAnimator(@NonNull final ViewGroup sceneRoot, final TransitionValues startValues,
        final TransitionValues endValues) {
    if (startValues == null || endValues == null) {
        return null;
    }/*ww  w.  j a v a2s  .c om*/

    final Rect startBounds = (Rect) startValues.values.get(PROP_BOUNDS);
    final Rect endBounds = (Rect) endValues.values.get(PROP_BOUNDS);

    final boolean fromFab = endBounds.width() > startBounds.width();
    final View view = endValues.view;
    final Rect dialogBounds = fromFab ? endBounds : startBounds;
    final Rect fabBounds = fromFab ? startBounds : endBounds;
    final Interpolator fastOutSlowInInterpolator = new FastOutSlowInInterpolator();
    final long duration = getDuration();
    final long halfDuration = duration / 2;
    final long twoThirdsDuration = duration * 2 / 3;

    if (!fromFab) {
        // Force measure / layout the dialog back to it's original bounds
        view.measure(makeMeasureSpec(startBounds.width(), View.MeasureSpec.EXACTLY),
                makeMeasureSpec(startBounds.height(), View.MeasureSpec.EXACTLY));
        view.layout(startBounds.left, startBounds.top, startBounds.right, startBounds.bottom);
    }

    final int translationX = startBounds.centerX() - endBounds.centerX();
    final int translationY = startBounds.centerY() - endBounds.centerY();
    if (fromFab) {
        view.setTranslationX(translationX);
        view.setTranslationY(translationY);
    }

    // Add a color overlay to fake appearance of the FAB
    final ColorDrawable fabColor = new ColorDrawable(color);
    fabColor.setBounds(0, 0, dialogBounds.width(), dialogBounds.height());
    if (!fromFab) {
        fabColor.setAlpha(0);
    }
    view.getOverlay().add(fabColor);

    // Add an icon overlay again to fake the appearance of the FAB
    final Drawable fabIcon = ContextCompat.getDrawable(sceneRoot.getContext(), icon).mutate();
    final int iconLeft = (dialogBounds.width() - fabIcon.getIntrinsicWidth()) / 2;
    final int iconTop = (dialogBounds.height() - fabIcon.getIntrinsicHeight()) / 2;
    fabIcon.setBounds(iconLeft, iconTop, iconLeft + fabIcon.getIntrinsicWidth(),
            iconTop + fabIcon.getIntrinsicHeight());
    if (!fromFab) {
        fabIcon.setAlpha(0);
    }
    view.getOverlay().add(fabIcon);

    // Circular clip from/to the FAB size
    final Animator circularReveal;
    if (fromFab) {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, startBounds.width() / 2,
                (float) Math.hypot(endBounds.width() / 2, endBounds.height() / 2));
        circularReveal.setInterpolator(new FastOutLinearInInterpolator());
    } else {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, (float) Math.hypot(startBounds.width() / 2, startBounds.height() / 2),
                endBounds.width() / 2);
        circularReveal.setInterpolator(new LinearOutSlowInInterpolator());

        // Persist the end clip i.e. stay at FAB size after the reveal has run
        circularReveal.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setOutlineProvider(new ViewOutlineProvider() {
                    @Override
                    public void getOutline(View view, Outline outline) {
                        final int left = (view.getWidth() - fabBounds.width()) / 2;
                        final int top = (view.getHeight() - fabBounds.height()) / 2;
                        outline.setOval(left, top, left + fabBounds.width(), top + fabBounds.height());
                        view.setClipToOutline(true);
                    }
                });
            }
        });
    }
    circularReveal.setDuration(duration);

    // Translate to end position along an arc
    final Animator translate = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, View.TRANSLATION_Y,
            fromFab ? getPathMotion().getPath(translationX, translationY, 0, 0)
                    : getPathMotion().getPath(0, 0, -translationX, -translationY));
    translate.setDuration(duration);
    translate.setInterpolator(fastOutSlowInInterpolator);

    // Fade contents of non-FAB view in/out
    List<Animator> fadeContents = null;
    if (view instanceof ViewGroup) {
        final ViewGroup vg = ((ViewGroup) view);
        fadeContents = new ArrayList<>(vg.getChildCount());
        for (int i = vg.getChildCount() - 1; i >= 0; i--) {
            final View child = vg.getChildAt(i);
            final Animator fade = ObjectAnimator.ofFloat(child, View.ALPHA, fromFab ? 1f : 0f);
            if (fromFab) {
                child.setAlpha(0f);
            }
            fade.setDuration(twoThirdsDuration);
            fade.setInterpolator(fastOutSlowInInterpolator);
            fadeContents.add(fade);
        }
    }

    // Fade in/out the fab color & icon overlays
    final Animator colorFade = ObjectAnimator.ofInt(fabColor, "alpha", fromFab ? 0 : 255);
    final Animator iconFade = ObjectAnimator.ofInt(fabIcon, "alpha", fromFab ? 0 : 255);
    if (!fromFab) {
        colorFade.setStartDelay(halfDuration);
        iconFade.setStartDelay(halfDuration);
    }
    colorFade.setDuration(halfDuration);
    iconFade.setDuration(halfDuration);
    colorFade.setInterpolator(fastOutSlowInInterpolator);
    iconFade.setInterpolator(fastOutSlowInInterpolator);

    // Work around issue with elevation shadows. At the end of the return transition the shared
    // element's shadow is drawn twice (by each activity) which is jarring. This workaround
    // still causes the shadow to snap, but it's better than seeing it double drawn.
    Animator elevation = null;
    if (!fromFab) {
        elevation = ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, -view.getElevation());
        elevation.setDuration(duration);
        elevation.setInterpolator(fastOutSlowInInterpolator);
    }

    // Run all animations together
    final AnimatorSet transition = new AnimatorSet();
    transition.playTogether(circularReveal, translate, colorFade, iconFade);
    transition.playTogether(fadeContents);
    if (elevation != null) {
        transition.play(elevation);
    }
    if (fromFab) {
        transition.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // Clean up
                view.getOverlay().clear();
            }
        });
    }
    return new NoPauseAnimator(transition);
}

From source file:com.amitupadhyay.aboutexample.ui.transitions.FabTransform.java

@Override
public Animator createAnimator(final ViewGroup sceneRoot, final TransitionValues startValues,
        final TransitionValues endValues) {
    if (startValues == null || endValues == null)
        return null;

    final Rect startBounds = (Rect) startValues.values.get(PROP_BOUNDS);
    final Rect endBounds = (Rect) endValues.values.get(PROP_BOUNDS);

    final boolean fromFab = endBounds.width() > startBounds.width();
    final View view = endValues.view;
    final Rect dialogBounds = fromFab ? endBounds : startBounds;
    final Rect fabBounds = fromFab ? startBounds : endBounds;
    final Interpolator fastOutSlowInInterpolator = AnimUtils
            .getFastOutSlowInInterpolator(sceneRoot.getContext());
    final long duration = getDuration();
    final long halfDuration = duration / 2;
    final long twoThirdsDuration = duration * 2 / 3;

    if (!fromFab) {
        // Force measure / layout the dialog back to it's original bounds
        view.measure(makeMeasureSpec(startBounds.width(), View.MeasureSpec.EXACTLY),
                makeMeasureSpec(startBounds.height(), View.MeasureSpec.EXACTLY));
        view.layout(startBounds.left, startBounds.top, startBounds.right, startBounds.bottom);
    }// w w w. j av  a 2  s  .com

    final int translationX = startBounds.centerX() - endBounds.centerX();
    final int translationY = startBounds.centerY() - endBounds.centerY();
    if (fromFab) {
        view.setTranslationX(translationX);
        view.setTranslationY(translationY);
    }

    // Add a color overlay to fake appearance of the FAB
    final ColorDrawable fabColor = new ColorDrawable(color);
    fabColor.setBounds(0, 0, dialogBounds.width(), dialogBounds.height());
    if (!fromFab)
        fabColor.setAlpha(0);
    view.getOverlay().add(fabColor);

    // Add an icon overlay again to fake the appearance of the FAB
    final Drawable fabIcon = ContextCompat.getDrawable(sceneRoot.getContext(), icon).mutate();
    final int iconLeft = (dialogBounds.width() - fabIcon.getIntrinsicWidth()) / 2;
    final int iconTop = (dialogBounds.height() - fabIcon.getIntrinsicHeight()) / 2;
    fabIcon.setBounds(iconLeft, iconTop, iconLeft + fabIcon.getIntrinsicWidth(),
            iconTop + fabIcon.getIntrinsicHeight());
    if (!fromFab)
        fabIcon.setAlpha(0);
    view.getOverlay().add(fabIcon);

    // Circular clip from/to the FAB size
    final Animator circularReveal;
    if (fromFab) {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, startBounds.width() / 2,
                (float) Math.hypot(endBounds.width() / 2, endBounds.height() / 2));
        circularReveal.setInterpolator(AnimUtils.getFastOutLinearInInterpolator(sceneRoot.getContext()));
    } else {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, (float) Math.hypot(startBounds.width() / 2, startBounds.height() / 2),
                endBounds.width() / 2);
        circularReveal.setInterpolator(AnimUtils.getLinearOutSlowInInterpolator(sceneRoot.getContext()));

        // Persist the end clip i.e. stay at FAB size after the reveal has run
        circularReveal.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setOutlineProvider(new ViewOutlineProvider() {
                    @Override
                    public void getOutline(View view, Outline outline) {
                        final int left = (view.getWidth() - fabBounds.width()) / 2;
                        final int top = (view.getHeight() - fabBounds.height()) / 2;
                        outline.setOval(left, top, left + fabBounds.width(), top + fabBounds.height());
                        view.setClipToOutline(true);
                    }
                });
            }
        });
    }
    circularReveal.setDuration(duration);

    // Translate to end position along an arc
    final Animator translate = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, View.TRANSLATION_Y,
            fromFab ? getPathMotion().getPath(translationX, translationY, 0, 0)
                    : getPathMotion().getPath(0, 0, -translationX, -translationY));
    translate.setDuration(duration);
    translate.setInterpolator(fastOutSlowInInterpolator);

    // Fade contents of non-FAB view in/out
    List<Animator> fadeContents = null;
    if (view instanceof ViewGroup) {
        final ViewGroup vg = ((ViewGroup) view);
        fadeContents = new ArrayList<>(vg.getChildCount());
        for (int i = vg.getChildCount() - 1; i >= 0; i--) {
            final View child = vg.getChildAt(i);
            final Animator fade = ObjectAnimator.ofFloat(child, View.ALPHA, fromFab ? 1f : 0f);
            if (fromFab) {
                child.setAlpha(0f);
            }
            fade.setDuration(twoThirdsDuration);
            fade.setInterpolator(fastOutSlowInInterpolator);
            fadeContents.add(fade);
        }
    }

    // Fade in/out the fab color & icon overlays
    final Animator colorFade = ObjectAnimator.ofInt(fabColor, "alpha", fromFab ? 0 : 255);
    final Animator iconFade = ObjectAnimator.ofInt(fabIcon, "alpha", fromFab ? 0 : 255);
    if (!fromFab) {
        colorFade.setStartDelay(halfDuration);
        iconFade.setStartDelay(halfDuration);
    }
    colorFade.setDuration(halfDuration);
    iconFade.setDuration(halfDuration);
    colorFade.setInterpolator(fastOutSlowInInterpolator);
    iconFade.setInterpolator(fastOutSlowInInterpolator);

    // Work around issue with elevation shadows. At the end of the return transition the shared
    // element's shadow is drawn twice (by each activity) which is jarring. This workaround
    // still causes the shadow to snap, but it's better than seeing it double drawn.
    Animator elevation = null;
    if (!fromFab) {
        elevation = ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, -view.getElevation());
        elevation.setDuration(duration);
        elevation.setInterpolator(fastOutSlowInInterpolator);
    }

    // Run all animations together
    final AnimatorSet transition = new AnimatorSet();
    transition.playTogether(circularReveal, translate, colorFade, iconFade);
    transition.playTogether(fadeContents);
    if (elevation != null)
        transition.play(elevation);
    if (fromFab) {
        transition.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // Clean up
                view.getOverlay().clear();
            }
        });
    }
    return new AnimUtils.NoPauseAnimator(transition);
}

From source file:ameircom.keymedia.Activity.transition.FabTransform.java

@Override
public Animator createAnimator(final ViewGroup sceneRoot, final TransitionValues startValues,
        final TransitionValues endValues) {
    if (startValues == null || endValues == null)
        return null;

    final Rect startBounds = (Rect) startValues.values.get(PROP_BOUNDS);
    final Rect endBounds = (Rect) endValues.values.get(PROP_BOUNDS);

    final boolean fromFab = endBounds.width() > startBounds.width();
    final View view = endValues.view;
    final Rect dialogBounds = fromFab ? endBounds : startBounds;
    final Rect fabBounds = fromFab ? startBounds : endBounds;
    final Interpolator fastOutSlowInInterpolator = AnimUtils
            .getFastOutSlowInInterpolator(sceneRoot.getContext());
    final long duration = getDuration();
    final long halfDuration = duration / 2;
    final long twoThirdsDuration = duration * 2 / 3;

    if (!fromFab) {
        // Force measure / layout the dialog back to it's original bounds
        view.measure(makeMeasureSpec(startBounds.width(), View.MeasureSpec.EXACTLY),
                makeMeasureSpec(startBounds.height(), View.MeasureSpec.EXACTLY));
        view.layout(startBounds.left, startBounds.top, startBounds.right, startBounds.bottom);
    }//w ww .  j a v  a  2s  .com

    final int translationX = startBounds.centerX() - endBounds.centerX();
    final int translationY = startBounds.centerY() - endBounds.centerY();
    if (fromFab) {
        view.setTranslationX(translationX);
        view.setTranslationY(translationY);
    }

    // Add a color overlay to fake appearance of the FAB
    final ColorDrawable fabColor = new ColorDrawable(color);
    fabColor.setBounds(0, 0, dialogBounds.width(), dialogBounds.height());
    if (!fromFab)
        fabColor.setAlpha(0);
    view.getOverlay().add(fabColor);

    // Add an icon overlay again to fake the appearance of the FAB
    final Drawable fabIcon = ContextCompat.getDrawable(sceneRoot.getContext(), icon).mutate();
    final int iconLeft = (dialogBounds.width() - fabIcon.getIntrinsicWidth()) / 2;
    final int iconTop = (dialogBounds.height() - fabIcon.getIntrinsicHeight()) / 2;
    fabIcon.setBounds(iconLeft, iconTop, iconLeft + fabIcon.getIntrinsicWidth(),
            iconTop + fabIcon.getIntrinsicHeight());
    if (!fromFab)
        fabIcon.setAlpha(0);
    view.getOverlay().add(fabIcon);

    // Circular clip from/to the FAB size
    final Animator circularReveal;
    if (fromFab) {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, startBounds.width() / 2,
                (float) Math.hypot(endBounds.width() / 2, endBounds.width() / 2));
        circularReveal.setInterpolator(AnimUtils.getFastOutLinearInInterpolator(sceneRoot.getContext()));
    } else {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, (float) Math.hypot(startBounds.width() / 2, startBounds.width() / 2),
                endBounds.width() / 2);
        circularReveal.setInterpolator(AnimUtils.getLinearOutSlowInInterpolator(sceneRoot.getContext()));

        // Persist the end clip i.e. stay at FAB size after the reveal has run
        circularReveal.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setOutlineProvider(new ViewOutlineProvider() {
                    @Override
                    public void getOutline(View view, Outline outline) {
                        final int left = (view.getWidth() - fabBounds.width()) / 2;
                        final int top = (view.getHeight() - fabBounds.height()) / 2;
                        outline.setOval(left, top, left + fabBounds.width(), top + fabBounds.height());
                        view.setClipToOutline(true);
                    }
                });
            }
        });
    }
    circularReveal.setDuration(duration);

    // Translate to end position along an arc
    final Animator translate = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, View.TRANSLATION_Y,
            fromFab ? getPathMotion().getPath(translationX, translationY, 0, 0)
                    : getPathMotion().getPath(0, 0, -translationX, -translationY));
    translate.setDuration(duration);
    translate.setInterpolator(fastOutSlowInInterpolator);

    // Fade contents of non-FAB view in/out
    List<Animator> fadeContents = null;
    if (view instanceof ViewGroup) {
        final ViewGroup vg = ((ViewGroup) view);
        fadeContents = new ArrayList<>(vg.getChildCount());
        for (int i = vg.getChildCount() - 1; i >= 0; i--) {
            final View child = vg.getChildAt(i);
            final Animator fade = ObjectAnimator.ofFloat(child, View.ALPHA, fromFab ? 1f : 0f);
            if (fromFab) {
                child.setAlpha(0f);
            }
            fade.setDuration(twoThirdsDuration);
            fade.setInterpolator(fastOutSlowInInterpolator);
            fadeContents.add(fade);
        }
    }

    // Fade in/out the fab color & icon overlays
    final Animator colorFade = ObjectAnimator.ofInt(fabColor, "alpha", fromFab ? 0 : 255);
    final Animator iconFade = ObjectAnimator.ofInt(fabIcon, "alpha", fromFab ? 0 : 255);
    if (!fromFab) {
        colorFade.setStartDelay(halfDuration);
        iconFade.setStartDelay(halfDuration);
    }
    colorFade.setDuration(halfDuration);
    iconFade.setDuration(halfDuration);
    colorFade.setInterpolator(fastOutSlowInInterpolator);
    iconFade.setInterpolator(fastOutSlowInInterpolator);

    // Work around issue with elevation shadows. At the end of the return transition the shared
    // element's shadow is drawn twice (by each activity) which is jarring. This workaround
    // still causes the shadow to snap, but it's better than seeing it double drawn.
    Animator elevation = null;
    if (!fromFab) {
        elevation = ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, -view.getElevation());
        elevation.setDuration(duration);
        elevation.setInterpolator(fastOutSlowInInterpolator);
    }

    // Run all animations together
    final AnimatorSet transition = new AnimatorSet();
    transition.playTogether(circularReveal, translate, colorFade, iconFade);
    transition.playTogether(fadeContents);
    if (elevation != null)
        transition.play(elevation);
    if (fromFab) {
        transition.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // Clean up
                view.getOverlay().clear();
            }
        });
    }
    return new AnimUtils.NoPauseAnimator(transition);
}

From source file:com.hitomi.circlemenu.transtion.FabTransform.java

@Override
public Animator createAnimator(final ViewGroup sceneRoot, final TransitionValues startValues,
        final TransitionValues endValues) {
    if (startValues == null || endValues == null)
        return null;

    final Rect startBounds = (Rect) startValues.values.get(PROP_BOUNDS);
    final Rect endBounds = (Rect) endValues.values.get(PROP_BOUNDS);

    final boolean fromFab = endBounds.width() > startBounds.width();
    final View view = endValues.view;
    final Rect dialogBounds = fromFab ? endBounds : startBounds;
    final Rect fabBounds = fromFab ? startBounds : endBounds;
    final Interpolator fastOutSlowInInterpolator = AnimUtils
            .getFastOutSlowInInterpolator(sceneRoot.getContext());
    final long duration = getDuration();
    final long halfDuration = duration / 2;
    final long twoThirdsDuration = duration * 2 / 3;

    if (!fromFab) {
        // Force measure / layout the dialog back to it'page_two original bounds
        view.measure(makeMeasureSpec(startBounds.width(), View.MeasureSpec.EXACTLY),
                makeMeasureSpec(startBounds.height(), View.MeasureSpec.EXACTLY));
        view.layout(startBounds.left, startBounds.top, startBounds.right, startBounds.bottom);
    }//from  ww w. j  a v a  2s .c om

    final int translationX = startBounds.centerX() - endBounds.centerX();
    final int translationY = startBounds.centerY() - endBounds.centerY();
    if (fromFab) {
        view.setTranslationX(translationX);
        view.setTranslationY(translationY);
    }

    // Add a color overlay to fake appearance of the FAB
    final ColorDrawable fabColor = new ColorDrawable(color);
    fabColor.setBounds(0, 0, dialogBounds.width(), dialogBounds.height());
    if (!fromFab)
        fabColor.setAlpha(0);
    view.getOverlay().add(fabColor);

    // Add an icon overlay again to fake the appearance of the FAB
    final Drawable fabIcon = ContextCompat.getDrawable(sceneRoot.getContext(), icon).mutate();
    final int iconLeft = (dialogBounds.width() - fabIcon.getIntrinsicWidth()) / 2;
    final int iconTop = (dialogBounds.height() - fabIcon.getIntrinsicHeight()) / 2;
    fabIcon.setBounds(iconLeft, iconTop, iconLeft + fabIcon.getIntrinsicWidth(),
            iconTop + fabIcon.getIntrinsicHeight());
    if (!fromFab)
        fabIcon.setAlpha(0);
    view.getOverlay().add(fabIcon);

    // Circular clip from/to the FAB size
    final Animator circularReveal;
    if (fromFab) {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, startBounds.width() / 2,
                (float) Math.hypot(endBounds.width() / 2, endBounds.height() / 2));
        circularReveal.setInterpolator(AnimUtils.getFastOutLinearInInterpolator(sceneRoot.getContext()));
    } else {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, (float) Math.hypot(startBounds.width() / 2, startBounds.height() / 2),
                endBounds.width() / 2);
        circularReveal.setInterpolator(AnimUtils.getLinearOutSlowInInterpolator(sceneRoot.getContext()));

        // Persist the end clip i.e. stay at FAB size after the reveal has run
        circularReveal.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setOutlineProvider(new ViewOutlineProvider() {
                    @Override
                    public void getOutline(View view, Outline outline) {
                        final int left = (view.getWidth() - fabBounds.width()) / 2;
                        final int top = (view.getHeight() - fabBounds.height()) / 2;
                        outline.setOval(left, top, left + fabBounds.width(), top + fabBounds.height());
                        view.setClipToOutline(true);
                    }
                });
            }
        });
    }
    circularReveal.setDuration(duration);

    // Translate to end position along an arc
    final Animator translate = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, View.TRANSLATION_Y,
            fromFab ? getPathMotion().getPath(translationX, translationY, 0, 0)
                    : getPathMotion().getPath(0, 0, -translationX, -translationY));
    translate.setDuration(duration);
    translate.setInterpolator(fastOutSlowInInterpolator);

    // Fade contents of non-FAB view in/out
    List<Animator> fadeContents = null;
    if (view instanceof ViewGroup) {
        final ViewGroup vg = ((ViewGroup) view);
        fadeContents = new ArrayList<>(vg.getChildCount());
        for (int i = vg.getChildCount() - 1; i >= 0; i--) {
            final View child = vg.getChildAt(i);
            final Animator fade = ObjectAnimator.ofFloat(child, View.ALPHA, fromFab ? 1f : 0f);
            if (fromFab) {
                child.setAlpha(0f);
            }
            fade.setDuration(twoThirdsDuration);
            fade.setInterpolator(fastOutSlowInInterpolator);
            fadeContents.add(fade);
        }
    }

    // Fade in/out the fab color & icon overlays
    final Animator colorFade = ObjectAnimator.ofInt(fabColor, "alpha", fromFab ? 0 : 255);
    final Animator iconFade = ObjectAnimator.ofInt(fabIcon, "alpha", fromFab ? 0 : 255);
    if (!fromFab) {
        colorFade.setStartDelay(halfDuration);
        iconFade.setStartDelay(halfDuration);
    }
    colorFade.setDuration(halfDuration);
    iconFade.setDuration(halfDuration);
    colorFade.setInterpolator(fastOutSlowInInterpolator);
    iconFade.setInterpolator(fastOutSlowInInterpolator);

    // Work around issue with elevation shadows. At the end of the return transition the shared
    // element'page_two shadow is drawn twice (by each activity) which is jarring. This workaround
    // still causes the shadow to snap, but it'page_two better than seeing it double drawn.
    Animator elevation = null;
    if (!fromFab) {
        elevation = ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, -view.getElevation());
        elevation.setDuration(duration);
        elevation.setInterpolator(fastOutSlowInInterpolator);
    }

    // Run all animations together
    final AnimatorSet transition = new AnimatorSet();
    transition.playTogether(circularReveal, translate, colorFade, iconFade);
    transition.playTogether(fadeContents);
    if (elevation != null)
        transition.play(elevation);
    if (fromFab) {
        transition.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // Clean up
                view.getOverlay().clear();
            }
        });
    }
    return new AnimUtils.NoPauseAnimator(transition);
}

From source file:ccv.checkhelzio.nuevaagendacucsh.transitions.FabTransition.java

@Override
public Animator createAnimator(final ViewGroup sceneRoot, final TransitionValues startValues,
        final TransitionValues endValues) {
    if (startValues == null || endValues == null)
        return null;

    final Rect startBounds = (Rect) startValues.values.get(PROP_BOUNDS);
    final Rect endBounds = (Rect) endValues.values.get(PROP_BOUNDS);

    final boolean fromFab = endBounds.width() > startBounds.width();
    final View view = endValues.view;
    final Rect dialogBounds = fromFab ? endBounds : startBounds;
    final Rect fabBounds = fromFab ? startBounds : endBounds;
    final Interpolator fastOutSlowInInterpolator = AnimUtils
            .getFastOutSlowInInterpolator(sceneRoot.getContext());
    final long duration = getDuration();
    final long halfDuration = duration / 2;
    final long twoThirdsDuration = duration * 2 / 3;

    if (!fromFab) {
        // Force measure / layout the dialog back to it's original bounds
        view.measure(makeMeasureSpec(startBounds.width(), View.MeasureSpec.EXACTLY),
                makeMeasureSpec(startBounds.height(), View.MeasureSpec.EXACTLY));
        view.layout(startBounds.left, startBounds.top, startBounds.right, startBounds.bottom);
    }//from w w  w  .  j  ava2  s.co  m

    final int translationX = startBounds.centerX() - endBounds.centerX();
    final int translationY = startBounds.centerY() - endBounds.centerY();
    if (fromFab) {
        view.setTranslationX(translationX);
        view.setTranslationY(translationY);
    }

    // Add a color overlay to fake appearance of the FAB
    final ColorDrawable fabColor = new ColorDrawable(color);
    fabColor.setBounds(0, 0, dialogBounds.width(), dialogBounds.height());
    if (!fromFab)
        fabColor.setAlpha(0);
    view.getOverlay().add(fabColor);

    // Add an icon overlay again to fake the appearance of the FAB
    final Drawable fabIcon = ContextCompat.getDrawable(sceneRoot.getContext(), icon).mutate();
    final int iconLeft = (dialogBounds.width() - fabIcon.getIntrinsicWidth()) / 2;
    final int iconTop = (dialogBounds.height() - fabIcon.getIntrinsicHeight()) / 2;
    fabIcon.setBounds(iconLeft, iconTop, iconLeft + fabIcon.getIntrinsicWidth(),
            iconTop + fabIcon.getIntrinsicHeight());
    if (!fromFab)
        fabIcon.setAlpha(0);
    view.getOverlay().add(fabIcon);

    // Circular clip from/to the FAB size
    final Animator circularReveal;
    if (fromFab) {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, startBounds.width() / 2,
                (float) Math.hypot(endBounds.width() / 2, endBounds.height() / 2));
        circularReveal.setInterpolator(AnimUtils.getFastOutLinearInInterpolator(sceneRoot.getContext()));
        //circularReveal.setInterpolator(new AnimationUtils().loadInterpolator(sceneRoot.getContext(), android.R.interpolator.fast_out_slow_in));
    } else {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, (float) Math.hypot(startBounds.width() / 2, startBounds.height() / 2),
                endBounds.width() / 2);
        circularReveal.setInterpolator(AnimUtils.getLinearOutSlowInInterpolator(sceneRoot.getContext()));

        // Persist the end clip i.e. stay at FAB size after the reveal has run
        circularReveal.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setOutlineProvider(new ViewOutlineProvider() {
                    @Override
                    public void getOutline(View view, Outline outline) {
                        final int left = (view.getWidth() - fabBounds.width()) / 2;
                        final int top = (view.getHeight() - fabBounds.height()) / 2;
                        outline.setOval(left, top, left + fabBounds.width(), top + fabBounds.height());
                        view.setClipToOutline(true);
                    }
                });
            }
        });
    }
    circularReveal.setDuration(duration);

    // Translate to end position along an arc
    final Animator translate = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, View.TRANSLATION_Y,
            fromFab ? getPathMotion().getPath(translationX, translationY, 0, 0)
                    : getPathMotion().getPath(0, 0, -translationX, -translationY));
    translate.setDuration(duration);
    translate.setInterpolator(fastOutSlowInInterpolator);

    // Fade contents of non-FAB view in/out
    List<Animator> fadeContents = null;
    if (view instanceof ViewGroup) {
        final ViewGroup vg = ((ViewGroup) view);
        fadeContents = new ArrayList<>(vg.getChildCount());
        for (int i = vg.getChildCount() - 1; i >= 0; i--) {
            final View child = vg.getChildAt(i);
            final Animator fade = ObjectAnimator.ofFloat(child, View.ALPHA, fromFab ? 1f : 0f);
            if (fromFab) {
                child.setAlpha(0f);
            }
            fade.setDuration(twoThirdsDuration);
            fade.setInterpolator(fastOutSlowInInterpolator);
            fadeContents.add(fade);
        }
    }

    // Fade in/out the fab color & icon overlays
    final Animator colorFade = ObjectAnimator.ofInt(fabColor, "alpha", fromFab ? 0 : 255);
    final Animator iconFade = ObjectAnimator.ofInt(fabIcon, "alpha", fromFab ? 0 : 255);
    if (!fromFab) {
        colorFade.setStartDelay(halfDuration);
        iconFade.setStartDelay(halfDuration);
    }
    colorFade.setDuration(halfDuration);
    iconFade.setDuration(halfDuration);
    colorFade.setInterpolator(fastOutSlowInInterpolator);
    iconFade.setInterpolator(fastOutSlowInInterpolator);

    // Work around issue with elevation shadows. At the end of the return transition the shared
    // element's shadow is drawn twice (by each activity) which is jarring. This workaround
    // still causes the shadow to snap, but it's better than seeing it double drawn.
    Animator elevation = null;
    if (!fromFab) {
        elevation = ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, -view.getElevation());
        elevation.setDuration(duration);
        elevation.setInterpolator(fastOutSlowInInterpolator);
    }

    // Run all animations together
    final AnimatorSet transition = new AnimatorSet();
    transition.playTogether(circularReveal, translate, colorFade, iconFade);
    transition.playTogether(fadeContents);
    if (elevation != null)
        transition.play(elevation);
    if (fromFab) {
        transition.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // Clean up
                view.getOverlay().clear();
            }
        });
    }
    return new AnimUtils.NoPauseAnimator(transition);
}

From source file:com.n3rditorium.pocketdoggie.transitions.FabTransform.java

@Override
public Animator createAnimator(final ViewGroup sceneRoot, final TransitionValues startValues,
        final TransitionValues endValues) {
    if (startValues == null || endValues == null) {
        return null;
    }//from   w  w w.  j  a  v  a  2 s .c  o  m

    final Rect startBounds = (Rect) startValues.values.get(PROP_BOUNDS);
    final Rect endBounds = (Rect) endValues.values.get(PROP_BOUNDS);

    final boolean fromFab = endBounds.width() > startBounds.width();
    final View view = endValues.view;
    final Rect dialogBounds = fromFab ? endBounds : startBounds;
    final Rect fabBounds = fromFab ? startBounds : endBounds;
    final Interpolator fastOutSlowInInterpolator = AnimUtils
            .getFastOutSlowInInterpolator(sceneRoot.getContext());
    final long duration = getDuration();
    final long halfDuration = duration / 2;
    final long twoThirdsDuration = duration * 2 / 3;

    if (!fromFab) {
        // Force measure / layout the dialog back to it's original bounds
        view.measure(makeMeasureSpec(startBounds.width(), View.MeasureSpec.EXACTLY),
                makeMeasureSpec(startBounds.height(), View.MeasureSpec.EXACTLY));
        view.layout(startBounds.left, startBounds.top, startBounds.right, startBounds.bottom);
    }

    final int translationX = startBounds.centerX() - endBounds.centerX();
    final int translationY = startBounds.centerY() - endBounds.centerY();
    if (fromFab) {
        view.setTranslationX(translationX);
        view.setTranslationY(translationY);
    }

    // Add a color overlay to fake appearance of the FAB
    final ColorDrawable fabColor = new ColorDrawable(color);
    fabColor.setBounds(0, 0, dialogBounds.width(), dialogBounds.height());
    if (!fromFab) {
        fabColor.setAlpha(0);
    }
    view.getOverlay().add(fabColor);

    // Add an icon overlay again to fake the appearance of the FAB
    final Drawable fabIcon = ContextCompat.getDrawable(sceneRoot.getContext(), icon).mutate();
    final int iconLeft = (dialogBounds.width() - fabIcon.getIntrinsicWidth()) / 2;
    final int iconTop = (dialogBounds.height() - fabIcon.getIntrinsicHeight()) / 2;
    fabIcon.setBounds(iconLeft, iconTop, iconLeft + fabIcon.getIntrinsicWidth(),
            iconTop + fabIcon.getIntrinsicHeight());
    if (!fromFab) {
        fabIcon.setAlpha(0);
    }
    view.getOverlay().add(fabIcon);

    // Circular clip from/to the FAB size
    final Animator circularReveal;
    if (fromFab) {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, startBounds.width() / 2,
                (float) Math.hypot(endBounds.width() / 2, endBounds.width() / 2));
        circularReveal.setInterpolator(AnimUtils.getFastOutLinearInInterpolator(sceneRoot.getContext()));
    } else {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2,
                view.getHeight() / 2, (float) Math.hypot(startBounds.width() / 2, startBounds.width() / 2),
                endBounds.width() / 2);
        circularReveal.setInterpolator(AnimUtils.getLinearOutSlowInInterpolator(sceneRoot.getContext()));

        // Persist the end clip i.e. stay at FAB size after the reveal has run
        circularReveal.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setOutlineProvider(new ViewOutlineProvider() {
                    @Override
                    public void getOutline(View view, Outline outline) {
                        final int left = (view.getWidth() - fabBounds.width()) / 2;
                        final int top = (view.getHeight() - fabBounds.height()) / 2;
                        outline.setOval(left, top, left + fabBounds.width(), top + fabBounds.height());
                        view.setClipToOutline(true);
                    }
                });
            }
        });
    }
    circularReveal.setDuration(duration);

    // Translate to end position along an arc
    final Animator translate = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, View.TRANSLATION_Y,
            fromFab ? getPathMotion().getPath(translationX, translationY, 0, 0)
                    : getPathMotion().getPath(0, 0, -translationX, -translationY));
    translate.setDuration(duration);
    translate.setInterpolator(fastOutSlowInInterpolator);

    // Fade contents of non-FAB view in/out
    List<Animator> fadeContents = null;
    if (view instanceof ViewGroup) {
        final ViewGroup vg = ((ViewGroup) view);
        fadeContents = new ArrayList<>(vg.getChildCount());
        for (int i = vg.getChildCount() - 1; i >= 0; i--) {
            final View child = vg.getChildAt(i);
            final Animator fade = ObjectAnimator.ofFloat(child, View.ALPHA, fromFab ? 1f : 0f);
            if (fromFab) {
                child.setAlpha(0f);
            }
            fade.setDuration(twoThirdsDuration);
            fade.setInterpolator(fastOutSlowInInterpolator);
            fadeContents.add(fade);
        }
    }

    // Fade in/out the fab color & icon overlays
    final Animator colorFade = ObjectAnimator.ofInt(fabColor, "alpha", fromFab ? 0 : 255);
    final Animator iconFade = ObjectAnimator.ofInt(fabIcon, "alpha", fromFab ? 0 : 255);
    if (!fromFab) {
        colorFade.setStartDelay(halfDuration);
        iconFade.setStartDelay(halfDuration);
    }
    colorFade.setDuration(halfDuration);
    iconFade.setDuration(halfDuration);
    colorFade.setInterpolator(fastOutSlowInInterpolator);
    iconFade.setInterpolator(fastOutSlowInInterpolator);

    // Work around issue with elevation shadows. At the end of the return transition the shared
    // element's shadow is drawn twice (by each activity) which is jarring. This workaround
    // still causes the shadow to snap, but it's better than seeing it double drawn.
    Animator elevation = null;
    if (!fromFab) {
        elevation = ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, -view.getElevation());
        elevation.setDuration(duration);
        elevation.setInterpolator(fastOutSlowInInterpolator);
    }

    // Run all animations together
    final AnimatorSet transition = new AnimatorSet();
    transition.playTogether(circularReveal, translate, colorFade, iconFade);
    transition.playTogether(fadeContents);
    if (elevation != null) {
        transition.play(elevation);
    }
    if (fromFab) {
        transition.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // Clean up
                view.getOverlay().clear();
            }
        });
    }
    return new AnimUtils.NoPauseAnimator(transition);
}

From source file:io.plaidapp.core.ui.transitions.ReflowText.java

/**
 * Create Animators to transition each run of text from start to end position and size.
 *//*from  w w w.j ava 2s  .  c om*/
@NonNull
private List<Animator> createRunAnimators(View view, ReflowData startData, ReflowData endData, Bitmap startText,
        Bitmap endText, List<Run> runs) {
    List<Animator> animators = new ArrayList<>(runs.size());
    int dx = startData.bounds.left - endData.bounds.left;
    int dy = startData.bounds.top - endData.bounds.top;
    long startDelay = 0L;
    // move text closest to the destination first i.e. loop forward or backward over the runs
    boolean upward = startData.bounds.centerY() > endData.bounds.centerY();
    boolean first = true;
    boolean lastRightward = true;
    LinearInterpolator linearInterpolator = new LinearInterpolator();

    for (int i = upward ? 0 : runs.size() - 1; ((upward && i < runs.size())
            || (!upward && i >= 0)); i += (upward ? 1 : -1)) {
        Run run = runs.get(i);

        // skip text runs which aren't visible in either state
        if (!run.startVisible && !run.endVisible)
            continue;

        // create & position the drawable which displays the run; add it to the overlay.
        SwitchDrawable drawable = new SwitchDrawable(startText, run.start, startData.textSize, endText, run.end,
                endData.textSize);
        drawable.setBounds(run.start.left + dx, run.start.top + dy, run.start.right + dx,
                run.start.bottom + dy);
        view.getOverlay().add(drawable);

        PropertyValuesHolder topLeft = PropertyValuesHolder.ofObject(SwitchDrawable.TOP_LEFT, null,
                getPathMotion().getPath(run.start.left + dx, run.start.top + dy, run.end.left, run.end.top));
        PropertyValuesHolder width = PropertyValuesHolder.ofInt(SwitchDrawable.WIDTH, run.start.width(),
                run.end.width());
        PropertyValuesHolder height = PropertyValuesHolder.ofInt(SwitchDrawable.HEIGHT, run.start.height(),
                run.end.height());
        // the progress property drives the switching behaviour
        PropertyValuesHolder progress = PropertyValuesHolder.ofFloat(SwitchDrawable.PROGRESS, 0f, 1f);
        Animator runAnim = ObjectAnimator.ofPropertyValuesHolder(drawable, topLeft, width, height, progress);

        boolean rightward = run.start.centerX() + dx < run.end.centerX();
        if ((run.startVisible && run.endVisible) && !first && rightward != lastRightward) {
            // increase the start delay (by a decreasing amount) for the next run
            // (if it's visible throughout) to stagger the movement and try to minimize overlaps
            startDelay += staggerDelay;
            staggerDelay *= STAGGER_DECAY;
        }
        lastRightward = rightward;
        first = false;

        runAnim.setStartDelay(startDelay);
        long animDuration = Math.max(minDuration, duration - (startDelay / 2));
        runAnim.setDuration(animDuration);
        animators.add(runAnim);

        if (run.startVisible != run.endVisible) {
            // if run is appearing/disappearing then fade it in/out
            ObjectAnimator fade = ObjectAnimator.ofInt(drawable, SwitchDrawable.ALPHA,
                    run.startVisible ? OPAQUE : TRANSPARENT, run.endVisible ? OPAQUE : TRANSPARENT);
            fade.setDuration((duration + startDelay) / 2);
            if (!run.startVisible) {
                drawable.setAlpha(TRANSPARENT);
                fade.setStartDelay((duration + startDelay) / 2);
            } else {
                fade.setStartDelay(startDelay);
            }
            animators.add(fade);
        } else {
            // slightly fade during transition to minimize movement
            ObjectAnimator fade = ObjectAnimator.ofInt(drawable, SwitchDrawable.ALPHA, OPAQUE,
                    OPACITY_MID_TRANSITION, OPAQUE);
            fade.setStartDelay(startDelay);
            fade.setDuration(duration + startDelay);
            fade.setInterpolator(linearInterpolator);
            animators.add(fade);
        }
    }
    return animators;
}

From source file:io.plaidapp.core.ui.transitions.ReflowText.java

@Override
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {

    if (startValues == null || endValues == null)
        return null;

    final View view = endValues.view;
    AnimatorSet transition = new AnimatorSet();
    ReflowData startData = (ReflowData) startValues.values.get(PROPNAME_DATA);
    ReflowData endData = (ReflowData) endValues.values.get(PROPNAME_DATA);
    duration = calculateDuration(startData.bounds, endData.bounds);

    // create layouts & capture a bitmaps of the text in both states
    // (with max lines variants where needed)
    Layout startLayout = createLayout(startData, sceneRoot.getContext(), false);
    Layout endLayout = createLayout(endData, sceneRoot.getContext(), false);
    Layout startLayoutMaxLines = null;/*from  w  w w . j  ava  2s  .co  m*/
    Layout endLayoutMaxLines = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // StaticLayout maxLines support
        if (startData.maxLines != -1) {
            startLayoutMaxLines = createLayout(startData, sceneRoot.getContext(), true);
        }
        if (endData.maxLines != -1) {
            endLayoutMaxLines = createLayout(endData, sceneRoot.getContext(), true);
        }
    }
    final Bitmap startText = createBitmap(startData,
            startLayoutMaxLines != null ? startLayoutMaxLines : startLayout);
    final Bitmap endText = createBitmap(endData, endLayoutMaxLines != null ? endLayoutMaxLines : endLayout);

    // temporarily turn off clipping so we can draw outside of our bounds don't draw
    view.setWillNotDraw(true);
    ((ViewGroup) view.getParent()).setClipChildren(false);

    // calculate the runs of text to move together
    List<Run> runs = getRuns(startData, startLayout, startLayoutMaxLines, endData, endLayout,
            endLayoutMaxLines);

    // create animators for moving, scaling and fading each run of text
    transition.playTogether(createRunAnimators(view, startData, endData, startText, endText, runs));

    if (!freezeFrame) {
        transition.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // clean up
                view.setWillNotDraw(false);
                view.getOverlay().clear();
                ((ViewGroup) view.getParent()).setClipChildren(true);
                startText.recycle();
                endText.recycle();
            }
        });
    }
    return transition;
}