Example usage for android.animation ObjectAnimator setDuration

List of usage examples for android.animation ObjectAnimator setDuration

Introduction

In this page you can find the example usage for android.animation ObjectAnimator setDuration.

Prototype

@Override
@NonNull
public ObjectAnimator setDuration(long duration) 

Source Link

Document

Sets the length of the animation.

Usage

From source file:Main.java

public static void shake(View v) {
    final float distance = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4.0f,
            v.getResources().getDisplayMetrics());
    final ObjectAnimator animator = ObjectAnimator.ofFloat(v, "translationX", 0, distance, 0, -distance, 0);
    animator.setRepeatMode(ObjectAnimator.RESTART);
    animator.setInterpolator(new LinearInterpolator());
    animator.setRepeatCount(4);/*from   w ww .ja va 2 s  .c o  m*/
    animator.setDuration(150);
    animator.start();
}

From source file:Main.java

public static void animateTextChange(final TextView view, final String toText) {
    ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f);
    final ObjectAnimator restore = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
    alpha.setDuration(DURATION_SHORT);
    alpha.setInterpolator(new AccelerateDecelerateInterpolator());
    restore.setDuration(DURATION_SHORT);
    restore.setInterpolator(new AccelerateDecelerateInterpolator());
    alpha.addListener(new Animator.AnimatorListener() {
        @Override/*from w w  w  . ja  va 2 s.c om*/
        public void onAnimationStart(Animator animation) {
            // Do nothing.
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            view.setText(toText);
            restore.start();
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            view.setText(toText);
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            // Do nothing.
        }
    });
    alpha.start();
}

From source file:Main.java

public static void animateTextChange(final TextView view, @IdRes final int toText, final Runnable rWhenEnd) {
    ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f);
    final ObjectAnimator restore = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
    alpha.setDuration(DURATION_SHORT);
    alpha.setInterpolator(new AccelerateDecelerateInterpolator());
    restore.setDuration(DURATION_SHORT);
    restore.setInterpolator(new AccelerateDecelerateInterpolator());
    alpha.addListener(new Animator.AnimatorListener() {
        @Override//from  ww  w. j  a  va  2  s .  co m
        public void onAnimationStart(Animator animation) {
            // Do nothing.
        }

        @SuppressWarnings("ResourceType")
        @Override
        public void onAnimationEnd(Animator animation) {
            view.setText(toText);
            restore.start();
        }

        @SuppressWarnings("ResourceType")
        @Override
        public void onAnimationCancel(Animator animation) {
            view.setText(toText);
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            // Do nothing.
        }
    });
    if (rWhenEnd != null)
        restore.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

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

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

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    alpha.start();
}

From source file:com.jarklee.materialdatetimepicker.Utils.java

/**
 * Render an animator to pulsate a view in place.
 *
 * @param labelToAnimate the view to pulsate.
 * @return The animator object. Use .start() to begin.
 *///from www. ja v  a  2 s  . c o m
@RequiresApi(Build.VERSION_CODES.HONEYCOMB)
public static ObjectAnimator getPulseAnimator(View labelToAnimate, float decreaseRatio, float increaseRatio) {
    Keyframe k0 = Keyframe.ofFloat(0f, 1f);
    Keyframe k1 = Keyframe.ofFloat(0.275f, decreaseRatio);
    Keyframe k2 = Keyframe.ofFloat(0.69f, increaseRatio);
    Keyframe k3 = Keyframe.ofFloat(1f, 1f);

    PropertyValuesHolder scaleX = PropertyValuesHolder.ofKeyframe("scaleX", k0, k1, k2, k3);
    PropertyValuesHolder scaleY = PropertyValuesHolder.ofKeyframe("scaleY", k0, k1, k2, k3);
    ObjectAnimator pulseAnimator = ObjectAnimator.ofPropertyValuesHolder(labelToAnimate, scaleX, scaleY);
    pulseAnimator.setDuration(PULSE_ANIMATOR_DURATION);
    return pulseAnimator;
}

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

