Example usage for android.animation ValueAnimator setDuration

List of usage examples for android.animation ValueAnimator setDuration

Introduction

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

Prototype

@Override
public ValueAnimator setDuration(long duration) 

Source Link

Document

Sets the length of the animation.

Usage

From source file:com.android.nobug.view.pattern.PatternView.java

private void startRadiusAnimation(float start, float end, long duration, Interpolator interpolator,
        final CellState state, final Runnable endRunnable) {
    ValueAnimator valueAnimator = ValueAnimator.ofFloat(start, end);
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override//w  w w.  j a  va2  s .c  om
        public void onAnimationUpdate(ValueAnimator animation) {
            state.radius = (float) animation.getAnimatedValue();
            invalidate();
        }
    });
    if (endRunnable != null) {
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                endRunnable.run();
            }
        });
    }
    valueAnimator.setInterpolator(interpolator);
    valueAnimator.setDuration(duration);
    valueAnimator.start();
}

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  ww  w  .ja  v  a2 s  . c om
 */
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.shalzz.attendance.fragment.AttendanceListFragment.java

@Override
public void onItemExpanded(final View view) {
    final int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    final ExpandableListAdapter.GenericViewHolder viewHolder = (ExpandableListAdapter.GenericViewHolder) view
            .getTag();/*from w  ww  .jav  a  2s  .  c  om*/
    final RelativeLayout childView = viewHolder.childView;
    childView.measure(spec, spec);
    final int startingHeight = view.getHeight();
    final ViewTreeObserver observer = mRecyclerView.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            // We don'mTracker want to continue getting called for every draw.
            if (observer.isAlive()) {
                observer.removeOnPreDrawListener(this);
            }
            // Calculate some values to help with the animation.
            final int endingHeight = view.getHeight();
            final int distance = Math.abs(endingHeight - startingHeight);
            final int baseHeight = Math.min(endingHeight, startingHeight);
            final boolean isExpanded = endingHeight > startingHeight;

            // Set the views back to the start state of the animation
            view.getLayoutParams().height = startingHeight;
            if (!isExpanded) {
                viewHolder.childView.setVisibility(View.VISIBLE);
            }

            // Set up the fade effect for the action buttons.
            if (isExpanded) {
                // Start the fade in after the expansion has partly completed, otherwise it
                // will be mostly over before the expansion completes.
                viewHolder.childView.setAlpha(0f);
                viewHolder.childView.animate().alpha(1f).setStartDelay(mFadeInStartDelay)
                        .setDuration(mFadeInDuration).start();
            } else {
                viewHolder.childView.setAlpha(1f);
                viewHolder.childView.animate().alpha(0f).setDuration(mFadeOutDuration).start();
            }
            view.requestLayout();

            // Set up the animator to animate the expansion and shadow depth.
            ValueAnimator animator = isExpanded ? ValueAnimator.ofFloat(0f, 1f) : ValueAnimator.ofFloat(1f, 0f);

            // scroll to make the view fully visible.
            mRecyclerView.smoothScrollToPosition(viewHolder.position);

            animator.addUpdateListener(animator1 -> {
                Float value = (Float) animator1.getAnimatedValue();

                // For each value from 0 to 1, animate the various parts of the layout.
                view.getLayoutParams().height = (int) (value * distance + baseHeight);
                float z = mExpandedItemTranslationZ * value;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    view.setTranslationZ(z);
                }
                view.requestLayout();
            });

            // Set everything to their final values when the animation's done.
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    view.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;

                    if (!isExpanded) {
                        viewHolder.childView.setVisibility(View.GONE);
                    } else {
                        // This seems like it should be unnecessary, but without this, after
                        // navigating out of the activity and then back, the action view alpha
                        // is defaulting to the value (0) at the start of the expand animation.
                        viewHolder.childView.setAlpha(1);
                    }
                }
            });

            animator.setDuration(mExpandCollapseDuration);
            animator.start();

            // Return false so this draw does not occur to prevent the final frame from
            // being drawn for the single frame before the animations start.
            return false;
        }
    });
}

From source file:com.phonemetra.turbo.launcher.AsyncTaskCallback.java

@Override
public void onClick(View v) {
    // When we have exited all apps or are in transition, disregard clicks
    if (!mLauncher.isAllAppsVisible() || mLauncher.getWorkspace().isSwitchingState())
        return;//www  .j a v a2s . com

    if (v instanceof PagedViewIcon) {
        // Animate some feedback to the click
        final AppInfo appInfo = (AppInfo) v.getTag();

        // Lock the drawable state to pressed until we return to Launcher
        if (mPressedIcon != null) {
            mPressedIcon.lockDrawableState();
        }
        mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
        mLauncher.getStats().recordLaunch(appInfo.intent);
    } else if (v instanceof PagedViewWidget) {
        // Let the user know that they have to long press to add a widget
        if (mWidgetInstructionToast != null) {
            mWidgetInstructionToast.cancel();
        }
        mWidgetInstructionToast = Toast.makeText(getContext(), R.string.long_press_widget_to_add,
                Toast.LENGTH_SHORT);
        mWidgetInstructionToast.show();

        // Create a little animation to show that the widget can move
        float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
        final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
        AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
        ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
        tyuAnim.setDuration(125);
        ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
        tydAnim.setDuration(100);
        bounce.play(tyuAnim).before(tydAnim);
        bounce.setInterpolator(new AccelerateInterpolator());
        bounce.start();
    }
}

