Example usage for android.animation ValueAnimator addListener

List of usage examples for android.animation ValueAnimator addListener

Introduction

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

Prototype

public void addListener(AnimatorListener listener) 

Source Link

Document

Adds a listener to the set of listeners that are sent events through the life of an animation, such as start, repeat, and end.

Usage

From source file:com.android.systemui.statusbar.phone.NotificationPanelView.java

private void flingSettings(float vel, boolean expand, final Runnable onFinishRunnable, boolean isClick) {
    float target = expand ? mQsMaxExpansionHeight : mQsMinExpansionHeight;
    if (target == mQsExpansionHeight) {
        mScrollYOverride = -1;//w  w w .j  av  a  2 s.  com
        if (onFinishRunnable != null) {
            onFinishRunnable.run();
        }
        return;
    }
    boolean belowFalsingThreshold = isBelowFalsingThreshold();
    if (belowFalsingThreshold) {
        vel = 0;
    }
    mScrollView.setBlockFlinging(true);
    ValueAnimator animator = ValueAnimator.ofFloat(mQsExpansionHeight, target);
    if (isClick) {
        animator.setInterpolator(mTouchResponseInterpolator);
        animator.setDuration(368);
    } else {
        mFlingAnimationUtils.apply(animator, mQsExpansionHeight, target, vel);
    }
    if (belowFalsingThreshold) {
        animator.setDuration(350);
    }
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            setQsExpansion((Float) animation.getAnimatedValue());
        }
    });
    animator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            mScrollView.setBlockFlinging(false);
            mScrollYOverride = -1;
            mQsExpansionAnimator = null;
            if (onFinishRunnable != null) {
                onFinishRunnable.run();
            }
        }
    });
    animator.start();
    mQsExpansionAnimator = animator;
    mQsAnimatorExpand = expand;
}

From source file:com.marlonjones.voidlauncher.CellLayout.java

public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration, int delay,
        boolean permanent, boolean adjustOccupied) {
    ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();

    if (clc.indexOfChild(child) != -1) {
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final ItemInfo info = (ItemInfo) child.getTag();

        // We cancel any existing animations
        if (mReorderAnimators.containsKey(lp)) {
            mReorderAnimators.get(lp).cancel();
            mReorderAnimators.remove(lp);
        }/*from   w  w  w  .j a  v a2s .co  m*/

        final int oldX = lp.x;
        final int oldY = lp.y;
        if (adjustOccupied) {
            GridOccupancy occupied = permanent ? mOccupied : mTmpOccupied;
            occupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
            occupied.markCells(cellX, cellY, lp.cellHSpan, lp.cellVSpan, true);
        }
        lp.isLockedToGrid = true;
        if (permanent) {
            lp.cellX = info.cellX = cellX;
            lp.cellY = info.cellY = cellY;
        } else {
            lp.tmpCellX = cellX;
            lp.tmpCellY = cellY;
        }
        clc.setupLp(lp);
        lp.isLockedToGrid = false;
        final int newX = lp.x;
        final int newY = lp.y;

        lp.x = oldX;
        lp.y = oldY;

        // Exit early if we're not actually moving the view
        if (oldX == newX && oldY == newY) {
            lp.isLockedToGrid = true;
            return true;
        }

        ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
        va.setDuration(duration);
        mReorderAnimators.put(lp, va);

        va.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float r = ((Float) animation.getAnimatedValue()).floatValue();
                lp.x = (int) ((1 - r) * oldX + r * newX);
                lp.y = (int) ((1 - r) * oldY + r * newY);
                child.requestLayout();
            }
        });
        va.addListener(new AnimatorListenerAdapter() {
            boolean cancelled = false;

            public void onAnimationEnd(Animator animation) {
                // If the animation was cancelled, it means that another animation
                // has interrupted this one, and we don't want to lock the item into
                // place just yet.
                if (!cancelled) {
                    lp.isLockedToGrid = true;
                    child.requestLayout();
                }
                if (mReorderAnimators.containsKey(lp)) {
                    mReorderAnimators.remove(lp);
                }
            }

            public void onAnimationCancel(Animator animation) {
                cancelled = true;
            }
        });
        va.setStartDelay(delay);
        va.start();
        return true;
    }
    return false;
}

