Search in sources :

Example 21 with TimeInterpolator

use of android.animation.TimeInterpolator in project android_packages_apps_Launcher2 by CyanogenMod.

the class DragLayer method animateView.

/**
     * This method animates a view at the end of a drag and drop animation.
     *
     * @param view The view to be animated. This view is drawn directly into DragLayer, and so
     *        doesn't need to be a child of DragLayer.
     * @param from The initial location of the view. Only the left and top parameters are used.
     * @param to The final location of the view. Only the left and top parameters are used. This
     *        location doesn't account for scaling, and so should be centered about the desired
     *        final location (including scaling).
     * @param finalAlpha The final alpha of the view, in case we want it to fade as it animates.
     * @param finalScale The final scale of the view. The view is scaled about its center.
     * @param duration The duration of the animation.
     * @param motionInterpolator The interpolator to use for the location of the view.
     * @param alphaInterpolator The interpolator to use for the alpha of the view.
     * @param onCompleteRunnable Optional runnable to run on animation completion.
     * @param fadeOut Whether or not to fade out the view once the animation completes. If true,
     *        the runnable will execute after the view is faded out.
     * @param anchorView If not null, this represents the view which the animated view stays
     *        anchored to in case scrolling is currently taking place. Note: currently this is
     *        only used for the X dimension for the case of the workspace.
     */
public void animateView(final DragView view, final Rect from, final Rect to, final float finalAlpha, final float initScaleX, final float initScaleY, final float finalScaleX, final float finalScaleY, int duration, final Interpolator motionInterpolator, final Interpolator alphaInterpolator, final Runnable onCompleteRunnable, final int animationEndStyle, View anchorView) {
    // Calculate the duration of the animation based on the object's distance
    final float dist = (float) Math.sqrt(Math.pow(to.left - from.left, 2) + Math.pow(to.top - from.top, 2));
    final Resources res = getResources();
    final float maxDist = (float) res.getInteger(R.integer.config_dropAnimMaxDist);
    // If duration < 0, this is a cue to compute the duration based on the distance
    if (duration < 0) {
        duration = res.getInteger(R.integer.config_dropAnimMaxDuration);
        if (dist < maxDist) {
            duration *= mCubicEaseOutInterpolator.getInterpolation(dist / maxDist);
        }
        duration = Math.max(duration, res.getInteger(R.integer.config_dropAnimMinDuration));
    }
    // Fall back to cubic ease out interpolator for the animation if none is specified
    TimeInterpolator interpolator = null;
    if (alphaInterpolator == null || motionInterpolator == null) {
        interpolator = mCubicEaseOutInterpolator;
    }
    // Animate the view
    final float initAlpha = view.getAlpha();
    final float dropViewScale = view.getScaleX();
    AnimatorUpdateListener updateCb = new AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            final float percent = (Float) animation.getAnimatedValue();
            final int width = view.getMeasuredWidth();
            final int height = view.getMeasuredHeight();
            float alphaPercent = alphaInterpolator == null ? percent : alphaInterpolator.getInterpolation(percent);
            float motionPercent = motionInterpolator == null ? percent : motionInterpolator.getInterpolation(percent);
            float initialScaleX = initScaleX * dropViewScale;
            float initialScaleY = initScaleY * dropViewScale;
            float scaleX = finalScaleX * percent + initialScaleX * (1 - percent);
            float scaleY = finalScaleY * percent + initialScaleY * (1 - percent);
            float alpha = finalAlpha * alphaPercent + initAlpha * (1 - alphaPercent);
            float fromLeft = from.left + (initialScaleX - 1f) * width / 2;
            float fromTop = from.top + (initialScaleY - 1f) * height / 2;
            int x = (int) (fromLeft + Math.round(((to.left - fromLeft) * motionPercent)));
            int y = (int) (fromTop + Math.round(((to.top - fromTop) * motionPercent)));
            int xPos = x - mDropView.getScrollX() + (mAnchorView != null ? (mAnchorViewInitialScrollX - mAnchorView.getScrollX()) : 0);
            int yPos = y - mDropView.getScrollY();
            mDropView.setTranslationX(xPos);
            mDropView.setTranslationY(yPos);
            mDropView.setScaleX(scaleX);
            mDropView.setScaleY(scaleY);
            mDropView.setAlpha(alpha);
        }
    };
    animateView(view, updateCb, duration, interpolator, onCompleteRunnable, animationEndStyle, anchorView);
}
Also used : Resources(android.content.res.Resources) AnimatorUpdateListener(android.animation.ValueAnimator.AnimatorUpdateListener) ValueAnimator(android.animation.ValueAnimator) TimeInterpolator(android.animation.TimeInterpolator)

