Example usage for android.animation AnimatorSet setDuration

List of usage examples for android.animation AnimatorSet setDuration

Introduction

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

Prototype

@Override
public AnimatorSet setDuration(long duration) 

Source Link

Document

Sets the length of each of the current child animations of this AnimatorSet.

Usage

From source file:com.btmura.android.reddit.app.AbstractBrowserActivity.java

private AnimatorSet newExpandLeftAnimator() {
    ObjectAnimator slTransX = ObjectAnimator.ofFloat(leftContainer, "translationX", -leftWidth, 0);
    ObjectAnimator tlTransX = ObjectAnimator.ofFloat(rightContainer, "translationX", -leftWidth, 0);

    AnimatorSet as = new AnimatorSet();
    as.setDuration(durationMs).play(slTransX).with(tlTransX);
    as.addListener(new AnimatorListenerAdapter() {
        @Override//from w  w w . j  av  a  2  s  .c  o m
        public void onAnimationStart(Animator animation) {
            leftContainer.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            leftContainer.setVisibility(View.VISIBLE);
            rightContainer.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            rightContainer.setVisibility(View.VISIBLE);
            thingContainer.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            leftContainer.setLayerType(View.LAYER_TYPE_NONE, null);
            rightContainer.setLayerType(View.LAYER_TYPE_NONE, null);
        }
    });
    return as;
}

From source file:com.ferdi2005.secondgram.AndroidUtilities.java

public static void shakeView(final View view, final float x, final int num) {
    if (num == 6) {
        view.setTranslationX(0);//from w ww.  jav a2  s .c  o  m
        return;
    }
    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playTogether(ObjectAnimator.ofFloat(view, "translationX", AndroidUtilities.dp(x)));
    animatorSet.setDuration(50);
    animatorSet.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            shakeView(view, num == 5 ? 0 : -x, num + 1);
        }
    });
    animatorSet.start();
}

From source file:com.btmura.android.reddit.app.AbstractBrowserActivity.java

private AnimatorSet newCloseNavAnimator() {
    ObjectAnimator ncTransX = ObjectAnimator.ofFloat(navContainer, "translationX", 0, -leftWidth);

    AnimatorSet as = new AnimatorSet();
    as.setDuration(durationMs).play(ncTransX);
    as.addListener(new AnimatorListenerAdapter() {
        @Override/*from  w  ww .  j  a v  a  2 s .c om*/
        public void onAnimationStart(Animator animation) {
            navContainer.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            navContainer.setVisibility(View.VISIBLE);
            thingContainer.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            thingContainer.setVisibility(View.GONE);
            thingContainer.setTranslationX(0);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            navContainer.setLayerType(View.LAYER_TYPE_NONE, null);
            navContainer.setVisibility(View.GONE);
            thingContainer.setLayerType(View.LAYER_TYPE_NONE, null);
            thingContainer.setVisibility(View.VISIBLE);
        }
    });
    return as;
}

From source file:com.btmura.android.reddit.app.AbstractBrowserActivity.java

private AnimatorSet newCollapseLeftAnimator() {
    ObjectAnimator slTransX = ObjectAnimator.ofFloat(leftContainer, "translationX", 0, -leftWidth);
    ObjectAnimator tlTransX = ObjectAnimator.ofFloat(rightContainer, "translationX", 0, -leftWidth);

    AnimatorSet as = new AnimatorSet();
    as.setDuration(durationMs).play(slTransX).with(tlTransX);
    as.addListener(new AnimatorListenerAdapter() {
        @Override//w  w  w.j  a  v  a 2s  .com
        public void onAnimationStart(Animator animation) {
            leftContainer.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            leftContainer.setVisibility(View.VISIBLE);
            rightContainer.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            rightContainer.setVisibility(View.VISIBLE);
            thingContainer.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            leftContainer.setLayerType(View.LAYER_TYPE_NONE, null);
            leftContainer.setVisibility(View.GONE);
            rightContainer.setLayerType(View.LAYER_TYPE_NONE, null);
            rightContainer.setTranslationX(0);
            thingContainer.setVisibility(View.VISIBLE);
        }
    });
    return as;
}

