Example usage for android.animation AnimatorListenerAdapter AnimatorListenerAdapter

List of usage examples for android.animation AnimatorListenerAdapter AnimatorListenerAdapter

Introduction

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

Prototype

AnimatorListenerAdapter

Source Link

Usage

From source file:com.heinrichreimersoftware.materialintro.app.IntroActivity.java

private void smoothScrollPagerTo(final int position) {
    if (miPager.isFakeDragging())
        return;//www. j a v  a2  s.  c  om

    ValueAnimator animator = ValueAnimator.ofFloat(miPager.getCurrentItem(), position);
    animator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            if (miPager.isFakeDragging())
                miPager.endFakeDrag();
            miPager.setCurrentItem(position);
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            if (miPager.isFakeDragging())
                miPager.endFakeDrag();
        }
    });
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float position = (Float) animation.getAnimatedValue();

            fakeDragToPosition(position);
        }

        private boolean fakeDragToPosition(float position) {
            // The following mimics the underlying calculations in ViewPager
            float scrollX = miPager.getScrollX();
            int pagerWidth = miPager.getWidth();
            int currentPosition = miPager.getCurrentItem();

            if (position > currentPosition && Math.floor(position) != currentPosition && position % 1 != 0) {
                miPager.setCurrentItem((int) Math.floor(position), false);
            } else if (position < currentPosition && Math.ceil(position) != currentPosition
                    && position % 1 != 0) {
                miPager.setCurrentItem((int) Math.ceil(position), false);
            }

            if (!miPager.isFakeDragging() && !miPager.beginFakeDrag())
                return false;

            miPager.fakeDragBy(scrollX - pagerWidth * position);
            return true;
        }
    });

    int distance = Math.abs(position - miPager.getCurrentItem());

    animator.setInterpolator(pageScrollInterpolator);
    animator.setDuration(calculateScrollDuration(distance));
    animator.start();
}

From source file:com.shoshin.paidpay.ExpandingListViewPayVia.java

/**
 * This method expands the view that was clicked and animates all the views
 * around it to make room for the expanding view. There are several steps required
 * to do this which are outlined below./* w ww. ja v  a 2  s  . co  m*/
 *
 * 1. Store the current top and bottom bounds of each visible item in the listview.
 * 2. Update the layout parameters of the selected view. In the context of this
 *    method, the view should be originally collapsed and set to some custom height.
 *    The layout parameters are updated so as to wrap the content of the additional
 *    text that is to be displayed.
 *
 * After invoking a layout to take place, the listview will order all the items
 * such that there is space for each view. This layout will be independent of what
 * the bounds of the items were prior to the layout so two pre-draw passes will
 * be made. This is necessary because after the layout takes place, some views that
 * were visible before the layout may now be off bounds but a reference to these
 * views is required so the animation completes as intended.
 *
 * 3. The first predraw pass will set the bounds of all the visible items to
 *    their original location before the layout took place and then force another
 *    layout. Since the bounds of the cells cannot be set directly, the method
 *    setSelectionFromTop can be used to achieve a very similar effect.
 * 4. The expanding view's bounds are animated to what the final values should be
 *    from the original bounds.
 * 5. The bounds above the expanding view are animated upwards while the bounds
 *    below the expanding view are animated downwards.
 * 6. The extra text is faded in as its contents become visible throughout the
 *    animation process.
 *
 * It is important to note that the listview is disabled during the animation
 * because the scrolling behaviour is unpredictable if the bounds of the items
 * within the listview are not constant during the scroll.
 */

