Example usage for android.animation TypeEvaluator TypeEvaluator

List of usage examples for android.animation TypeEvaluator TypeEvaluator

Introduction

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

Prototype

TypeEvaluator

Source Link

Usage

From source file:net.osmand.plus.views.controls.DynamicListView.java

/**
 * Resets all the appropriate fields to a default state while also animating
 * the hover cell back to its correct location.
 *//*from www . ja v  a  2s  .c  o m*/
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());

        if (android.os.Build.VERSION.SDK_INT < 12) {
            finishTouch();
        } else {
            /**
             * This TypeEvaluator is used to animate the BitmapDrawable back to its
             * final location when the user lifts his finger by modifying the
             * BitmapDrawable's bounds.
             */
            TypeEvaluator<Rect> sBoundEvaluator = new TypeEvaluator<Rect>() {
                public Rect evaluate(float fraction, Rect startValue, Rect endValue) {
                    return new Rect(interpolate(startValue.left, endValue.left, fraction),
                            interpolate(startValue.top, endValue.top, fraction),
                            interpolate(startValue.right, endValue.right, fraction),
                            interpolate(startValue.bottom, endValue.bottom, fraction));
                }

                public int interpolate(int start, int end, float fraction) {
                    return (int) (start + fraction * (end - start));
                }
            };

            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) {
                    finishTouch();
                }
            });
            hoverViewAnimator.start();
        }
    } else {
        touchEventsCancelled();
    }
}