From source file:com.hamzahrmalik.calculator2.Calculator.java

@Override
public void onTextSizeChanged(final TextView textView, float oldSize) {
    if (mCurrentState != CalculatorState.INPUT) {
        // Only animate text changes that occur from user input.
        return;//ww w  .  j av a 2 s.c  o  m
    }

    // Calculate the values needed to perform the scale and translation
    // animations,
    // maintaining the same apparent baseline for the displayed text.
    final float textScale = oldSize / textView.getTextSize();
    final float translationX = (1.0f - textScale) * (textView.getWidth() / 2.0f - textView.getPaddingEnd());
    final float translationY = (1.0f - textScale) * (textView.getHeight() / 2.0f - textView.getPaddingBottom());

    final AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playTogether(ObjectAnimator.ofFloat(textView, View.SCALE_X, textScale, 1.0f),
            ObjectAnimator.ofFloat(textView, View.SCALE_Y, textScale, 1.0f),
            ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, translationX, 0.0f),
            ObjectAnimator.ofFloat(textView, View.TRANSLATION_Y, translationY, 0.0f));
    animatorSet.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
    animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
    animatorSet.start();
}

From source file:eu.davidea.flexibleadapter.FlexibleAnimatorAdapter.java

/**
 * Animates the view based on the custom animator list built with {@link #getAnimators(View, int, boolean)}.
 */// ww w  . j a  v  a  2 s.  c o m
public final void animateView(final View itemView, int position, boolean isSelected) {
    //FIXME: first completed visible item on rotation gets high delay

    //      if (DEBUG)
    //         Log.v(TAG, "shouldAnimate=" + shouldAnimate
    //               + " isFastScroll=" + isFastScroll
    //               + " isNotified=" + mAnimatorNotifierObserver.isPositionNotified()
    //               + " isReverseEnabled=" + isReverseEnabled
    //               + (!isReverseEnabled ? " Pos>AniPos=" + (position > mLastAnimatedPosition) : "")
    //         );

    if (shouldAnimate && !isFastScroll && !mAnimatorNotifierObserver.isPositionNotified()
            && (isReverseEnabled || (!isReverseEnabled && position > mLastAnimatedPosition))) {

        //Cancel animation is necessary when fling
        cancelExistingAnimation(itemView);

        //Retrieve user animators
        List<Animator> animators = getAnimators(itemView, position, isSelected);

        //Add Alpha animator if not yet
        ViewCompat.setAlpha(itemView, 0);
        if (!animatorsUsed.contains(AnimatorEnum.ALPHA))
            addAlphaAnimator(animators, itemView, 0f);
        //Clear animators since the new item might have different animations
        animatorsUsed.clear();

        //Execute the animations all together
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animators);
        //TODO: Animate with Solution 1 or 2?
        //set.setStartDelay(calculateAnimationDelay1(position));
        set.setStartDelay(calculateAnimationDelay2(position));
        set.setInterpolator(mInterpolator);
        set.setDuration(mDuration);
        set.addListener(new HelperAnimatorListener(itemView.hashCode()));
        set.start();
        if (DEBUG)
            Log.v(TAG, "Started Animation on position " + position + " animatorsUsed=" + animatorsUsed);
        mAnimators.put(itemView.hashCode(), set);
    }

    if (mAnimatorNotifierObserver.isPositionNotified())
        mAnimatorNotifierObserver.clearNotified();

    mLastAnimatedPosition = position;
}

From source file:com.example.imac.animationsdemo.ZoomActivity.java

/**
 * "Zooms" in a thumbnail view by assigning the high resolution image to a hidden "zoomed-in"
 * image view and animating its bounds to fit the entire activity content area. More
 * specifically://from  w  w  w .  ja  v  a 2 s.c o  m
 * <p/>
 * <ol>
 * <li>Assign the high-res image to the hidden "zoomed-in" (expanded) image view.</li>
 * <li>Calculate the starting and ending bounds for the expanded view.</li>
 * <li>Animate each of four positioning/sizing properties (X, Y, SCALE_X, SCALE_Y)
 * simultaneously, from the starting bounds to the ending bounds.</li>
 * <li>Zoom back out by running the reverse animation on click.</li>
 * </ol>
 *
 * @param thumbView  The thumbnail view to zoom in.
 * @param imageResId The high-resolution version of the image represented by the thumbnail.
 */
