Example usage for android.animation ValueAnimator getAnimatedFraction

List of usage examples for android.animation ValueAnimator getAnimatedFraction

Introduction

In this page you can find the example usage for android.animation ValueAnimator getAnimatedFraction.

Prototype

public float getAnimatedFraction() 

Source Link

Document

Returns the current animation fraction, which is the elapsed/interpolated fraction used in the most recent frame update on the animation.

Usage

From source file:Main.java

public static ValueAnimator getRiseElevationValue(final View targetView, int duration, final int mimElevation,
        final int maxElevation) {
    ValueAnimator addElevationValueAnim = ValueAnimator.ofInt(1);
    addElevationValueAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
        @Override// ww  w.  j  a  v a  2  s .c  o  m
        public void onAnimationUpdate(ValueAnimator animation) {
            float fraction = animation.getAnimatedFraction();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                targetView.setElevation((1 - fraction) * maxElevation + mimElevation);
            }
        }
    });
    addElevationValueAnim.setDuration(duration);
    return addElevationValueAnim;
}

From source file:Main.java

public static void start(ValueAnimator... animators) {
    for (ValueAnimator animator : animators) {
        final float fraction = animator.getAnimatedFraction();
        if (fraction < 1.0f) {
            animator.start();/*www.jav  a2 s. com*/
        }
    }
}

From source file:Main.java

public static void reverse(ValueAnimator... animators) {
    for (ValueAnimator animator : animators) {
        final float fraction = animator.getAnimatedFraction();
        if (fraction > 0.0f) {
            animator.reverse();//from   w  ww .  ja va2 s. c o m
        }
    }
}

From source file:com.android.clear.reminder.AnimatorUtils.java

public static void reverse(ValueAnimator... animators) {
    for (ValueAnimator animator : animators) {
        final float fraction = animator.getAnimatedFraction();
        if (fraction > 0.0f) {
            animator.reverse();//from w w  w.j  av  a  2 s  . c  o  m
            setAnimatedFraction(animator, 1.0f - fraction);
        }
    }
}

From source file:android.support.v17.leanback.widget.AbstractMediaItemPresenter.java

/**
 * Each media item row can have multiple focusable elements; the details on the left and a set
 * of optional custom actions on the right.
 * The selector is a highlight that moves to highlight to cover whichever views is in focus.
 *
 * @param selectorView the selector view used to highlight an individual element within a row.
 * @param focusChangedView The component within the media row whose focus got changed.
 * @param layoutAnimator the ValueAnimator producing animation frames for the selector's width
 *                       and x-translation, generated by this method and stored for the each
 *                       {@link ViewHolder}.
 * @param isDetails Whether the changed-focused view is for a media item details (true) or
 *                  an action (false).//from w  w w .jav  a  2s . c o m
 */
private static ValueAnimator updateSelector(final View selectorView, View focusChangedView,
        ValueAnimator layoutAnimator, boolean isDetails) {
    int animationDuration = focusChangedView.getContext().getResources()
            .getInteger(android.R.integer.config_shortAnimTime);
    DecelerateInterpolator interpolator = new DecelerateInterpolator();

    int layoutDirection = ViewCompat.getLayoutDirection(selectorView);
    if (!focusChangedView.hasFocus()) {
        // if neither of the details or action views are in focus (ie. another row is in focus),
        // animate the selector out.
        selectorView.animate().cancel();
        selectorView.animate().alpha(0f).setDuration(animationDuration).setInterpolator(interpolator).start();
        // keep existing layout animator
        return layoutAnimator;
    } else {
        // cancel existing layout animator
        if (layoutAnimator != null) {
            layoutAnimator.cancel();
            layoutAnimator = null;
        }
        float currentAlpha = selectorView.getAlpha();
        selectorView.animate().alpha(1f).setDuration(animationDuration).setInterpolator(interpolator).start();

        final ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) selectorView.getLayoutParams();
        ViewGroup rootView = (ViewGroup) selectorView.getParent();
        sTempRect.set(0, 0, focusChangedView.getWidth(), focusChangedView.getHeight());
        rootView.offsetDescendantRectToMyCoords(focusChangedView, sTempRect);
        if (isDetails) {
            if (layoutDirection == View.LAYOUT_DIRECTION_RTL) {
                sTempRect.right += rootView.getHeight();
                sTempRect.left -= rootView.getHeight() / 2;
            } else {
                sTempRect.left -= rootView.getHeight();
                sTempRect.right += rootView.getHeight() / 2;
            }
        }
        final int targetLeft = sTempRect.left;
        final int targetWidth = sTempRect.width();
        final float deltaWidth = lp.width - targetWidth;
        final float deltaLeft = lp.leftMargin - targetLeft;

        if (deltaLeft == 0f && deltaWidth == 0f) {
            // no change needed
        } else if (currentAlpha == 0f) {
            // change selector to the proper width and marginLeft without animation.
            lp.width = targetWidth;
            lp.leftMargin = targetLeft;
            selectorView.requestLayout();
        } else {
            // animate the selector to the proper width and marginLeft.
            layoutAnimator = ValueAnimator.ofFloat(0f, 1f);
            layoutAnimator.setDuration(animationDuration);
            layoutAnimator.setInterpolator(interpolator);

            layoutAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    // Set width to the proper width for this animation step.
                    float fractionToEnd = 1f - valueAnimator.getAnimatedFraction();
                    lp.leftMargin = Math.round(targetLeft + deltaLeft * fractionToEnd);
                    lp.width = Math.round(targetWidth + deltaWidth * fractionToEnd);
                    selectorView.requestLayout();
                }
            });
            layoutAnimator.start();
        }
        return layoutAnimator;

    }
}

