diff options
Diffstat (limited to 'src/com/android/launcher2/DragLayer.java')
-rw-r--r-- | src/com/android/launcher2/DragLayer.java | 172 |
1 files changed, 99 insertions, 73 deletions
diff --git a/src/com/android/launcher2/DragLayer.java b/src/com/android/launcher2/DragLayer.java index 915477119..a3b389d24 100644 --- a/src/com/android/launcher2/DragLayer.java +++ b/src/com/android/launcher2/DragLayer.java @@ -62,15 +62,17 @@ public class DragLayer extends FrameLayout { private ValueAnimator mDropAnim = null; private ValueAnimator mFadeOutAnim = null; private TimeInterpolator mCubicEaseOutInterpolator = new DecelerateInterpolator(1.5f); - private View mDropView = null; + private DragView mDropView = null; + private int mAnchorViewInitialScrollX = 0; + private View mAnchorView = null; - private int[] mDropViewPos = new int[2]; - private float mDropViewScale; - private float mDropViewAlpha; private boolean mHoverPointClosesFolder = false; private Rect mHitRect = new Rect(); private int mWorkspaceIndex = -1; private int mQsbIndex = -1; + public static final int ANIMATION_END_DISAPPEAR = 0; + public static final int ANIMATION_END_FADE_OUT = 1; + public static final int ANIMATION_END_REMAIN_VISIBLE = 2; /** * Used to create a new DragLayer from XML. @@ -412,24 +414,25 @@ public class DragLayer extends FrameLayout { animateViewIntoPosition(dragView, child, null); } - public void animateViewIntoPosition(DragView dragView, final int[] pos, float scale, - Runnable onFinishRunnable) { + public void animateViewIntoPosition(DragView dragView, final int[] pos, float alpha, + float scaleX, float scaleY, int animationEndStyle, Runnable onFinishRunnable, + int duration) { Rect r = new Rect(); getViewRectRelativeToSelf(dragView, r); final int fromX = r.left; final int fromY = r.top; - animateViewIntoPosition(dragView, fromX, fromY, pos[0], pos[1], scale, - onFinishRunnable, true, -1); + animateViewIntoPosition(dragView, fromX, fromY, pos[0], pos[1], alpha, 1, 1, scaleX, scaleY, + onFinishRunnable, animationEndStyle, duration, null); } public void animateViewIntoPosition(DragView dragView, final View child, final Runnable onFinishAnimationRunnable) { - animateViewIntoPosition(dragView, child, -1, onFinishAnimationRunnable); + animateViewIntoPosition(dragView, child, -1, onFinishAnimationRunnable, null); } public void animateViewIntoPosition(DragView dragView, final View child, int duration, - final Runnable onFinishAnimationRunnable) { + final Runnable onFinishAnimationRunnable, View anchorView) { ((CellLayoutChildren) child.getParent()).measureChild(child); CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); @@ -455,7 +458,7 @@ public class DragLayer extends FrameLayout { toX -= (dragView.getMeasuredWidth() - Math.round(scale * child.getMeasuredWidth())) / 2; } else if (child instanceof FolderIcon) { // Account for holographic blur padding on the drag view - toY -= HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2; + toY -= Workspace.DRAG_BITMAP_PADDING / 2; // Center in the x coordinate about the target's drawable toX -= (dragView.getMeasuredWidth() - Math.round(scale * child.getMeasuredWidth())) / 2; } else { @@ -467,34 +470,27 @@ public class DragLayer extends FrameLayout { final int fromX = r.left; final int fromY = r.top; child.setVisibility(INVISIBLE); - child.setAlpha(0); Runnable onCompleteRunnable = new Runnable() { public void run() { child.setVisibility(VISIBLE); - ObjectAnimator oa = ObjectAnimator.ofFloat(child, "alpha", 0f, 1f); - oa.setDuration(60); - oa.addListener(new AnimatorListenerAdapter() { - @Override - public void onAnimationEnd(android.animation.Animator animation) { - if (onFinishAnimationRunnable != null) { - onFinishAnimationRunnable.run(); - } - } - }); - oa.start(); + if (onFinishAnimationRunnable != null) { + onFinishAnimationRunnable.run(); + } } }; - animateViewIntoPosition(dragView, fromX, fromY, toX, toY, scale, - onCompleteRunnable, true, duration); + animateViewIntoPosition(dragView, fromX, fromY, toX, toY, 1, 1, 1, scale, scale, + onCompleteRunnable, ANIMATION_END_DISAPPEAR, duration, anchorView); } - private void animateViewIntoPosition(final View view, final int fromX, final int fromY, - final int toX, final int toY, float finalScale, Runnable onCompleteRunnable, - boolean fadeOut, int duration) { + public void animateViewIntoPosition(final DragView view, final int fromX, final int fromY, + final int toX, final int toY, float finalAlpha, float initScaleX, float initScaleY, + float finalScaleX, float finalScaleY, Runnable onCompleteRunnable, + int animationEndStyle, int duration, View anchorView) { Rect from = new Rect(fromX, fromY, fromX + view.getMeasuredWidth(), fromY + view.getMeasuredHeight()); Rect to = new Rect(toX, toY, toX + view.getMeasuredWidth(), toY + view.getMeasuredHeight()); - animateView(view, from, to, 1f, finalScale, duration, null, null, onCompleteRunnable, true); + animateView(view, from, to, finalAlpha, initScaleX, initScaleY, finalScaleX, finalScaleY, duration, + null, null, onCompleteRunnable, animationEndStyle, anchorView); } /** @@ -514,11 +510,16 @@ public class DragLayer extends FrameLayout { * @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 View view, final Rect from, final Rect to, final float finalAlpha, - final float finalScale, int duration, final Interpolator motionInterpolator, - final Interpolator alphaInterpolator, final Runnable onCompleteRunnable, - final boolean fadeOut) { + 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)); @@ -531,6 +532,7 @@ public class DragLayer extends FrameLayout { if (dist < maxDist) { duration *= mCubicEaseOutInterpolator.getInterpolation(dist / maxDist); } + duration = Math.max(duration, res.getInteger(R.integer.config_dropAnimMinDuration)); } if (mDropAnim != null) { @@ -541,36 +543,58 @@ public class DragLayer extends FrameLayout { mFadeOutAnim.cancel(); } + // Show the drop view if it was previously hidden mDropView = view; - final float initialAlpha = view.getAlpha(); + mDropView.cancelAnimation(); + mDropView.resetLayoutParams(); mDropAnim = new ValueAnimator(); if (alphaInterpolator == null || motionInterpolator == null) { mDropAnim.setInterpolator(mCubicEaseOutInterpolator); } + if (anchorView != null) { + mAnchorViewInitialScrollX = anchorView.getScrollX(); + } + mAnchorView = anchorView; + + final float initAlpha = view.getAlpha(); + final float dropViewScale = mDropView.getScaleX(); + mDropAnim.setDuration(duration); mDropAnim.setFloatValues(0.0f, 1.0f); mDropAnim.removeAllUpdateListeners(); mDropAnim.addUpdateListener(new AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { final float percent = (Float) animation.getAnimatedValue(); - // Invalidate the old position - int width = view.getMeasuredWidth(); - int height = view.getMeasuredHeight(); - invalidate(mDropViewPos[0], mDropViewPos[1], - mDropViewPos[0] + width, mDropViewPos[1] + height); + 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); - mDropViewPos[0] = from.left + (int) Math.round(((to.left - from.left) * motionPercent)); - mDropViewPos[1] = from.top + (int) Math.round(((to.top - from.top) * motionPercent)); - mDropViewScale = percent * finalScale + (1 - percent); - mDropViewAlpha = alphaPercent * finalAlpha + (1 - alphaPercent) * initialAlpha; - invalidate(mDropViewPos[0], mDropViewPos[1], - mDropViewPos[0] + width, mDropViewPos[1] + height); + 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); } }); mDropAnim.addListener(new AnimatorListenerAdapter() { @@ -578,16 +602,36 @@ public class DragLayer extends FrameLayout { if (onCompleteRunnable != null) { onCompleteRunnable.run(); } - if (fadeOut) { + switch (animationEndStyle) { + case ANIMATION_END_DISAPPEAR: + clearAnimatedView(); + break; + case ANIMATION_END_FADE_OUT: fadeOutDragView(); - } else { - mDropView = null; + break; + case ANIMATION_END_REMAIN_VISIBLE: + break; } } }); mDropAnim.start(); } + public void clearAnimatedView() { + if (mDropAnim != null) { + mDropAnim.cancel(); + } + if (mDropView != null) { + mDropView.remove(); + } + mDropView = null; + invalidate(); + } + + public View getAnimatedView() { + return mDropView; + } + private void fadeOutDragView() { mFadeOutAnim = new ValueAnimator(); mFadeOutAnim.setDuration(150); @@ -596,16 +640,18 @@ public class DragLayer extends FrameLayout { mFadeOutAnim.addUpdateListener(new AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { final float percent = (Float) animation.getAnimatedValue(); - mDropViewAlpha = 1 - percent; - int width = mDropView.getMeasuredWidth(); - int height = mDropView.getMeasuredHeight(); - invalidate(mDropViewPos[0], mDropViewPos[1], - mDropViewPos[0] + width, mDropViewPos[1] + height); + + float alpha = 1 - percent; + mDropView.setAlpha(alpha); } }); mFadeOutAnim.addListener(new AnimatorListenerAdapter() { public void onAnimationEnd(Animator animation) { + if (mDropView != null) { + mDropView.remove(); + } mDropView = null; + invalidate(); } }); mFadeOutAnim.start(); @@ -654,24 +700,4 @@ public class DragLayer extends FrameLayout { return i; } } - - @Override - protected void dispatchDraw(Canvas canvas) { - super.dispatchDraw(canvas); - if (mDropView != null) { - // We are animating an item that was just dropped on the home screen. - // Render its View in the current animation position. - canvas.save(Canvas.MATRIX_SAVE_FLAG); - final int xPos = mDropViewPos[0] - mDropView.getScrollX(); - final int yPos = mDropViewPos[1] - mDropView.getScrollY(); - int width = mDropView.getMeasuredWidth(); - int height = mDropView.getMeasuredHeight(); - canvas.translate(xPos, yPos); - canvas.translate((1 - mDropViewScale) * width / 2, (1 - mDropViewScale) * height / 2); - canvas.scale(mDropViewScale, mDropViewScale); - mDropView.setAlpha(mDropViewAlpha); - mDropView.draw(canvas); - canvas.restore(); - } - } } |