private void expandView(final View view) {
    final ExpandableCardsWithOffers viewObject = (ExpandableCardsWithOffers) getItemAtPosition(
            getPositionForView(view));

    /* Store the original top and bottom bounds of all the cells.*/
    final int oldTop = view.getTop();
    final int oldBottom = view.getBottom();

    final HashMap<View, int[]> oldCoordinates = new HashMap<View, int[]>();

    int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View v = getChildAt(i);
        ViewCompat.setHasTransientState(v, true);
        oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() });
    }

    /* Update the layout so the extra content becomes visible.*/
    final View expandingLayout = view.findViewById(R.id.expanding_layout);
    expandingLayout.setVisibility(View.VISIBLE);

    /* Add an onPreDraw Listener to the listview. onPreDraw will get invoked after onLayout
    * and onMeasure have run but before anything has been drawn. This
    * means that the final post layout properties for all the items have already been
    * determined, but still have not been rendered onto the screen.*/
    final ViewTreeObserver observer = getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            /* Determine if this is the first or second pass.*/
            if (!mShouldRemoveObserver) {
                mShouldRemoveObserver = true;

                /* Calculate what the parameters should be for setSelectionFromTop.
                * The ListView must be offset in a way, such that after the animation
                * takes place, all the cells that remain visible are rendered completely
                * by the ListView.*/
                int newTop = view.getTop();
                int newBottom = view.getBottom();

                int newHeight = newBottom - newTop;
                int oldHeight = oldBottom - oldTop;
                int delta = newHeight - oldHeight;

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, delta, true);

                int currentTop = view.getTop();
                int futureTop = oldTop - mTranslate[0];

                int firstChildStartTop = getChildAt(0).getTop();
                int firstVisiblePosition = getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = getChildAt(i);
                    int height = v.getBottom() - Math.max(0, v.getTop());
                    if (deltaTop - height > 0) {
                        firstVisiblePosition++;
                        deltaTop -= height;
                    } else {
                        break;
                    }
                }

                if (i > 0) {
                    firstChildStartTop = 0;
                }

                setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                /* Request another layout to update the layout parameters of the cells.*/
                requestLayout();

                /* Return false such that the ListView does not redraw its contents on
                 * this layout but only updates all the parameters associated with its
                 * children.*/
                return false;
            }

            /* Remove the predraw listener so this method does not keep getting called. */
            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

            int yTranslateTop = mTranslate[0];
            int yTranslateBottom = mTranslate[1];

            ArrayList<Animator> animations = new ArrayList<Animator>();

            int index = indexOfChild(view);

            /* Loop through all the views that were on the screen before the cell was
            *  expanded. Some cells will still be children of the ListView while
            *  others will not. The cells that remain children of the ListView
            *  simply have their bounds animated appropriately. The cells that are no
            *  longer children of the ListView also have their bounds animated, but
            *  must also be added to a list of views which will be drawn in dispatchDraw.*/
            for (View v : oldCoordinates.keySet()) {
                int[] old = oldCoordinates.get(v);
                v.setTop(old[0]);
                v.setBottom(old[1]);
                if (v.getParent() == null) {
                    mViewsToDraw.add(v);
                    int delta = old[0] < oldTop ? -yTranslateTop : yTranslateBottom;
                    animations.add(getAnimation(v, delta, delta));
                } else {
                    int i = indexOfChild(v);
                    if (v != view) {
                        int delta = i > index ? yTranslateBottom : -yTranslateTop;
                        animations.add(getAnimation(v, delta, delta));
                    }
                    ViewCompat.setHasTransientState(v, false);
                }
            }

            /* Adds animation for expanding the cell that was clicked. */
            animations.add(getAnimation(view, -yTranslateTop, yTranslateBottom));

            /* Adds an animation for fading in the extra content. */
            //TODO
            animations.add(ObjectAnimator.ofFloat(view.findViewById(R.id.expanding_layout), View.ALPHA, 0, 1));

            /* Disabled the ListView for the duration of the animation.*/
            setEnabled(false);
            setClickable(false);

            /* Play all the animations created above together at the same time. */
            AnimatorSet s = new AnimatorSet();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    viewObject.setExpanded(true);
                    setEnabled(true);
                    setClickable(true);
                    if (mViewsToDraw.size() > 0) {
                        for (View v : mViewsToDraw) {
                            ViewCompat.setHasTransientState(v, false);
                        }
                    }
                    mViewsToDraw.clear();
                }
            });
            s.start();
            return true;
        }
    });
}

From source file:com.grepsound.activities.MainActivity.java

