Example usage for android.animation AnimatorSet addListener

List of usage examples for android.animation AnimatorSet addListener

Introduction

In this page you can find the example usage for android.animation AnimatorSet addListener.

Prototype

public void addListener(AnimatorListener listener) 

Source Link

Document

Adds a listener to the set of listeners that are sent events through the life of an animation, such as start, repeat, and end.

Usage

From source file:Main.java

public static void scaleHide(final View view, final Runnable rWhenEnd) {
    if (view.getWindowToken() == null) {
        view.setVisibility(View.INVISIBLE);
        if (rWhenEnd != null) {
            rWhenEnd.run();//w ww. ja  v a  2 s  .  c om
        }
        return;
    }
    ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0f);
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0f);
    AnimatorSet set = new AnimatorSet();
    set.playTogether(scaleX, scaleY);
    set.setDuration(DURATION_SHORT);
    set.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            view.setVisibility(View.INVISIBLE);
            if (rWhenEnd != null) {
                rWhenEnd.run();
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            view.setVisibility(View.INVISIBLE);
            if (rWhenEnd != null) {
                rWhenEnd.run();
            }
        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
    set.start();
}

From source file:Main.java

public static void scaleShow(final View view, final Runnable rWhenEnd) {
    if (view.getVisibility() == View.VISIBLE) {
        view.setVisibility(View.VISIBLE);
        if (rWhenEnd != null) {
            rWhenEnd.run();/*  w  w w. ja  va2s . c  om*/
        }
        return;
    }
    if (view.getWindowToken() == null) {
        view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
                scaleShow(view);
            }

            @Override
            public void onViewDetachedFromWindow(View v) {

            }
        });
    }
    ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f);
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f);
    AnimatorSet set = new AnimatorSet();
    set.playTogether(scaleX, scaleY);
    set.setDuration(DURATION_SHORT);
    set.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            view.setVisibility(View.VISIBLE);
            if (rWhenEnd != null) {
                rWhenEnd.run();
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            view.setVisibility(View.VISIBLE);
            if (rWhenEnd != null) {
                rWhenEnd.run();
            }
        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
    set.start();
}

From source file:Main.java

public static void animateColorChange(View view, int fromColor, int toColor, int duration,
        Animator.AnimatorListener listener) {
    if (view.getWindowToken() == null) {
        return;// www.  ja va  2  s  .  c o  m
    }
    AnimatorSet animation = new AnimatorSet();
    ObjectAnimator colorAnimator = ObjectAnimator.ofInt(view, COLOR_PROPERTY, fromColor, toColor);
    colorAnimator.setEvaluator(new ArgbEvaluator());
    colorAnimator.setDuration(duration);
    if (listener != null)
        animation.addListener(listener);
    animation.play(colorAnimator);
    animation.start();
}

From source file:com.microsoft.azure.engagement.ProductDiscountActivity.java

/**
 * Method that animates a view// w w  w .ja  v  a2  s. c  o  m
 *
 * @param view           The view to animate
 * @param objectAnimator The objectAnimator to play
 * @param isVisible      The visibility of the view at the end of the animation
 */
public static final void performAnimation(final View view, ObjectAnimator objectAnimator,
        final boolean isVisible) {
    view.setVisibility(View.VISIBLE);

    objectAnimator.setDuration(300);

    final AnimatorSet animatorSet = new AnimatorSet();

    animatorSet.play(objectAnimator);

    animatorSet.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            view.setVisibility(isVisible ? View.VISIBLE : View.INVISIBLE);
        }
    });
    animatorSet.start();
}

From source file:Main.java

public static void animateScaleIn(View view, long duration, Animator.AnimatorListener listener) {
    view.setScaleX(0f);//from   www .  j  av  a  2 s . co  m
    view.setScaleY(0f);
    view.setVisibility(View.VISIBLE);

    AnimatorSet scaleSet = new AnimatorSet();
    scaleSet.playTogether(ObjectAnimator.ofFloat(view, View.SCALE_X, 1f),
            ObjectAnimator.ofFloat(view, View.SCALE_Y, 1f));
    scaleSet.setInterpolator(new AccelerateDecelerateInterpolator());
    scaleSet.setDuration(duration);
    if (listener != null) {
        scaleSet.addListener(listener);
    }
    scaleSet.start();
    //return scaleSet;
}

From source file:Main.java

public static void runFlipHorizonAnimation(@NonNull View view, long duration, final Runnable rWhenEnd) {
    view.setAlpha(0);//from w  w  w. j  a va  2s  .  c om
    AnimatorSet set = new AnimatorSet();
    ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(view, "rotationY", -180f, 0f);
    ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
    set.setDuration(duration);
    set.playTogether(objectAnimator1, objectAnimator2);
    if (rWhenEnd != null)
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                rWhenEnd.run();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    set.start();
}

From source file:com.chromium.fontinstaller.util.ViewUtils.java