From source file:com.android.launcher3.CellLayout.java

public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration, int delay,
        boolean permanent, boolean adjustOccupied) {
    ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
    boolean[][] occupied = mOccupied;
    if (!permanent) {
        occupied = mTmpOccupied;//from  w  w w  .j  a v a  2s  .  c  o m
    }

    if (clc.indexOfChild(child) != -1) {
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final ItemInfo info = (ItemInfo) child.getTag();

        // We cancel any existing animations
        if (mReorderAnimators.containsKey(lp)) {
            mReorderAnimators.get(lp).cancel();
            mReorderAnimators.remove(lp);
        }

        final int oldX = lp.x;
        final int oldY = lp.y;
        if (adjustOccupied) {
            occupied[lp.cellX][lp.cellY] = false;
            occupied[cellX][cellY] = true;
        }
        lp.isLockedToGrid = true;
        if (permanent) {
            lp.cellX = info.cellX = cellX;
            lp.cellY = info.cellY = cellY;
        } else {
            lp.tmpCellX = cellX;
            lp.tmpCellY = cellY;
        }
        clc.setupLp(lp);
        lp.isLockedToGrid = false;
        final int newX = lp.x;
        final int newY = lp.y;

        lp.x = oldX;
        lp.y = oldY;

        // Exit early if we're not actually moving the view
        if (oldX == newX && oldY == newY) {
            lp.isLockedToGrid = true;
            return true;
        }

        ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
        va.setDuration(duration);
        mReorderAnimators.put(lp, va);

        va.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float r = ((Float) animation.getAnimatedValue()).floatValue();
                lp.x = (int) ((1 - r) * oldX + r * newX);
                lp.y = (int) ((1 - r) * oldY + r * newY);
                child.requestLayout();
            }
        });
        va.addListener(new AnimatorListenerAdapter() {
            boolean cancelled = false;

            public void onAnimationEnd(Animator animation) {
                // If the animation was cancelled, it means that another animation
                // has interrupted this one, and we don't want to lock the item into
                // place just yet.
                if (!cancelled) {
                    lp.isLockedToGrid = true;
                    child.requestLayout();
                }
                if (mReorderAnimators.containsKey(lp)) {
                    mReorderAnimators.remove(lp);
                }
            }

            public void onAnimationCancel(Animator animation) {
                cancelled = true;
            }
        });
        va.setStartDelay(delay);
        va.start();
        return true;
    }
    return false;
}

From source file:com.android.leanlauncher.CellLayout.java

public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration, int delay,
        boolean permanent, boolean adjustOccupied) {
    ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
    boolean[][] occupied = mOccupied;
    if (!permanent) {
        occupied = mTmpOccupied;/*w w  w  . ja  v  a 2  s  .  c  o m*/
    }

    if (clc.indexOfChild(child) != -1) {
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final ItemInfo info = (ItemInfo) child.getTag();

        // We cancel any existing animations
        if (mReorderAnimators.containsKey(lp)) {
            mReorderAnimators.get(lp).cancel();
            mReorderAnimators.remove(lp);
        }

        final int oldX = lp.x;
        final int oldY = lp.y;
        if (adjustOccupied) {
            occupied[lp.cellX][lp.cellY] = false;
            occupied[cellX][cellY] = true;
        }
        lp.isLockedToGrid = true;
        if (permanent) {
            lp.cellX = info.cellX = cellX;
            lp.cellY = info.cellY = cellY;
        } else {
            lp.tmpCellX = cellX;
            lp.tmpCellY = cellY;
        }
        clc.setupLp(lp);
        lp.isLockedToGrid = false;
        final int newX = lp.x;
        final int newY = lp.y;

        lp.x = oldX;
        lp.y = oldY;

        // Exit early if we're not actually moving the view
        if (oldX == newX && oldY == newY) {
            lp.isLockedToGrid = true;
            return true;
        }

        ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
        va.setDuration(duration);
        mReorderAnimators.put(lp, va);

        va.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float r = (Float) animation.getAnimatedValue();
                lp.x = (int) ((1 - r) * oldX + r * newX);
                lp.y = (int) ((1 - r) * oldY + r * newY);
                child.requestLayout();
            }
        });
        va.addListener(new AnimatorListenerAdapter() {
            boolean cancelled = false;

            public void onAnimationEnd(Animator animation) {
                // If the animation was cancelled, it means that another animation
                // has interrupted this one, and we don't want to lock the item into
                // place just yet.
                if (!cancelled) {
                    lp.isLockedToGrid = true;
                    child.requestLayout();
                }
                if (mReorderAnimators.containsKey(lp)) {
                    mReorderAnimators.remove(lp);
                }
            }

            public void onAnimationCancel(Animator animation) {
                cancelled = true;
            }
        });
        va.setStartDelay(delay);
        va.start();
        return true;
    }
    return false;
}