/**
 * This method animates the image fragment into the foreground by both
 * scaling and rotating the fragment's view, while also removing the
 * previously added translucent dark hover view. Upon the completion of
 * this animation, the image fragment regains focus since this method is
 * called from the onBackStackChanged method.
 */// ww w . j  a v a 2s . c  o m
public void slideForward(Animator.AnimatorListener listener) {
    View movingFragmentView = mMainFrag.getView();

    PropertyValuesHolder rotateX = PropertyValuesHolder.ofFloat("rotationX", 40f);
    PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
    PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
    ObjectAnimator movingFragmentAnimator = ObjectAnimator.ofPropertyValuesHolder(movingFragmentView, rotateX,
            scaleX, scaleY);

    ObjectAnimator darkHoverViewAnimator = ObjectAnimator.ofFloat(mDarkHoverView, "alpha", 0.5f, 0.0f);

    ObjectAnimator movingFragmentRotator = ObjectAnimator.ofFloat(movingFragmentView, "rotationX", 0);
    movingFragmentRotator.setStartDelay(getResources().getInteger(R.integer.half_slide_up_down_duration));

    AnimatorSet s = new AnimatorSet();
    s.playTogether(movingFragmentAnimator, movingFragmentRotator, darkHoverViewAnimator);
    s.setStartDelay(getResources().getInteger(R.integer.slide_up_down_duration));
    s.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            mIsAnimating = false;
        }
    });
    s.start();
}

From source file:com.l4digital.fastscroll.FastScroller.java

private void showScrollbar() {
    if (mRecyclerView.computeVerticalScrollRange() - mHeight > 0) {
        float transX = getResources().getDimensionPixelSize(R.dimen.fastscroll_scrollbar_padding);

        mScrollbar.setTranslationX(transX);
        mScrollbar.setVisibility(VISIBLE);
        mScrollbarAnimator = mScrollbar.animate().translationX(0f).alpha(1f).setDuration(sScrollbarAnimDuration)
                .setListener(new AnimatorListenerAdapter() {
                    // adapter required for new alpha value to stick
                });/*from w w w .  j  av  a  2  s .  co m*/
    }
}

From source file:com.itsronald.widget.IndicatorDotPathView.java

/**
 * Animation: Retreat an indicator dot to a specified position.
 * After the animation, the dot will be invisibly moved back to its original position.
 *
 * @param retreatingDot The dot that should be animated.
 * @param toX The horizontal coordinate to which the dot should move (in its own coordinate space).
 * @param toY The vertical coordinate to which the dot should move (in its own coordinate space).
 * @param animationDuration How long the movement should take, in milliseconds.
 * @return An animator that moves the dot when started.
 */// www .jav  a2 s .c  o m
@NonNull
private Animator retreatDotAnimator(@NonNull final IndicatorDotView retreatingDot, final float toX,
        final float toY, final long animationDuration) {
    final Animator dotSlideAnimator = retreatingDot.slideAnimator(toX, toY, animationDuration);

    final float originalTranslationX = retreatingDot.getTranslationX();
    final float originalTranslationY = retreatingDot.getTranslationY();
    dotSlideAnimator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            retreatingDot.setVisibility(INVISIBLE);
            retreatingDot.setTranslationX(originalTranslationX);
            retreatingDot.setTranslationY(originalTranslationY);
        }
    });

    return dotSlideAnimator;
}

From source file:ch.berta.fabio.fabprogress.FabProgress.java

private void fadeOut(boolean animate, final boolean reverse) {
    if (animate) {
        animate().setDuration(ICON_CHANGE_ANIM_DURATION).alpha(0).scaleX(0).scaleY(0)
                .setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR).setListener(new AnimatorListenerAdapter() {
                    @Override/*  w ww  .  ja v a  2  s  .c  o  m*/
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);

                        if (reverse) {
                            setImageDrawable(mFabIcon);
                            setBackgroundTintList(ColorStateList.valueOf(mAccentColor));
                        } else {
                            setImageDrawable(mCompleteIcon);
                            setBackgroundTintList(ColorStateList.valueOf(mArcColor));
                        }

                        fadeIn(reverse);
                    }
                }).start();
    } else if (reverse) {
        setImageDrawable(mFabIcon);
        setBackgroundTintList(ColorStateList.valueOf(mAccentColor));
    } else {
        setImageDrawable(mCompleteIcon);
        setBackgroundTintList(ColorStateList.valueOf(mArcColor));
    }
}

