Example usage for android.animation AnimatorSet setInterpolator

List of usage examples for android.animation AnimatorSet setInterpolator

Introduction

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

Prototype

@Override
public void setInterpolator(TimeInterpolator interpolator) 

Source Link

Document

Sets the TimeInterpolator for all current #getChildAnimations() child animations of this AnimatorSet.

Usage

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

/**
 * Animates the view based on the custom animator list built with {@link #getAnimators(View, int, boolean)}.
 *//*from  w w 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.msn.support.gallery.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:/* w ww. j  av  a2  s . c om*/
 * <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>
 * ??ImageView?
 * Activity
 * <ol>
 *     <li>???ImageView</li>
 *     <li>?ImageView?</li>
 *     <li>??ImageView?/?X, Y, SCALE_X, SCALE_Y
 *     ??</li>
 *     <li>???</li>
 * @param thumbView  The thumbnail view to zoom in. 
 * @param imageResId The high-resolution version of the image represented by the thumbnail.
 *                   
 */
@TargetApi(11)
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.?ImageView
    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.
    //?ImageView??
    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);
    Log.e("Test", "globalOffset.x=" + globalOffset.x + " globalOffset.y=" + globalOffset.y);
    Log.e("Test", "startBounds.top=" + startBounds.top + " startBounds.left=" + startBounds.left);
    startBounds.offset(-globalOffset.x, -globalOffset.y);
    Log.e("Test", "startBounds2.top=" + startBounds.top + " startBounds2.left=" + startBounds.left);
    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).
    // ??"center crop"???
    // ????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;
        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).
    AnimatorSet set = new AnimatorSet();
    set.play(ObjectAnimator.ofFloat(expandedImageView, View.X, startBounds.left, finalBounds.left))
            .with(ObjectAnimator.ofFloat(expandedImageView, View.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:org.chromium.chrome.browser.ntp.cards.NewTabPageRecyclerView.java

/**
 * Animates the card being swiped to the right as if the user had dismissed it. Any changes to
 * the animation here should be reflected also in
 * {@link #updateViewStateForDismiss(float, ViewHolder)} and reset in
 * {@link CardViewHolder#onBindViewHolder()}.
 * @param suggestion The item to be dismissed.
 *//*w w w .ja  v  a  2s  .co m*/
public void dismissItemWithAnimation(SnippetArticle suggestion) {
    // We need to recompute the position, as it might have changed.
    final int position = getNewTabPageAdapter().getSuggestionPosition(suggestion);
    if (position == RecyclerView.NO_POSITION) {
        // The item does not exist anymore, so ignore.
        return;
    }

    final View itemView = mLayoutManager.findViewByPosition(position);
    if (itemView == null) {
        // The view is not visible anymore, skip the animation.
        getNewTabPageAdapter().dismissItem(position);
        return;
    }

    final ViewHolder viewHolder = getChildViewHolder(itemView);
    if (!((NewTabPageViewHolder) viewHolder).isDismissable()) {
        // The item is not dismissable (anymore), so ignore.
        return;
    }

    AnimatorSet animation = new AnimatorSet();
    animation.playTogether(ObjectAnimator.ofFloat(itemView, View.ALPHA, 0f),
            ObjectAnimator.ofFloat(itemView, View.TRANSLATION_X, (float) itemView.getWidth()));

    animation.setDuration(DISMISS_ANIMATION_TIME_MS);
    animation.setInterpolator(DISMISS_INTERPOLATOR);
    animation.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationStart(Animator animation) {
            NewTabPageRecyclerView.this.onItemDismissStarted(viewHolder);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            getNewTabPageAdapter().dismissItem(position);
            NewTabPageRecyclerView.this.onItemDismissFinished(viewHolder);
        }
    });
    animation.start();
}

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/* www.j a  v a2s  .c o 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);//www. j av a 2  s  .  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   ww w .ja v  a  2  s . c  om*/
 * @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;
}

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

/**
 * Performs checks to scroll animate the itemView and in case, it animates the view.
 * <p><b>Note:</b> If you have to change at runtime the LayoutManager <i>and</i> add
 * Scrollable Headers too, consider to add them in post, using a {@code delay >= 0},
 * otherwise scroll animations on all items will not start correctly.</p>
 *
 * @param holder   the ViewHolder just bound
 * @param position the current item position
 *///from  ww  w .j  a  va 2 s .  c o m