From source file:io.authme.sdk.widget.LockPatternView.java

private void startLineEndAnimation(final CellState state, final float startX, final float startY,
        final float targetX, final float targetY) {
    /*/*from  ww  w . j  a  v  a 2  s  .  co  m*/
     * Currently this animation looks unclear, we don't really need it...
     */
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
        return;

    ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float t = (Float) animation.getAnimatedValue();
            state.lineEndX = (1 - t) * startX + t * targetX;
            state.lineEndY = (1 - t) * startY + t * targetY;
            invalidate();
        }

    });
    valueAnimator.addListener(new AnimatorListenerAdapter() {

        @Override
        public void onAnimationEnd(Animator animation) {
            state.lineAnimator = null;
        }

    });
    valueAnimator.setInterpolator(mFastOutSlowInInterpolator);
    valueAnimator.setDuration(100);
    valueAnimator.start();
    state.lineAnimator = valueAnimator;
}

From source file:com.android.nobug.view.pattern.PatternView.java

private void startLineEndAnimation(final CellState state, final float startX, final float startY,
        final float targetX, final float targetY) {
    ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override/*from   w ww .j  a  va2  s. c o  m*/
        public void onAnimationUpdate(ValueAnimator animation) {
            float t = (float) animation.getAnimatedValue();
            state.lineEndX = (1 - t) * startX + t * targetX;
            state.lineEndY = (1 - t) * startY + t * targetY;
            invalidate();
        }
    });
    valueAnimator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            state.lineAnimator = null;
        }
    });
    valueAnimator.setInterpolator(mFastOutSlowInInterpolator);
    valueAnimator.setDuration(100);
    valueAnimator.start();
    state.lineAnimator = valueAnimator;
}

From source file:io.jawg.osmcontributor.ui.fragments.MapFragment.java

@Subscribe(threadMode = ThreadMode.MAIN)
public void onPleaseChangePoiPosition(PleaseChangePoiPosition event) {
    Timber.d("Received event PleaseChangePoiPosition");
    if (configManager.hasPoiModification()) {
        switchMode(MapMode.POI_POSITION_EDITION);
        creationPin.setVisibility(View.GONE);

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,
                OsmAnimatorUpdateListener.STEPS_CENTER_ANIMATION);
        valueAnimator.setDuration(900);
        valueAnimator.addUpdateListener(new OsmAnimatorUpdateListener(mapboxMap.getCameraPosition().target,
                markerSelected.getPosition(), mapboxMap));

        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override//from   w ww. j a v a 2s  .  co  m
            public void onAnimationEnd(Animator animation) {
                creationPin.setVisibility(View.VISIBLE);
                hideMarker(markerSelected);
            }
        });
        valueAnimator.start();
    } else {
        Toast.makeText(getActivity(), getResources().getString(R.string.point_modification_forbidden),
                Toast.LENGTH_SHORT).show();
    }
}

From source file:io.authme.sdk.widget.LockPatternView.java

private void startSizeAnimation(float start, float end, long duration, Interpolator interpolator,
        final CellState state, final Runnable endRunnable) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        FloatAnimator animator = new FloatAnimator(start, end, duration);
        animator.addEventListener(new FloatAnimator.SimpleEventListener() {

            @Override//from w  w w.j  a v  a 2 s.c om
            public void onAnimationUpdate(FloatAnimator animator) {
                state.size = (Float) animator.getAnimatedValue();
                invalidate();
            }// onAnimationUpdate()

            @Override
            public void onAnimationEnd(FloatAnimator animator) {
                if (endRunnable != null)
                    endRunnable.run();
            }// onAnimationEnd()

        });
        animator.start();
    } // API < 11
    else {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(start, end);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                state.size = (Float) animation.getAnimatedValue();
                invalidate();
            }

        });
        if (endRunnable != null) {
            valueAnimator.addListener(new AnimatorListenerAdapter() {

                @Override
                public void onAnimationEnd(Animator animation) {
                    if (endRunnable != null)
                        endRunnable.run();
                }

            });
        }
        valueAnimator.setInterpolator(interpolator);
        valueAnimator.setDuration(duration);
        valueAnimator.start();
    } // API 11+
}

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  w w  w . j a  va2 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:com.arlib.floatingsearchview.FloatingSearchView.java

private void openMenuDrawable(final DrawerArrowDrawable drawerArrowDrawable, boolean withAnim) {
    if (withAnim) {
        ValueAnimator anim = ValueAnimator.ofFloat(0.0f, 1.0f);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override/*  ww w.  ja v a 2  s . c o m*/
            public void onAnimationUpdate(ValueAnimator animation) {

                float value = (Float) animation.getAnimatedValue();
                drawerArrowDrawable.setProgress(value);
            }
        });
        anim.setDuration(MENU_ICON_ANIM_DURATION);
        anim.start();
    } else {
        drawerArrowDrawable.setProgress(1.0f);
    }
}