From source file:com.fada21.android.hydralist.expandable.ExpandingListViewDelegate.java

/**
 * <p>/*  www. ja  v a 2  s .  c  o m*/
 * This method expands the view that was clicked and animates all the views around it to make room for the expanding view. There are several steps required
 * to do this which are outlined below.
 * </p>
 * 
 * <li>Store the current top and bottom bounds of each visible item in the listview.</li> <li>Update the layout parameters of the selected view. In the
 * context of this method, the view should be originally collapsed and set to some custom height. The layout parameters are updated so as to wrap the
 * content of the additional text that is to be displayed.</li>
 * 
 * <br/>
 * <br/>
 * <p>
 * After invoking a layout to take place, the listview will order all the items such that there is space for each view. This layout will be independent of
 * what the bounds of the items were prior to the layout so two pre-draw passes will be made. This is necessary because after the layout takes place, some
 * views that were visible before the layout may now be off bounds but a reference to these views is required so the animation completes as intended.
 * </p>
 * <br/>
 * 
 * <li>The first predraw pass will set the bounds of all the visible items to their original location before the layout took place and then force another
 * layout. Since the bounds of the cells cannot be set directly, the method setSelectionFromTop can be used to achieve a very similar effect.</li> <li>The
 * expanding view's bounds are animated to what the final values should be from the original bounds.</li> <li>The bounds above the expanding view are
 * animated upwards while the bounds below the expanding view are animated downwards.</li> <li>The extra text is faded in as its contents become visible
 * throughout the animation process.</li>
 * 
 * <br/>
 * <br/>
 * <p>
 * It is important to note that the listview is disabled during the animation because the scrolling behavior is unpredictable if the bounds of the items
 * within the listview are not constant during the scroll.
 * </p>
 * <br/>
 * 
 * @param view
 *            expanding view
 * @param position
 *            item position in list
 * @param id
 *            item id
 */