/**
 * Method that animates a view//ww  w.  ja  v a2 s.com
 *
 * @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:com.ae.apps.messagecounter.fragments.SentCountFragment.java

/**
 * Sets the progress value to a progressbar, with animation if the OS supports it
 * //  w w  w.  j ava 2  s . c  o  m
 * @param progressBar
 * @param progress
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static void setProgressWithAnimation(ProgressBar progressBar, int progress, long animDelay) {
    // We try to add animation for newer APIs here
    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.HONEYCOMB) {
        android.animation.ObjectAnimator animation = android.animation.ObjectAnimator.ofInt(progressBar,
                PROGRESS_VALUE, 0, progress);
        animation.setDuration(100);
        animation.setStartDelay(animDelay);
        animation.setInterpolator(new AccelerateDecelerateInterpolator());
        animation.start();
    } else {
        progressBar.setProgress(progress);
    }
}

From source file:Main.java

public static void animateTextViewMaxLinesChange(final TextView textView, final int maxLines,
        final int duration) {
    final int startHeight = textView.getMeasuredHeight();
    textView.setMaxLines(maxLines);/*from   w  ww .  jav a2  s .  com*/
    textView.measure(View.MeasureSpec.makeMeasureSpec(textView.getWidth(), View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    final int endHeight = textView.getMeasuredHeight();
    ObjectAnimator animation = ObjectAnimator.ofInt(textView, MAX_HEIGHT_ATTR, startHeight, endHeight);
    animation.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            if (textView.getMaxHeight() == endHeight) {
                textView.setMaxLines(maxLines);
            }
        }
    }

    );
    animation.setDuration(duration).start();
}

From source file:com.google.samples.apps.ourstreets.view.ViewUtils.java

/**
 * Create a color change animation over a foreground property of a {@link FrameLayout}.
 *
 * @param target The target view./*from  w  w w.  ja  v  a 2s .  c  om*/
 * @param startColorRes The color to start from.
 * @param targetColorRes The color this animation will end with.
 * @param interpolator The interpolator to use.
 * @return The color change animation.
 */
@NonNull
public static ObjectAnimator createColorChange(@NonNull FrameLayout target, @ColorRes int startColorRes,
        @ColorRes int targetColorRes, @NonNull Interpolator interpolator) {
    Context context = target.getContext();
    final int startColor = ContextCompat.getColor(context, startColorRes);
    final int targetColor = ContextCompat.getColor(context, targetColorRes);
    ObjectAnimator colorChange = ObjectAnimator.ofInt(target, ViewUtils.FOREGROUND_COLOR, startColor,
            targetColor);
    colorChange.setEvaluator(new ArgbEvaluator());
    colorChange.setInterpolator(interpolator);
    colorChange.setDuration(context.getResources().getInteger(android.R.integer.config_longAnimTime));
    return colorChange;
}

From source file:com.miz.utils.ViewUtils.java

public static void animateFabJump(View fab, Animator.AnimatorListener listener) {
    try {//from w  w  w . j  a v  a  2  s.c om
        ObjectAnimator animation = ObjectAnimator.ofFloat(fab, "translationY", -10f, -5f, 0f, 5f, 10f, 5f, 0f,
                -5f, -10f, -5f, 0f);
        animation.setDuration(350);
        animation.addListener(listener);
        animation.start();
    } catch (Exception e) {
        // Some devices crash at runtime when using the ObjectAnimator
    }
}

From source file:com.miz.utils.ViewUtils.java

/**
 * Animates the transition when changing the maxLines
 * attribute of a TextView./*from   w  w  w .j  av  a  2  s .  c  om*/
 * @param text
 * @param maxLines
 */
public static void animateTextViewMaxLines(TextView text, int maxLines) {
    try {
        ObjectAnimator animation = ObjectAnimator.ofInt(text, "maxLines", maxLines);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setDuration(200);
        animation.start();
    } catch (Exception e) {
        // Some devices crash at runtime when using the ObjectAnimator
        text.setMaxLines(maxLines);
    }
}