Example 22 with TimeInterpolator

use of android.animation.TimeInterpolator in project Fairphone by Kwamecorp.

the class DeleteDropTarget method createFlingToTrashAnimatorListener.

/**
     * Creates an animation from the current drag view to the delete trash icon.
     */
private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer, DragObject d, PointF vel, ViewConfiguration config) {
    final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(), mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
    final Rect from = new Rect();
    dragLayer.getViewRectRelativeToSelf(d.dragView, from);
    // Calculate how far along the velocity vector we should put the intermediate point on
    // the bezier curve
    float velocity = Math.abs(vel.length());
    float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
    int offsetY = (int) (-from.top * vp);
    int offsetX = (int) (offsetY / (vel.y / vel.x));
    // intermediate t/l
    final float y2 = from.top + offsetY;
    final float x2 = from.left + offsetX;
    // drag view t/l
    final float x1 = from.left;
    final float y1 = from.top;
    // delete target t/l
    final float x3 = to.left;
    final float y3 = to.top;
    final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {

        @Override
        public float getInterpolation(float t) {
            return t * t * t * t * t * t * t * t;
        }
    };
    return new AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            final DragView dragView = (DragView) dragLayer.getAnimatedView();
            float t = ((Float) animation.getAnimatedValue()).floatValue();
            float tp = scaleAlphaInterpolator.getInterpolation(t);
            float initialScale = dragView.getInitialScale();
            float finalAlpha = 0.5f;
            float scale = dragView.getScaleX();
            float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
            float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
            float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) + (t * t) * x3;
            float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) + (t * t) * y3;
            dragView.setTranslationX(x);
            dragView.setTranslationY(y);
            dragView.setScaleX(initialScale * (1f - tp));
            dragView.setScaleY(initialScale * (1f - tp));
            dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
        }
    };
}
Also used : Rect(android.graphics.Rect) AnimatorUpdateListener(android.animation.ValueAnimator.AnimatorUpdateListener) ValueAnimator(android.animation.ValueAnimator) TimeInterpolator(android.animation.TimeInterpolator)

Example 23 with TimeInterpolator

use of android.animation.TimeInterpolator in project Fairphone by Kwamecorp.

the class DragLayer method animateView.

/**
     * This method animates a view at the end of a drag and drop animation.
     *
     * @param view The view to be animated. This view is drawn directly into DragLayer, and so
     *        doesn't need to be a child of DragLayer.
     * @param from The initial location of the view. Only the left and top parameters are used.
     * @param to The final location of the view. Only the left and top parameters are used. This
     *        location doesn't account for scaling, and so should be centered about the desired
     *        final location (including scaling).
     * @param finalAlpha The final alpha of the view, in case we want it to fade as it animates.
     * @param finalScale The final scale of the view. The view is scaled about its center.
     * @param duration The duration of the animation.
     * @param motionInterpolator The interpolator to use for the location of the view.
     * @param alphaInterpolator The interpolator to use for the alpha of the view.
     * @param onCompleteRunnable Optional runnable to run on animation completion.
     * @param fadeOut Whether or not to fade out the view once the animation completes. If true,
     *        the runnable will execute after the view is faded out.
     * @param anchorView If not null, this represents the view which the animated view stays
     *        anchored to in case scrolling is currently taking place. Note: currently this is
     *        only used for the X dimension for the case of the workspace.
     */