From source file:com.gu.swiperefresh.ProgressDrawable.java

private void setupAnimators() {
    final Ring ring = mRing;
    final ValueAnimator animator = ObjectAnimator.ofFloat(0f, 1f);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override// w  w  w.jav  a 2 s  .  c o m
        public void onAnimationUpdate(ValueAnimator animation) {
            float interpolatedTime = Float.valueOf(animation.getAnimatedValue().toString());
            if (mFinishing) {
                applyFinishTranslation(interpolatedTime, ring);
            } else {
                // The minProgressArc is calculated from 0 to create an
                // angle that matches the stroke width.
                final float minProgressArc = getMinProgressArc(ring);
                final float startingEndTrim = ring.getStartingEndTrim();
                final float startingTrim = ring.getStartingStartTrim();
                final float startingRotation = ring.getStartingRotation();

                updateRingColor(interpolatedTime, ring);

                // Moving the start trim only occurs in the first 50% of a
                // single ring animation
                if (interpolatedTime <= START_TRIM_DURATION_OFFSET) {
                    // scale the interpolatedTime so that the full
                    // transformation from 0 - 1 takes place in the
                    // remaining time
                    final float scaledTime = (interpolatedTime) / (1.0f - START_TRIM_DURATION_OFFSET);
                    final float startTrim = startingTrim + ((MAX_PROGRESS_ARC - minProgressArc)
                            * MATERIAL_INTERPOLATOR.getInterpolation(scaledTime));
                    ring.setStartTrim(startTrim);
                }

                // Moving the end trim starts after 50% of a single ring
                // animation completes
                if (interpolatedTime > END_TRIM_START_DELAY_OFFSET) {
                    // scale the interpolatedTime so that the full
                    // transformation from 0 - 1 takes place in the
                    // remaining time
                    final float minArc = MAX_PROGRESS_ARC - minProgressArc;
                    float scaledTime = (interpolatedTime - START_TRIM_DURATION_OFFSET)
                            / (1.0f - START_TRIM_DURATION_OFFSET);
                    final float endTrim = startingEndTrim
                            + (minArc * MATERIAL_INTERPOLATOR.getInterpolation(scaledTime));
                    ring.setEndTrim(endTrim);
                }

                final float rotation = startingRotation + (0.25f * interpolatedTime);
                ring.setRotation(rotation);

                float groupRotation = ((FULL_ROTATION / NUM_POINTS) * interpolatedTime)
                        + (FULL_ROTATION * (mRotationCount / NUM_POINTS));
                setRotation(groupRotation);
            }
        }
    });
    animator.setRepeatCount(Animation.INFINITE);
    animator.setRepeatMode(ValueAnimator.RESTART);
    animator.setInterpolator(LINEAR_INTERPOLATOR);
    animator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {

        }

        @Override
        public void onAnimationStart(Animator animation) {
            mRotationCount = 0;
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            ring.storeOriginals();
            ring.goToNextColor();
            ring.setStartTrim(ring.getEndTrim());
            if (mFinishing) {
                // finished closing the last ring from the swipe gesture; go
                // into progress mode
                mFinishing = false;
                animation.setDuration(ANIMATION_DURATION);
                ring.setShowArrow(false);
            } else {
                mRotationCount = (mRotationCount + 1) % (NUM_POINTS);
            }
        }
    });
    mAnimation = animator;
}