private void expandView(final View view, int position, long id) {
    // final ExpandableListItem item = (ExpandableListItem) nlv.getItemAtPosition(nlv.getPositionForView(view));
    HydraListAdapter<ExpandableListItem> expandingAdapter = getExpandingAdapter();
    final ExpandableListItem item = expandingAdapter.getDataProvider().get(position);
    ExpandingLayout expandingLayout = expandingAdapter.getExpandingHelper().getExpandedView(view, item);

    /* Store the original top and bottom bounds of all the cells. */
    final int oldTop = view.getTop();
    final int oldBottom = view.getBottom();

    final HashMap<View, int[]> oldCoordinates = new HashMap<View, int[]>();

    int childCount = nlv.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View v = nlv.getChildAt(i);
        ViewCompat.setHasTransientState(v, true);
        oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() });
    }

    /* Update the layout so the extra content becomes visible. */
    expandingLayout.setVisibility(View.VISIBLE);

    /*
     * Add an onPreDraw Listener to the listview. onPreDraw will get invoked after onLayout
     * and onMeasure have run but before anything has been drawn. This
     * means that the final post layout properties for all the items have already been
     * determined, but still have not been rendered onto the screen.
     */
    final ViewTreeObserver observer = nlv.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            /* Determine if this is the first or second pass. */
            if (!mShouldRemoveObserver) {
                mShouldRemoveObserver = true;

                /*
                 * Calculate what the parameters should be for setSelectionFromTop.
                 * The ListView must be offset in a way, such that after the animation
                 * takes place, all the cells that remain visible are rendered completely
                 * by the ListView.
                 */
                int newTop = view.getTop();
                int newBottom = view.getBottom();

                int newHeight = newBottom - newTop;
                int oldHeight = oldBottom - oldTop;
                int delta = newHeight - oldHeight;

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, delta, true);

                int currentTop = view.getTop();
                int futureTop = oldTop - mTranslate[0];

                int firstChildStartTop = nlv.getChildAt(0).getTop();
                int firstVisiblePosition = nlv.getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = nlv.getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = nlv.getChildAt(i);
                    int height = v.getBottom() - Math.max(0, v.getTop());
                    if (deltaTop - height > 0) {
                        firstVisiblePosition++;
                        deltaTop -= height;
                    } else {
                        break;
                    }
                }

                if (i > 0) {
                    firstChildStartTop = 0;
                }

                nlv.setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                /* Request another layout to update the layout parameters of the cells. */
                nlv.requestLayout();

                /*
                 * Return false such that the ListView does not redraw its contents on
                 * this layout but only updates all the parameters associated with its
                 * children.
                 */
                return false;
            }

            /* Remove the predraw listener so this method does not keep getting called. */
            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

            int yTranslateTop = mTranslate[0];
            int yTranslateBottom = mTranslate[1];

            ArrayList<Animator> animations = new ArrayList<Animator>();

            int index = nlv.indexOfChild(view);

            /*
             * Loop through all the views that were on the screen before the cell was
             * expanded. Some cells will still be children of the ListView while
             * others will not. The cells that remain children of the ListView
             * simply have their bounds animated appropriately. The cells that are no
             * longer children of the ListView also have their bounds animated, but
             * must also be added to a list of views which will be drawn in dispatchDraw.
             */
            for (View v : oldCoordinates.keySet()) {
                int[] old = oldCoordinates.get(v);
                v.setTop(old[0]);
                v.setBottom(old[1]);
                if (v.getParent() == null) {
                    mViewsToDraw.add(v);
                    int delta = old[0] < oldTop ? -yTranslateTop : yTranslateBottom;
                    animations.add(getAnimation(v, delta, delta));
                } else {
                    int i = nlv.indexOfChild(v);
                    if (v != view) {
                        int delta = i > index ? yTranslateBottom : -yTranslateTop;
                        animations.add(getAnimation(v, delta, delta));
                    }
                    ViewCompat.setHasTransientState(v, false);
                }
            }

            /* Adds animation for expanding the cell that was clicked. */
            animations.add(getAnimation(view, -yTranslateTop, yTranslateBottom));

            /* Adds an animation for fading in the extra content. */
            animations.add(ObjectAnimator.ofFloat(
                    view.findViewById(getExpandingAdapter().getExpandingHelper().getExpandingLayout()),
                    View.ALPHA, 0, 1));

            /* Disabled the ListView for the duration of the animation. */
            nlv.setEnabled(false);
            nlv.setClickable(false);

            /* Play all the animations created above together at the same time. */
            AnimatorSet s = new AnimatorSet();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    item.setExpanded(true);
                    nlv.setEnabled(true);
                    nlv.setClickable(true);
                    if (mViewsToDraw.size() > 0) {
                        for (View v : mViewsToDraw) {
                            ViewCompat.setHasTransientState(v, false);
                        }
                    }
                    mViewsToDraw.clear();
                }
            });
            s.start();
            return true;
        }
    });
}

From source file:com.shopify.buy.ui.ProductDetailsFragmentView.java

@Override
public void setImageAreaSize(final boolean grow) {
    final int finalHeight = grow ? getHeight() : imageAreaHeight;

    // hide or show the checkout button
    if (grow && !imageAreaIsExpanded) {
        hideCheckoutButton(IMAGE_AREA_FEATURES_ANIMATION_DURATION);
    } else if (!grow && imageAreaIsExpanded) {
        showCheckoutButton(IMAGE_AREA_FEATURES_ANIMATION_DURATION);
    }//from w  w  w  .  j  a v  a 2 s.c  om

    // Animate the image changing size
    ValueAnimator anim = ValueAnimator.ofInt(imageAreaWrapper.getHeight(), finalHeight);
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int val = (Integer) valueAnimator.getAnimatedValue();
            ViewGroup.LayoutParams containerLayoutParams = imageAreaWrapper.getLayoutParams();
            containerLayoutParams.height = val;
            imageAreaWrapper.setLayoutParams(containerLayoutParams);
        }

    });
    anim.setDuration(IMAGE_AREA_FEATURES_ANIMATION_DURATION);

    // Disable touch handler for duration of image resizing
    imagePager.setOnTouchListener(null);
    anim.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);

            // Re-enable the touch handle on the image
            imagePager.setOnTouchListener(imageAreaTouchHandler);

            if (grow) {
                onExpandImageArea();
            } else {
                onCollapseImageArea();
            }
        }
    });

    anim.start();
}