@SuppressWarnings("ConstantConditions")
protected final void animateView(final RecyclerView.ViewHolder holder, final int position) {
    if (mRecyclerView == null)
        return;

    // Use always the max child count reached
    if (mMaxChildViews < mRecyclerView.getChildCount()) {
        mMaxChildViews = mRecyclerView.getChildCount();
    }
    // Animate only during initial loading?
    if (onlyEntryAnimation && mLastAnimatedPosition >= mMaxChildViews) {
        shouldAnimate = false;
    }
    int lastVisiblePosition = Utils.findLastVisibleItemPosition(mRecyclerView.getLayoutManager());
    //      if (DEBUG) {
    //         Log.v(TAG, "shouldAnimate=" + shouldAnimate
    //               + " isFastScroll=" + isFastScroll
    //               + " isNotified=" + mAnimatorNotifierObserver.isPositionNotified()
    //               + " isReverseEnabled=" + isReverseEnabled
    //               + " mLastAnimatedPosition=" + mLastAnimatedPosition
    //               + (!isReverseEnabled ? " Pos>LasVisPos=" + (position > lastVisiblePosition) : "")
    //               + " mMaxChildViews=" + mMaxChildViews
    //         );
    //      }
    if (holder instanceof FlexibleViewHolder && shouldAnimate && !isFastScroll
            && !mAnimatorNotifierObserver.isPositionNotified()
            && (position > lastVisiblePosition || isReverseEnabled || isScrollableHeaderOrFooter(position)
                    || (position == 0 && mMaxChildViews == 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 >= lastVisiblePosition);

        // Execute the animations together
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animators);
        set.setInterpolator(mInterpolator);
        // Single view duration
        long duration = mDuration;
        for (Animator animator : animators) {
            if (animator.getDuration() != DEFAULT_DURATION) {
                duration = animator.getDuration();
            }
        }
        //Log.v(TAG, "duration=" + duration);
        set.setDuration(duration);
        set.addListener(new HelperAnimatorListener(hashCode));
        if (mEntryStep) {
            // Stop stepDelay when screen is filled
            set.setStartDelay(calculateAnimationDelay(position));
        }
        set.start();
        mAnimators.put(hashCode, set);
        if (DEBUG)
            Log.v(TAG, "animateView    Scroll animation on position " + position);
    }
    mAnimatorNotifierObserver.clearNotified();
    // Update last animated position
    mLastAnimatedPosition = position;
}

From source file:com.example.george.sharedelementimplementation.MainActivity.java

public void runExitAnimation() {
    final long duration = (long) (ANIM_DURATION * MainActivity.sAnimatorScale);
    ObjectAnimator anim = ObjectAnimator.ofInt(mBackground, "alpha", 0);
    anim.addListener(new Animator.AnimatorListener() {
        @Override/*from  w  ww. j a v a  2 s .  c o  m*/
        public void onAnimationStart(Animator animation) {
            // do nothing intended
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            mIsInFullscreen = false;
            mIsAnimationPlaying = false;
            mImage.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            // do nothing intended
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            // do nothing intended
        }
    });

    AnimatorSet set = new AnimatorSet();
    set.playTogether(ObjectAnimator.ofFloat(mImage, "translationX", 0, mXDelta),
            ObjectAnimator.ofFloat(mImage, "translationY", 0, mYDelta),
            ObjectAnimator.ofFloat(mImage, "scaleX", 1, mImageScale),
            ObjectAnimator.ofFloat(mImage, "scaleY", 1, mImageScale),
            //            ObjectAnimator.ofFloat(mImage, "alpha", 1, 0),
            ObjectAnimator.ofFloat(mImage, "imageCrop", 0f, clipRatio), anim);
    set.setInterpolator(sAccelerator);
    set.setDuration(duration).start();
    mPager.setVisibility(View.GONE);
    mIsAnimationPlaying = true;
}

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 ww 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:com.example.george.sharedelementimplementation.MainActivity.java

public void runEnterAnimation() {
    final long duration = (long) (ANIM_DURATION * MainActivity.sAnimatorScale);
    // set starting values for properties we're going to animate. These
    // values scale and position the full size version down to the thumbnail
    // size/location, from which we'll animate it back up
    ObjectAnimator anim = ObjectAnimator.ofInt(mBackground, "alpha", 0, 255);
    anim.addListener(new Animator.AnimatorListener() {
        @Override//www  . java2  s.  c om
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            mIsAnimationPlaying = false;
            mImage.setVisibility(View.GONE);
            mPager.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });

    AnimatorSet set = new AnimatorSet();
    set.playTogether(ObjectAnimator.ofFloat(mImage, "translationX", mXDelta, 0),
            ObjectAnimator.ofFloat(mImage, "translationY", mYDelta, 0),
            ObjectAnimator.ofFloat(mImage, "scaleX", mImageScale, 1),
            ObjectAnimator.ofFloat(mImage, "scaleY", mImageScale, 1),
            //            ObjectAnimator.ofFloat(mImage, "alpha", 0, 1),
            ObjectAnimator.ofFloat(mImage, "imageCrop", clipRatio, 0f), anim);
    set.setInterpolator(sDecelerator);
    set.setDuration(duration).start();
    mIsAnimationPlaying = true;
}