From source file:com.rbware.github.androidcouchpotato.widget.AbstractMediaItemPresenter.java

/**
 * Each media item row can have multiple focusable elements; the details on the left and a set
 * of optional custom actions on the right.
 * The selector is a highlight that moves to highlight to cover whichever views is in focus.
 *
 * @param selectorView the selector view used to highlight an individual element within a row.
 * @param focusChangedView The component within the media row whose focus got changed.
 * @param layoutAnimator the ValueAnimator producing animation frames for the selector's width
 *                       and x-translation, generated by this method and stored for the each
 *                       {@link ViewHolder}.
 * @param isDetails Whether the changed-focused view is for a media item details (true) or
 *                  an action (false).//from ww w  .  ja va  2 s .  c  o  m
 */
static ValueAnimator updateSelector(final View selectorView, View focusChangedView,
        ValueAnimator layoutAnimator, boolean isDetails) {
    int animationDuration = focusChangedView.getContext().getResources()
            .getInteger(android.R.integer.config_shortAnimTime);
    DecelerateInterpolator interpolator = new DecelerateInterpolator();

    int layoutDirection = ViewCompat.getLayoutDirection(selectorView);
    if (!focusChangedView.hasFocus()) {
        // if neither of the details or action views are in focus (ie. another row is in focus),
        // animate the selector out.
        selectorView.animate().cancel();
        selectorView.animate().alpha(0f).setDuration(animationDuration).setInterpolator(interpolator).start();
        // keep existing layout animator
        return layoutAnimator;
    } else {
        // cancel existing layout animator
        if (layoutAnimator != null) {
            layoutAnimator.cancel();
            layoutAnimator = null;
        }
        float currentAlpha = selectorView.getAlpha();
        selectorView.animate().alpha(1f).setDuration(animationDuration).setInterpolator(interpolator).start();

        final ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) selectorView.getLayoutParams();
        ViewGroup rootView = (ViewGroup) selectorView.getParent();
        sTempRect.set(0, 0, focusChangedView.getWidth(), focusChangedView.getHeight());
        rootView.offsetDescendantRectToMyCoords(focusChangedView, sTempRect);
        if (isDetails) {
            if (layoutDirection == View.LAYOUT_DIRECTION_RTL) {
                sTempRect.right += rootView.getHeight();
                sTempRect.left -= rootView.getHeight() / 2;
            } else {
                sTempRect.left -= rootView.getHeight();
                sTempRect.right += rootView.getHeight() / 2;
            }
        }
        final int targetLeft = sTempRect.left;
        final int targetWidth = sTempRect.width();
        final float deltaWidth = lp.width - targetWidth;
        final float deltaLeft = lp.leftMargin - targetLeft;

        if (deltaLeft == 0f && deltaWidth == 0f) {
            // no change needed
        } else if (currentAlpha == 0f) {
            // change selector to the proper width and marginLeft without animation.
            lp.width = targetWidth;
            lp.leftMargin = targetLeft;
            selectorView.requestLayout();
        } else {
            // animate the selector to the proper width and marginLeft.
            layoutAnimator = ValueAnimator.ofFloat(0f, 1f);
            layoutAnimator.setDuration(animationDuration);
            layoutAnimator.setInterpolator(interpolator);

            layoutAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    // Set width to the proper width for this animation step.
                    float fractionToEnd = 1f - valueAnimator.getAnimatedFraction();
                    lp.leftMargin = Math.round(targetLeft + deltaLeft * fractionToEnd);
                    lp.width = Math.round(targetWidth + deltaWidth * fractionToEnd);
                    selectorView.requestLayout();
                }
            });
            layoutAnimator.start();
        }
        return layoutAnimator;

    }
}