From source file:com.jforce.chapelhillnextbus.ExpandingListView.java

/**
 * This method expands the view that was clicked and animates all the views
 * around it to make room for the expanding view. There are several steps
 * required to do this which are outlined below.
 * <p/>/*from  w  ww  . j  a v  a 2s .  c o m*/
 * 1. Store the current top and bottom bounds of each visible item in the
 * listview. 2. Update the layout parameters of the selected view. In the
 * context of this method, the view should be originally collapsed and set
 * to some custom height. The layout parameters are updated so as to wrap
 * the content of the additional text that is to be displayed.
 * <p/>
 * After invoking a layout to take place, the listview will order all the
 * items such that there is space for each view. This layout will be
 * independent of what the bounds of the items were prior to the layout so
 * two pre-draw passes will be made. This is necessary because after the
 * layout takes place, some views that were visible before the layout may
 * now be off bounds but a reference to these views is required so the
 * animation completes as intended.
 * <p/>
 * 3. The first predraw pass will set the bounds of all the visible items to
 * their original location before the layout took place and then force
 * another layout. Since the bounds of the cells cannot be set directly, the
 * method setSelectionFromTop can be used to achieve a very similar effect.
 * 4. The expanding view's bounds are animated to what the final values
 * should be from the original bounds. 5. The bounds above the expanding
 * view are animated upwards while the bounds below the expanding view are
 * animated downwards. 6. The extra text is faded in as its contents become
 * visible throughout the animation process.
 * <p/>
 * It is important to note that the listview is disabled during the
 * animation because the scrolling behaviour is unpredictable if the bounds
 * of the items within the listview are not constant during the scroll.
 */