public void animateView(final DragView view, final Rect from, final Rect to, final float finalAlpha, final float initScaleX, final float initScaleY, final float finalScaleX, final float finalScaleY, int duration, final Interpolator motionInterpolator, final Interpolator alphaInterpolator, final Runnable onCompleteRunnable, final int animationEndStyle, View anchorView) {
    // Calculate the duration of the animation based on the object's distance
    final float dist = (float) Math.sqrt(Math.pow(to.left - from.left, 2) + Math.pow(to.top - from.top, 2));
    final Resources res = getResources();
    final float maxDist = (float) res.getInteger(R.integer.config_dropAnimMaxDist);
    // If duration < 0, this is a cue to compute the duration based on the distance
    if (duration < 0) {
        duration = res.getInteger(R.integer.config_dropAnimMaxDuration);
        if (dist < maxDist) {
            duration *= mCubicEaseOutInterpolator.getInterpolation(dist / maxDist);
        }
        duration = Math.max(duration, res.getInteger(R.integer.config_dropAnimMinDuration));
    }
    // Fall back to cubic ease out interpolator for the animation if none is specified
    TimeInterpolator interpolator = null;
    if (alphaInterpolator == null || motionInterpolator == null) {
        interpolator = mCubicEaseOutInterpolator;
    }
    // Animate the view
    final float initAlpha = view.getAlpha();
    final float dropViewScale = view.getScaleX();
    AnimatorUpdateListener updateCb = new AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            final float percent = (Float) animation.getAnimatedValue();
            final int width = view.getMeasuredWidth();
            final int height = view.getMeasuredHeight();
            float alphaPercent = alphaInterpolator == null ? percent : alphaInterpolator.getInterpolation(percent);
            float motionPercent = motionInterpolator == null ? percent : motionInterpolator.getInterpolation(percent);
            float initialScaleX = initScaleX * dropViewScale;
            float initialScaleY = initScaleY * dropViewScale;
            float scaleX = finalScaleX * percent + initialScaleX * (1 - percent);
            float scaleY = finalScaleY * percent + initialScaleY * (1 - percent);
            float alpha = finalAlpha * alphaPercent + initAlpha * (1 - alphaPercent);
            float fromLeft = from.left + (initialScaleX - 1f) * width / 2;
            float fromTop = from.top + (initialScaleY - 1f) * height / 2;
            int x = (int) (fromLeft + Math.round(((to.left - fromLeft) * motionPercent)));
            int y = (int) (fromTop + Math.round(((to.top - fromTop) * motionPercent)));
            int xPos = x - mDropView.getScrollX() + (mAnchorView != null ? (mAnchorViewInitialScrollX - mAnchorView.getScrollX()) : 0);
            int yPos = y - mDropView.getScrollY();
            mDropView.setTranslationX(xPos);
            mDropView.setTranslationY(yPos);
            mDropView.setScaleX(scaleX);
            mDropView.setScaleY(scaleY);
            mDropView.setAlpha(alpha);
        }
    };
    animateView(view, updateCb, duration, interpolator, onCompleteRunnable, animationEndStyle, anchorView);
}
Also used : Resources(android.content.res.Resources) AnimatorUpdateListener(android.animation.ValueAnimator.AnimatorUpdateListener) ValueAnimator(android.animation.ValueAnimator) TimeInterpolator(android.animation.TimeInterpolator)

Example 24 with TimeInterpolator

use of android.animation.TimeInterpolator in project XobotOS by xamarin.

the class Tweener method to.

public static Tweener to(Object object, long duration, Object... vars) {
    long delay = 0;
    AnimatorUpdateListener updateListener = null;
    AnimatorListener listener = null;
    TimeInterpolator interpolator = null;
    // Iterate through arguments and discover properties to animate
    ArrayList<PropertyValuesHolder> props = new ArrayList<PropertyValuesHolder>(vars.length / 2);
    for (int i = 0; i < vars.length; i += 2) {
        if (!(vars[i] instanceof String)) {
            throw new IllegalArgumentException("Key must be a string: " + vars[i]);
        }
        String key = (String) vars[i];
        Object value = vars[i + 1];
        if ("simultaneousTween".equals(key)) {
        // TODO
        } else if ("ease".equals(key)) {
            // TODO: multiple interpolators?
            interpolator = (TimeInterpolator) value;
        } else if ("onUpdate".equals(key) || "onUpdateListener".equals(key)) {
            updateListener = (AnimatorUpdateListener) value;
        } else if ("onComplete".equals(key) || "onCompleteListener".equals(key)) {
            listener = (AnimatorListener) value;
        } else if ("delay".equals(key)) {
            delay = ((Number) value).longValue();
        } else if ("syncWith".equals(key)) {
        // TODO
        } else if (value instanceof float[]) {
            props.add(PropertyValuesHolder.ofFloat(key, ((float[]) value)[0], ((float[]) value)[1]));
        } else if (value instanceof Number) {
            float floatValue = ((Number) value).floatValue();
            props.add(PropertyValuesHolder.ofFloat(key, floatValue));
        } else {
            throw new IllegalArgumentException("Bad argument for key \"" + key + "\" with value " + value.getClass());
        }
    }
    // Re-use existing tween, if present
    Tweener tween = sTweens.get(object);
    ObjectAnimator anim = null;
    if (tween == null) {
        anim = ObjectAnimator.ofPropertyValuesHolder(object, props.toArray(new PropertyValuesHolder[props.size()]));
        tween = new Tweener(anim);
        sTweens.put(object, tween);
        if (DEBUG)
            Log.v(TAG, "Added new Tweener " + tween);
    } else {
        anim = sTweens.get(object).animator;
        // Cancel all animators for given object
        replace(props, object);
    }
    if (interpolator != null) {
        anim.setInterpolator(interpolator);
    }
    // Update animation with properties discovered in loop above
    anim.setStartDelay(delay);
    anim.setDuration(duration);
    if (updateListener != null) {
        // There should be only one
        anim.removeAllUpdateListeners();
        anim.addUpdateListener(updateListener);
    }
    if (listener != null) {
        // There should be only one.
        anim.removeAllListeners();
        anim.addListener(listener);
    }
    anim.addListener(mCleanupListener);
    anim.start();
    return tween;
}
Also used : ObjectAnimator(android.animation.ObjectAnimator) ArrayList(java.util.ArrayList) AnimatorUpdateListener(android.animation.ValueAnimator.AnimatorUpdateListener) TimeInterpolator(android.animation.TimeInterpolator) AnimatorListener(android.animation.Animator.AnimatorListener) PropertyValuesHolder(android.animation.PropertyValuesHolder)