private void zoomImageFromThumb(final View thumbView, int imageResId) {
    // If there's an animation in progress, cancel it immediately and proceed with this one.
    if (mCurrentAnimator != null) {
        mCurrentAnimator.cancel();
    }

    // Load the high-resolution "zoomed-in" image.
    final ImageView expandedImageView = (ImageView) findViewById(R.id.expanded_image);
    expandedImageView.setImageResource(imageResId);

    // Calculate the starting and ending bounds for the zoomed-in image. This step
    // involves lots of math. Yay, math.
    final Rect startBounds = new Rect();
    final Rect finalBounds = new Rect();
    final Point globalOffset = new Point();

    // The start bounds are the global visible rectangle of the thumbnail, and the
    // final bounds are the global visible rectangle of the container view. Also
    // set the container view's offset as the origin for the bounds, since that's
    // the origin for the positioning animation properties (X, Y).
    thumbView.getGlobalVisibleRect(startBounds); //?
    findViewById(R.id.container).getGlobalVisibleRect(finalBounds, globalOffset);
    startBounds.offset(-globalOffset.x, -globalOffset.y);
    finalBounds.offset(-globalOffset.x, -globalOffset.y);
    // Adjust the start bounds to be the same aspect ratio as the final bounds using the
    // "center crop" technique. This prevents undesirable stretching during the animation.
    // Also calculate the start scaling factor (the end scaling factor is always 1.0).
    float startScale;
    if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds.width()
            / startBounds.height()) {
        // Extend start bounds horizontally
        startScale = (float) startBounds.height() / finalBounds.height();
        float startWidth = startScale * finalBounds.width();
        float deltaWidth = (startWidth - startBounds.width()) / 2;
        startBounds.left -= deltaWidth;
        startBounds.right += deltaWidth;
    } else {
        // Extend start bounds vertically
        startScale = (float) startBounds.width() / finalBounds.width();
        float startHeight = startScale * finalBounds.height(); //??
        float deltaHeight = (startHeight - startBounds.height()) / 2; //? ??? 2   ?
        startBounds.top -= deltaHeight; //
        startBounds.bottom += deltaHeight; // 
    }

    // Hide the thumbnail and show the zoomed-in view. When the animation begins,
    // it will position the zoomed-in view in the place of the thumbnail.
    thumbView.setAlpha(0f);
    expandedImageView.setVisibility(View.VISIBLE);

    // Set the pivot point for SCALE_X and SCALE_Y transformations to the top-left corner of
    // the zoomed-in view (the default is the center of the view).
    expandedImageView.setPivotX(0f);
    expandedImageView.setPivotY(0f);

    // Construct and run the parallel animation of the four translation and scale properties
    // (X, Y, SCALE_X, and SCALE_Y).
    Log.e("==startBounds===", startBounds.left + "   " + startBounds.top);
    AnimatorSet set = new AnimatorSet();
    Log.e("=====", startBounds.left + "  " + startBounds.top + "    " + thumbView.getLeft() + "    "
            + thumbView.getTop());
    set.play(ObjectAnimator.ofFloat(expandedImageView, "X", startBounds.left, finalBounds.left))
            .with(ObjectAnimator.ofFloat(expandedImageView, "Y", startBounds.top, finalBounds.top))
            .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScale, 1f))
            .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScale, 1f));
    set.setDuration(mShortAnimationDuration);
    set.setInterpolator(new DecelerateInterpolator());
    set.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            mCurrentAnimator = null;
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            mCurrentAnimator = null;
        }
    });
    set.start();
    mCurrentAnimator = set;

    // Upon clicking the zoomed-in image, it should zoom back down to the original bounds
    // and show the thumbnail instead of the expanded image.
    final float startScaleFinal = startScale;
    expandedImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mCurrentAnimator != null) {
                mCurrentAnimator.cancel();
            }

            // Animate the four positioning/sizing properties in parallel, back to their
            // original values.
            AnimatorSet set = new AnimatorSet();
            set.play(ObjectAnimator.ofFloat(expandedImageView, View.X, startBounds.left))
                    .with(ObjectAnimator.ofFloat(expandedImageView, View.Y, startBounds.top))
                    .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScaleFinal))
                    .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScaleFinal));
            set.setDuration(mShortAnimationDuration);
            set.setInterpolator(new DecelerateInterpolator());
            set.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    thumbView.setAlpha(1f);
                    //                        expandedImageView.setVisibility(View.GONE);
                    mCurrentAnimator = null;
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    thumbView.setAlpha(1f);
                    //                        expandedImageView.setVisibility(View.GONE);
                    mCurrentAnimator = null;
                }
            });
            set.start();
            mCurrentAnimator = set;
        }
    });
}