private void expandView(final View view) {
    final ExpandableListItem viewObject = (ExpandableListItem) getItemAtPosition(getPositionForView(view));

    /* Store the original top and bottom bounds of all the cells. */
    final int oldTop = view.getTop();
    final int oldBottom = view.getBottom();

    final HashMap<View, int[]> oldCoordinates = new HashMap<View, int[]>();

    int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View v = getChildAt(i);
        ViewCompat.setHasTransientState(v, true);
        oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() });
    }

    /* Update the layout so the extra content becomes visible. */
    final View expandingLayout = view.findViewById(R.id.expanding_layout);
    expandingLayout.setVisibility(View.VISIBLE);

    /*
       * Add an onPreDraw Listener to the listview. onPreDraw will get invoked
     * after onLayout and onMeasure have run but before anything has been
     * drawn. This means that the final post layout properties for all the
     * items have already been determined, but still have not been rendered
     * onto the screen.
     */
    final ViewTreeObserver observer = getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            /* Determine if this is the first or second pass. */
            if (!mShouldRemoveObserver) {
                mShouldRemoveObserver = true;

                /*
                 * Calculate what the parameters should be for
                 * setSelectionFromTop. The ListView must be offset in a
                 * way, such that after the animation takes place, all the
                 * cells that remain visible are rendered completely by the
                 * ListView.
                 */
                int newTop = view.getTop();
                int newBottom = view.getBottom();

                int newHeight = newBottom - newTop;
                int oldHeight = oldBottom - oldTop;
                int delta = newHeight - oldHeight;

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, delta, true);

                int currentTop = view.getTop();
                int futureTop = oldTop - mTranslate[0];

                int firstChildStartTop = getChildAt(0).getTop();
                int firstVisiblePosition = getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = getChildAt(i);
                    int height = v.getBottom() - Math.max(0, v.getTop());
                    if (deltaTop - height > 0) {
                        firstVisiblePosition++;
                        deltaTop -= height;
                    } else {
                        break;
                    }
                }

                if (i > 0) {
                    firstChildStartTop = 0;
                }

                setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                /*
                 * Request another layout to update the layout parameters of
                 * the cells.
                 */
                requestLayout();

                /*
                 * Return false such that the ListView does not redraw its
                 * contents on this layout but only updates all the
                 * parameters associated with its children.
                 */
                return false;
            }

            /*
             * Remove the predraw listener so this method does not keep
             * getting called.
             */
            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

            int yTranslateTop = mTranslate[0];
            int yTranslateBottom = mTranslate[1];

            ArrayList<Animator> animations = new ArrayList<Animator>();

            int index = indexOfChild(view);

            /*
             * Loop through all the views that were on the screen before the
             * cell was expanded. Some cells will still be children of the
             * ListView while others will not. The cells that remain
             * children of the ListView simply have their bounds animated
             * appropriately. The cells that are no longer children of the
             * ListView also have their bounds animated, but must also be
             * added to a list of views which will be drawn in dispatchDraw.
             */
            for (View v : oldCoordinates.keySet()) {
                int[] old = oldCoordinates.get(v);
                v.setTop(old[0]);
                v.setBottom(old[1]);
                if (v.getParent() == null) {
                    mViewsToDraw.add(v);
                    int delta = old[0] < oldTop ? -yTranslateTop : yTranslateBottom;
                    animations.add(getAnimation(v, delta, delta));
                } else {
                    int i = indexOfChild(v);
                    if (v != view) {
                        int delta = i > index ? yTranslateBottom : -yTranslateTop;
                        animations.add(getAnimation(v, delta, delta));
                    }
                    ViewCompat.setHasTransientState(v, false);
                }
            }

            /* Adds animation for expanding the cell that was clicked. */
            animations.add(getAnimation(view, -yTranslateTop, yTranslateBottom));

            /* Adds an animation for fading in the extra content. */
            animations.add(ObjectAnimator.ofFloat(view.findViewById(R.id.expanding_layout), View.ALPHA, 0, 1));

            /* Disabled the ListView for the duration of the animation. */
            setEnabled(false);
            setClickable(false);

            /*
             * Play all the animations created above together at the same
             * time.
             */
            AnimatorSet s = new AnimatorSet();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    viewObject.setExpanded(true);
                    setEnabled(true);
                    setClickable(true);
                    if (mViewsToDraw.size() > 0) {
                        for (View v : mViewsToDraw) {
                            ViewCompat.setHasTransientState(v, false);
                        }
                    }
                    mViewsToDraw.clear();
                }
            });
            s.start();
            return true;
        }
    });
}

From source file:com.dgnt.dominionCardPicker.view.DynamicListView.java

/**
 * Resets all the appropriate fields to a default state while also animating
 * the hover cell back to its correct location.
 *//*from ww  w . jav a2  s  .  c  om*/
private void touchEventsEnded() {
    final View mobileView = getViewForID(mMobileItemId);
    if (mCellIsMobile || mIsWaitingForScrollFinish) {
        mCellIsMobile = false;
        mIsWaitingForScrollFinish = false;
        mIsMobileScrolling = false;
        mActivePointerId = INVALID_POINTER_ID;

        // If the autoscroller has not completed scrolling, we need to wait for it to
        // finish in order to determine the final location of where the hover cell
        // should be animated to.
        if (mScrollState != OnScrollListener.SCROLL_STATE_IDLE) {
            mIsWaitingForScrollFinish = true;
            return;
        }

        mHoverCellCurrentBounds.offsetTo(mHoverCellOriginalBounds.left, mobileView.getTop());

        ObjectAnimator hoverViewAnimator = ObjectAnimator.ofObject(mHoverCell, "bounds", sBoundEvaluator,
                mHoverCellCurrentBounds);
        hoverViewAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                invalidate();
            }
        });
        hoverViewAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                setEnabled(false);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mAboveItemId = INVALID_ID;
                mMobileItemId = INVALID_ID;
                mBelowItemId = INVALID_ID;
                mobileView.setVisibility(VISIBLE);
                mHoverCell = null;
                setEnabled(true);
                invalidate();
            }
        });
        hoverViewAnimator.start();

    } else {
        touchEventsCancelled();
    }
}