public static void reveal(Activity activity, View view, View sourceView, int colorRes) {
    if (activity == null || view == null || sourceView == null)
        return;//from w  w  w  .j a  v a 2s.c  o m
    if (isLollipop()) {
        final ViewGroupOverlay groupOverlay = (ViewGroupOverlay) activity.getWindow().getDecorView()
                .getOverlay();

        final Rect displayRect = new Rect();
        view.getGlobalVisibleRect(displayRect);

        // Make reveal cover the display and status bar.
        final View revealView = new View(activity);
        revealView.setTop(displayRect.top);
        revealView.setBottom(displayRect.bottom);
        revealView.setLeft(displayRect.left);
        revealView.setRight(displayRect.right);
        revealView.setBackgroundColor(ContextCompat.getColor(activity, colorRes));
        groupOverlay.add(revealView);

        final int[] clearLocation = new int[2];
        sourceView.getLocationInWindow(clearLocation);
        clearLocation[0] += sourceView.getWidth() / 2;
        clearLocation[1] += sourceView.getHeight() / 2;

        final int revealCenterX = clearLocation[0] - revealView.getLeft();
        final int revealCenterY = clearLocation[1] - revealView.getTop();

        final double x1_2 = Math.pow(revealView.getLeft() - revealCenterX, 2);
        final double x2_2 = Math.pow(revealView.getRight() - revealCenterX, 2);
        final double y_2 = Math.pow(revealView.getTop() - revealCenterY, 2);
        final float revealRadius = (float) Math.max(Math.sqrt(x1_2 + y_2), Math.sqrt(x2_2 + y_2));

        try {
            final Animator revealAnimator = ViewAnimationUtils.createCircularReveal(revealView, revealCenterX,
                    revealCenterY, 0.0f, revealRadius);
            revealAnimator
                    .setDuration(activity.getResources().getInteger(android.R.integer.config_mediumAnimTime));

            final Animator alphaAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f);
            alphaAnimator
                    .setDuration(activity.getResources().getInteger(android.R.integer.config_shortAnimTime));
            alphaAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    view.startAnimation(AnimationUtils.loadAnimation(activity, R.anim.abc_fade_in));
                    view.setVisibility(View.VISIBLE);
                }
            });

            final AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.play(revealAnimator).before(alphaAnimator);
            animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
            animatorSet.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animator) {
                    groupOverlay.remove(revealView);
                }
            });

            animatorSet.start();
        } catch (IllegalStateException e) {
            Timber.i("View is detached - not animating");
        }
    } else {
        view.setVisibility(View.VISIBLE);
    }
}

From source file:Main.java

public static void animateHeart(View view) {
    AnimatorSet animatorSet = new AnimatorSet();
    ObjectAnimator imgScaleUpYAnim = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 1.2f);
    imgScaleUpYAnim.setDuration(300);/*from   w  w  w. ja v a2  s  . c  o m*/
    ObjectAnimator imgScaleUpXAnim = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 1.2f);
    imgScaleUpXAnim.setDuration(300);
    ObjectAnimator imgScaleDownYAnim = ObjectAnimator.ofFloat(view, "scaleY", 1.2f, 1.0f);
    imgScaleDownYAnim.setDuration(300);
    imgScaleDownYAnim.setInterpolator(ACCELERATE_INTERPOLATOR);
    ObjectAnimator imgScaleDownXAnim = ObjectAnimator.ofFloat(view, "scaleX", 1.2f, 1.0f);
    imgScaleDownXAnim.setDuration(300);
    animatorSet.playTogether(imgScaleUpXAnim, imgScaleUpYAnim);
    animatorSet.play(imgScaleDownYAnim).with(imgScaleDownXAnim).after(imgScaleUpXAnim);
    animatorSet.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            view.clearAnimation();
            animatorSet.start();
        }
    });
    animatorSet.start();
}

From source file:org.deviceconnect.android.deviceplugin.linking.setting.fragment.LinkingHelpFragment.java

private void createAnimation(final View v) {
    float size = 12.0f * getResources().getDisplayMetrics().density;
    long time = 1000;

    List<Animator> animatorList = new ArrayList<>();

    ObjectAnimator fadeIn = ObjectAnimator.ofFloat(v, "translationY", -size, 0);
    fadeIn.setDuration(time);/*ww w .jav  a2  s. com*/
    animatorList.add(fadeIn);

    ObjectAnimator fadeOut = ObjectAnimator.ofFloat(v, "translationY", 0, -size);
    fadeOut.setDuration(time);
    animatorList.add(fadeOut);

    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playSequentially(animatorList);
    animatorSet.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            if (!mDestroy) {
                animation.start();
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    });
    animatorSet.start();
}

From source file:com.google.samples.apps.iosched.util.LPreviewUtilsBase.java

public void setOrAnimatePlusCheckIcon(final ImageView imageView, boolean isCheck, boolean allowAnimate) {
    final int imageResId = isCheck ? R.drawable.add_schedule_button_icon_checked
            : R.drawable.add_schedule_button_icon_unchecked;

    if (imageView.getTag() != null) {
        if (imageView.getTag() instanceof Animator) {
            Animator anim = (Animator) imageView.getTag();
            anim.end();/*from  w  w  w . j  a  va 2  s .c o m*/
            imageView.setAlpha(1f);
        }
    }

    if (allowAnimate && isCheck) {
        int duration = mActivity.getResources().getInteger(android.R.integer.config_shortAnimTime);

        Animator outAnimator = ObjectAnimator.ofFloat(imageView, View.ALPHA, 0f);
        outAnimator.setDuration(duration / 2);
        outAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                imageView.setImageResource(imageResId);
            }
        });

        AnimatorSet inAnimator = new AnimatorSet();
        outAnimator.setDuration(duration);
        inAnimator.playTogether(ObjectAnimator.ofFloat(imageView, View.ALPHA, 1f),
                ObjectAnimator.ofFloat(imageView, View.SCALE_X, 0f, 1f),
                ObjectAnimator.ofFloat(imageView, View.SCALE_Y, 0f, 1f));

        AnimatorSet set = new AnimatorSet();
        set.playSequentially(outAnimator, inAnimator);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                imageView.setTag(null);
            }
        });
        imageView.setTag(set);
        set.start();
    } else {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                imageView.setImageResource(imageResId);
            }
        });
    }
}