From source file:eu.davidea.flexibleadapter.AnimatorAdapter.java

/**
 * Animates the view based on the custom animator list built with {@link #getAnimators(View, int, boolean)}.
 *
 * @since 5.0.0-b1//from  w  ww .  ja v a 2s  .co m
 * @deprecated New system in place. Implement {@link FlexibleViewHolder#scrollAnimators(List, int, boolean)}
 * and add new animator(s) to the list of {@code animators}.
 */
@Deprecated
public final void animateView(final View itemView, int position) {
    //      if (DEBUG)
    //         Log.v(TAG, "shouldAnimate=" + shouldAnimate
    //               + " isFastScroll=" + isFastScroll
    //               + " isNotified=" + mAnimatorNotifierObserver.isPositionNotified()
    //               + " isReverseEnabled=" + isReverseEnabled
    //               + " mLastAnimatedPosition=" + mLastAnimatedPosition
    //               + (!isReverseEnabled ? " Pos>AniPos=" + (position > mLastAnimatedPosition) : "")
    //         );

    if (shouldAnimate && !isFastScroll && !mAnimatorNotifierObserver.isPositionNotified() && (isReverseEnabled
            || position > mLastAnimatedPosition || (position == 0 && mRecyclerView.getChildCount() == 0))) {

        //Cancel animation is necessary when fling
        cancelExistingAnimation(itemView.hashCode());

        //Retrieve user animators
        List<Animator> animators = getAnimators(itemView, position, position > mLastAnimatedPosition);

        //Add Alpha animator
        ViewCompat.setAlpha(itemView, 0);
        animators.add(ObjectAnimator.ofFloat(itemView, "alpha", 0f, 1f));
        if (DEBUG)
            Log.d(TAG, "Started Deprecated Animation on position " + position);

        //Execute the animations
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animators);
        set.setInterpolator(mInterpolator);
        set.setDuration(mDuration);
        set.addListener(new HelperAnimatorListener(itemView.hashCode()));
        if (mEntryStep) {
            //set.setStartDelay(calculateAnimationDelay1(position));
            set.setStartDelay(calculateAnimationDelay2(position));
        }
        set.start();
        mAnimators.put(itemView.hashCode(), set);

        //Animate only during initial loading?
        if (onlyEntryAnimation && mLastAnimatedPosition >= mMaxChildViews) {
            shouldAnimate = false;
        }
    }

    mAnimatorNotifierObserver.clearNotified();
    mLastAnimatedPosition = position;
}

From source file:eu.davidea.flexibleadapter.AnimatorAdapter.java