From source file:com.guodong.sun.guodong.widget.ZoomImageView.java

private void startTransform(final int state) {
    if (mTransfrom == null) {
        return;//  ww w. j  a  v a2 s  .c o m
    }
    ValueAnimator valueAnimator = new ValueAnimator();
    valueAnimator.setDuration(300);
    valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    if (state == STATE_TRANSFORM_IN) {
        PropertyValuesHolder scaleHolder = PropertyValuesHolder.ofFloat("scale", mTransfrom.startScale,
                mTransfrom.endScale);
        PropertyValuesHolder leftHolder = PropertyValuesHolder.ofFloat("left", mTransfrom.startRect.left,
                mTransfrom.endRect.left);
        PropertyValuesHolder topHolder = PropertyValuesHolder.ofFloat("top", mTransfrom.startRect.top,
                mTransfrom.endRect.top);
        PropertyValuesHolder widthHolder = PropertyValuesHolder.ofFloat("width", mTransfrom.startRect.width,
                mTransfrom.endRect.width);
        PropertyValuesHolder heightHolder = PropertyValuesHolder.ofFloat("height", mTransfrom.startRect.height,
                mTransfrom.endRect.height);
        PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofInt("alpha", 0, 255);
        valueAnimator.setValues(scaleHolder, leftHolder, topHolder, widthHolder, heightHolder, alphaHolder);
    } else {
        PropertyValuesHolder scaleHolder = PropertyValuesHolder.ofFloat("scale", mTransfrom.endScale,
                mTransfrom.startScale);
        PropertyValuesHolder leftHolder = PropertyValuesHolder.ofFloat("left", mTransfrom.endRect.left,
                mTransfrom.startRect.left);
        PropertyValuesHolder topHolder = PropertyValuesHolder.ofFloat("top", mTransfrom.endRect.top,
                mTransfrom.startRect.top);
        PropertyValuesHolder widthHolder = PropertyValuesHolder.ofFloat("width", mTransfrom.endRect.width,
                mTransfrom.startRect.width);
        PropertyValuesHolder heightHolder = PropertyValuesHolder.ofFloat("height", mTransfrom.endRect.height,
                mTransfrom.startRect.height);
        PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofInt("alpha", 255, 0);
        valueAnimator.setValues(scaleHolder, leftHolder, topHolder, widthHolder, heightHolder, alphaHolder);
    }

    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public synchronized void onAnimationUpdate(ValueAnimator animation) {
            mTransfrom.scale = (Float) animation.getAnimatedValue("scale");
            mTransfrom.rect.left = (Float) animation.getAnimatedValue("left");
            mTransfrom.rect.top = (Float) animation.getAnimatedValue("top");
            mTransfrom.rect.width = (Float) animation.getAnimatedValue("width");
            mTransfrom.rect.height = (Float) animation.getAnimatedValue("height");
            mBgAlpha = (Integer) animation.getAnimatedValue("alpha");
            invalidate();
            ((Activity) getContext()).getWindow().getDecorView().invalidate();
        }
    });
    valueAnimator.addListener(new ValueAnimator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            /*
             * ???center_cropout??center_crop?
             *  ???out???Normal???bug
             */
            // TODO ???
            if (state == STATE_TRANSFORM_IN) {
                mState = STATE_NORMAL;
            }
            if (mTransformListener != null) {
                mTransformListener.onTransformComplete(state);
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }
    });
    valueAnimator.start();
}