use of android.animation.TimeInterpolator in project Launcher3 by chislon.
the class PagedView method onFlingToDelete.
public void onFlingToDelete(PointF vel) {
final long startTime = AnimationUtils.currentAnimationTimeMillis();
// NOTE: Because it takes time for the first frame of animation to actually be
// called and we expect the animation to be a continuation of the fling, we have
// to account for the time that has elapsed since the fling finished. And since
// we don't have a startDelay, we will always get call to update when we call
// start() (which we want to ignore).
final TimeInterpolator tInterpolator = new TimeInterpolator() {
private int mCount = -1;
private long mStartTime;
private float mOffset;
/* Anonymous inner class ctor */
{
mStartTime = startTime;
}
@Override
public float getInterpolation(float t) {
if (mCount < 0) {
mCount++;
} else if (mCount == 0) {
mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() - mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
mCount++;
}
return Math.min(1f, mOffset + t);
}
};
final Rect from = new Rect();
final View dragView = mDragView;
from.left = (int) dragView.getTranslationX();
from.top = (int) dragView.getTranslationY();
AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel, from, startTime, FLING_TO_DELETE_FRICTION);
final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
// Create and start the animation
ValueAnimator mDropAnim = new ValueAnimator();
mDropAnim.setInterpolator(tInterpolator);
mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
mDropAnim.setFloatValues(0f, 1f);
mDropAnim.addUpdateListener(updateCb);
mDropAnim.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
onAnimationEndRunnable.run();
}
});
mDropAnim.start();
mDeferringForDelete = true;
}
use of android.animation.TimeInterpolator in project Launcher3 by chislon.
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);
}
use of android.animation.TimeInterpolator in project Fairphone by Kwamecorp.
the class DeleteDropTarget method onFlingToDelete.
public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
// Don't highlight the icon as it's animating
d.dragView.setColor(0);
d.dragView.updateInitialScaleToCurrentScale();
// Don't highlight the target if we are flinging from AllApps
if (isAllApps) {
resetHoverColor();
}
if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
// Defer animating out the drop target if we are animating to it
mSearchDropTargetBar.deferOnDragEnd();
mSearchDropTargetBar.finishAnimations();
}
final ViewConfiguration config = ViewConfiguration.get(mLauncher);
final DragLayer dragLayer = mLauncher.getDragLayer();
final int duration = FLING_DELETE_ANIMATION_DURATION;
final long startTime = AnimationUtils.currentAnimationTimeMillis();
// NOTE: Because it takes time for the first frame of animation to actually be
// called and we expect the animation to be a continuation of the fling, we have
// to account for the time that has elapsed since the fling finished. And since
// we don't have a startDelay, we will always get call to update when we call
// start() (which we want to ignore).
final TimeInterpolator tInterpolator = new TimeInterpolator() {
private int mCount = -1;
private float mOffset = 0f;
@Override
public float getInterpolation(float t) {
if (mCount < 0) {
mCount++;
} else if (mCount == 0) {
mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() - startTime) / duration);
mCount++;
}
return Math.min(1f, mOffset + t);
}
};
AnimatorUpdateListener updateCb = null;
if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
} else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime, duration, config);
}
Runnable onAnimationEndRunnable = new Runnable() {
@Override
public void run() {
mSearchDropTargetBar.onDragEnd();
// itself, otherwise, complete the drop to initiate the deletion process
if (!isAllApps) {
mLauncher.exitSpringLoadedDragMode();
completeDrop(d);
}
mLauncher.getDragController().onDeferredEndFling(d);
}
};
dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable, DragLayer.ANIMATION_END_DISAPPEAR, null);
}
use of android.animation.TimeInterpolator in project Signal-Android by WhisperSystems.
the class Tweener method to.
@SuppressLint("NewApi")
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;
}
use of android.animation.TimeInterpolator in project plaid by nickbutcher.
the class MorphTransform method createAnimator.
@Override
public Animator createAnimator(final ViewGroup sceneRoot, final TransitionValues startValues, final TransitionValues endValues) {
final Animator changeBounds = super.createAnimator(sceneRoot, startValues, endValues);
if (changeBounds == null)
return null;
TimeInterpolator interpolator = getInterpolator();
if (interpolator == null) {
interpolator = AnimUtils.getFastOutSlowInInterpolator(sceneRoot.getContext());
}
final MorphDrawable background = new MorphDrawable(startColor, startCornerRadius);
endValues.view.setBackground(background);
final Animator color = ObjectAnimator.ofArgb(background, MorphDrawable.COLOR, endColor);
final Animator corners = ObjectAnimator.ofFloat(background, MorphDrawable.CORNER_RADIUS, endCornerRadius);
// ease in the dialog's child views (fade in & staggered slide up)
if (endValues.view instanceof ViewGroup) {
final ViewGroup vg = (ViewGroup) endValues.view;
final long duration = getDuration() / 2;
float offset = vg.getHeight() / 3;
for (int i = 0; i < vg.getChildCount(); i++) {
View v = vg.getChildAt(i);
v.setTranslationY(offset);
v.setAlpha(0f);
v.animate().alpha(1f).translationY(0f).setDuration(duration).setStartDelay(duration).setInterpolator(interpolator);
offset *= 1.8f;
}
}
final AnimatorSet transition = new AnimatorSet();
transition.playTogether(changeBounds, corners, color);
transition.setDuration(getDuration());
transition.setInterpolator(interpolator);
return transition;
}
Aggregations