protected void animateView(final RecyclerView.ViewHolder holder, final int position) {
    //FIXME: first completed visible item on rotation gets high delay

    //      if (DEBUG)
    //         Log.v(TAG, "shouldAnimate=" + shouldAnimate
    //               + " isFastScroll=" + isFastScroll
    //               + " isNotified=" + mAnimatorNotifierObserver.isPositionNotified()
    //               + " isReverseEnabled=" + isReverseEnabled
    //               + " mLastAnimatedPosition=" + mLastAnimatedPosition
    //               + (!isReverseEnabled ? " Pos>AniPos=" + (position > mLastAnimatedPosition) : "")
    //         );

    if (holder instanceof FlexibleViewHolder && shouldAnimate && !isFastScroll
            && !mAnimatorNotifierObserver.isPositionNotified()
            && (isReverseEnabled || position > mLastAnimatedPosition
                    || (position == 0 && mRecyclerView.getChildCount() == 0))) {

        //Cancel animation is necessary when fling
        int hashCode = holder.itemView.hashCode();
        cancelExistingAnimation(hashCode);

        //User animators
        List<Animator> animators = new ArrayList<>();
        FlexibleViewHolder flexibleViewHolder = (FlexibleViewHolder) holder;
        flexibleViewHolder.scrollAnimators(animators, position, position > mLastAnimatedPosition);

        //Execute the animations together
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animators);/*from  w ww.java2s  .  c o  m*/
        set.setInterpolator(mInterpolator);
        set.setDuration(mDuration);
        set.addListener(new HelperAnimatorListener(hashCode));
        if (mEntryStep) {
            //Stop stepDelay when screen is filled
            set.setStartDelay(calculateAnimationDelay2(position));
        }
        set.start();
        mAnimators.put(hashCode, set);
        if (DEBUG)
            Log.d(TAG, "Started Animation on position " + position);

        //Animate only during initial loading?
        if (onlyEntryAnimation && position >= mMaxChildViews) {
            shouldAnimate = false;
        }
    }

    mAnimatorNotifierObserver.clearNotified();
    mLastAnimatedPosition = position;
}

From source file:com.flexible.flexibleadapter.AnimatorAdapter.java

/**
 * Animates the view based on the custom animator list built with {@link #getAnimators(View, int, boolean)}.
 *
 * @since 5.0.0-b1/*from w  ww.j a va 2s.co m*/
 * @deprecated New system in place. Implement {@link FlexibleViewHolder#scrollAnimators(List, int, boolean)}
 * and add new animator(s) to the list of {@code animators}.
 */
@Deprecated
public final void animateView(final View itemView, int position) {
    //      if (DEBUG)
    //         Log.v(TAG, "shouldAnimate=" + shouldAnimate
    //               + " isFastScroll=" + isFastScroll
    //               + " isNotified=" + mAnimatorNotifierObserver.isPositionNotified()
    //               + " isReverseEnabled=" + isReverseEnabled
    //               + " mLastAnimatedPosition=" + mLastAnimatedPosition
    //               + (!isReverseEnabled ? " Pos>AniPos=" + (position > mLastAnimatedPosition) : "")
    //         );

    if (shouldAnimate && !isFastScroll && !mAnimatorNotifierObserver.isPositionNotified() && (isReverseEnabled
            || position > mLastAnimatedPosition || (position == 0 && mRecyclerView.getChildCount() == 0))) {

        //Cancel animation is necessary when fling
        cancelExistingAnimation(itemView.hashCode());

        //Retrieve user animators
        List<Animator> animators = getAnimators(itemView, position, position > mLastAnimatedPosition);

        //Add Alpha animator
        ViewCompat.setAlpha(itemView, 0);
        animators.add(ObjectAnimator.ofFloat(itemView, "alpha", 0f, 1f));
        Log.w(TAG, "Started Deprecated Animation on position " + position);

        //Execute the animations
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animators);
        set.setInterpolator(mInterpolator);
        set.setDuration(mDuration);
        set.addListener(new HelperAnimatorListener(itemView.hashCode()));
        if (mEntryStep) {
            set.setStartDelay(calculateAnimationDelay(position));
        }
        set.start();
        mAnimators.put(itemView.hashCode(), set);

        //Animate only during initial loading?
        if (onlyEntryAnimation && mLastAnimatedPosition >= mMaxChildViews) {
            shouldAnimate = false;
        }
    }

    mAnimatorNotifierObserver.clearNotified();
    mLastAnimatedPosition = position;
}