Example 25 with TimeInterpolator

use of android.animation.TimeInterpolator in project android_frameworks_base by crdroidandroid.

the class ViewPropertyAnimatorRT method doStartAnimation.

private void doStartAnimation(ViewPropertyAnimator parent) {
    int size = parent.mPendingAnimations.size();
    long startDelay = parent.getStartDelay();
    long duration = parent.getDuration();
    TimeInterpolator interpolator = parent.getInterpolator();
    if (interpolator == null) {
        // Documented to be LinearInterpolator in ValueAnimator.setInterpolator
        interpolator = sLinearInterpolator;
    }
    if (!RenderNodeAnimator.isNativeInterpolator(interpolator)) {
        interpolator = new FallbackLUTInterpolator(interpolator, duration);
    }
    for (int i = 0; i < size; i++) {
        NameValuesHolder holder = parent.mPendingAnimations.get(i);
        int property = RenderNodeAnimator.mapViewPropertyToRenderProperty(holder.mNameConstant);
        final float finalValue = holder.mFromValue + holder.mDeltaValue;
        RenderNodeAnimator animator = new RenderNodeAnimator(property, finalValue);
        animator.setStartDelay(startDelay);
        animator.setDuration(duration);
        animator.setInterpolator(interpolator);
        animator.setTarget(mView);
        animator.start();
        mAnimators[property] = animator;
    }
    parent.mPendingAnimations.clear();
}
Also used : FallbackLUTInterpolator(com.android.internal.view.animation.FallbackLUTInterpolator) NameValuesHolder(android.view.ViewPropertyAnimator.NameValuesHolder) TimeInterpolator(android.animation.TimeInterpolator)

Aggregations

TimeInterpolator (android.animation.TimeInterpolator)29 AnimatorUpdateListener (android.animation.ValueAnimator.AnimatorUpdateListener)14 ValueAnimator (android.animation.ValueAnimator)10 ObjectAnimator (android.animation.ObjectAnimator)8 Animator (android.animation.Animator)5 Rect (android.graphics.Rect)5 View (android.view.View)5 NameValuesHolder (android.view.ViewPropertyAnimator.NameValuesHolder)5 FallbackLUTInterpolator (com.android.internal.view.animation.FallbackLUTInterpolator)5 ArrayList (java.util.ArrayList)4 AnimatorListener (android.animation.Animator.AnimatorListener)3 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)3 AnimatorSet (android.animation.AnimatorSet)3 PropertyValuesHolder (android.animation.PropertyValuesHolder)3 Resources (android.content.res.Resources)3 ViewConfiguration (android.view.ViewConfiguration)3 ViewGroup (android.view.ViewGroup)3 TargetApi (android.annotation.TargetApi)2 Transition (android.transitions.everywhere.Transition)2 DecelerateInterpolator (android.view.animation.DecelerateInterpolator)2