From source file:im.ene.ribbon.TabletActionTabView.java

@Override
protected void onStatusChanged(final boolean expanded, final int size, final boolean animate) {
    if (!animate) {
        updateLayoutOnAnimation(1, expanded);
        return;/*from www.j a  v  a2  s  .c  o m*/
    }

    final ValueAnimator animator = ObjectAnimator.ofFloat(0, 1);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(final ValueAnimator animation) {
            updateLayoutOnAnimation(animation.getAnimatedFraction(), expanded);
        }
    });
    animator.setDuration(animationDuration);
    animator.setInterpolator(interpolator);
    animator.start();
}

From source file:com.savvasdalkitsis.betwixt.demo.InterpolatorView.java

private void init() {
    paint = new Paint();
    paint.setColor(Color.BLACK);//w w w. j  a  va2  s  . c  o  m
    paint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.interpolation_width));
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setAntiAlias(true);
    paint.setStrokeCap(Paint.Cap.ROUND);
    linePaint.setColor(Color.WHITE);
    linePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.interpolation_width));
    float dashSize = getResources().getDimensionPixelSize(R.dimen.dash_size);
    linePaint.setPathEffect(new DashPathEffect(new float[] { dashSize, dashSize }, 0));
    linePaint.setStyle(Paint.Style.STROKE);
    rectPaint = new Paint();
    rectPaint.setARGB(255, 255, 200, 200);
    radius = getResources().getDimensionPixelSize(R.dimen.circle_radius);
    animationInset = getResources().getDimensionPixelSize(R.dimen.play_inset);
    circlePaint.setColor(Color.RED);
    circlePaint.setAntiAlias(true);
    textPaint.setColor(Color.BLACK);
    textPaint.setAntiAlias(true);
    textPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.interpolator_description_size));
    textPaddingLeft = getResources().getDimensionPixelSize(R.dimen.text_padding_left);
    dim.setColor(ColorUtils.setAlphaComponent(Color.BLACK, 200));
    setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            play();
        }
    });
    playAnimator.setStartDelay(500);
    playAnimator.setDuration(1000);
    playAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            progress = animation.getAnimatedFraction();
            postInvalidate();
        }
    });
    playAnimator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationCancel(Animator animation) {
            endAnimation();
        }

        @Override
        public void onAnimationEnd(final Animator animation) {
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    endAnimation();
                }
            }, 500);
        }
    });
}

From source file:im.ene.ribbon.FixedActionTabView.java

@Override
protected void onStatusChanged(final boolean expanded, final int size, final boolean animate) {
    if (!animate) {
        updateLayoutOnAnimation(1, expanded);
        setIconTranslation(expanded ? 0 : (paddingTopInactive - paddingTopActive));
        return;//from   w  w  w.  j  av  a 2 s.c  o  m
    }

    final AnimatorSet set = new AnimatorSet();
    set.setDuration(animationDuration);
    set.setInterpolator(interpolator);

    final ValueAnimator textScaleAnimator = ObjectAnimator.ofFloat(this, "textScale",
            expanded ? TEXT_SCALE_ACTIVE : 1);

    textScaleAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(final ValueAnimator animation) {
            final float fraction = animation.getAnimatedFraction();
            updateLayoutOnAnimation(fraction, expanded);
        }
    });

    final ValueAnimator iconTranslationAnimator = ObjectAnimator.ofFloat(this, "iconTranslation",
            expanded ? 0 : (paddingTopInactive - paddingTopActive));

    set.playTogether(textScaleAnimator, iconTranslationAnimator);
    set.start();
}

From source file:net.margaritov.preference.colorpicker.ColorPickerDialog.java

private ValueAnimator createColorTransitionAnimator(float start, float end) {
    ValueAnimator animator = ValueAnimator.ofFloat(start, end);

    animator.setDuration(500);//ww w .j av a 2 s .  c om
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float position = animation.getAnimatedFraction();
            if (mIsPanelButtons) {
                int[] blended = new int[8];
                for (int i = 0; i < mPanelViewButtons.length; i++) {
                    blended[i] = blendColors(mPanelViewButtons[i].getColor(), mPaletteColors[mPalette][i],
                            position);
                    mPanelViewButtons[i].setColor(blended[i]);
                }
            } else {
                int blended = blendColors(mNewColor.getColor(), mNewColorValue, position);
                mNewColor.setColor(blended);
            }
        }
    });
    animator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            if (!mIsPanelButtons) {
                mIsPanelButtons = true;
            }
        }
    });
    return animator;
}