Reverting: "Animating the drag view scale up and down when dragging items."

Change-Id: I133ff2631834a4b97e1e4750fb7c07793ddeba69
diff --git a/res/values-sw600dp/config.xml b/res/values-sw600dp/config.xml
index 2ce563d..7a07898 100644
--- a/res/values-sw600dp/config.xml
+++ b/res/values-sw600dp/config.xml
@@ -11,9 +11,10 @@
     <!-- Whether or not to fade the side pages -->
     <bool name="config_workspaceFadeAdjacentScreens">true</bool>
 
-    <!--  When dragging an item on the workspace, how much bigger (scale factor) the dragged view
-          should be, as compared to the original view. If 0, it will not be scaled at all. -->
-    <integer name="config_dragViewScaleFactor">115</integer>
+    <!-- When dragging items on the workspace, how much bigger (in pixels) the dragged view
+         should be, as compared to the original view. If 0, it will not be scaled at all.
+         Should be an even number, for pixel alignment. -->
+    <integer name="config_dragViewExtraPixels">0</integer>
 
     <!-- When shrinking the workspace, this is the percentage of its original size. -->
     <integer name="config_workspaceShrinkPercent">17</integer>
diff --git a/res/values/colors.xml b/res/values/colors.xml
index 1efeb9e..ece0fd8 100644
--- a/res/values/colors.xml
+++ b/res/values/colors.xml
@@ -23,6 +23,9 @@
     <color name="delete_target_hover_tint">#DAFF0000</color>
     <color name="info_target_hover_tint">#DA0099CC</color>
 
+    <!-- The alpha/color to apply to the drag image -->
+    <color name="drag_view_multiply_color">#CCFFFFFF</color>
+
     <color name="bubble_dark_background">#20000000</color>
 
     <color name="appwidget_error_color">#FCCC</color>
diff --git a/res/values/config.xml b/res/values/config.xml
index 691fb07..7ab9870 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -54,9 +54,10 @@
 
     <integer name="config_crosshairsFadeInTime">600</integer>
 
-    <!--  When dragging an item on the workspace, how much bigger (scale factor) the dragged view
-          should be, as compared to the original view. If 0, it will not be scaled at all. -->
-    <integer name="config_dragViewScaleFactor">115</integer>
+    <!--  When dragging an item on the workspace, how much bigger (in pixels) the dragged view
+          should be, as compared to the original view. If 0, it will not be scaled at all.
+          Should be an even number, for pixel alignment. -->
+    <integer name="config_dragViewExtraPixels">40</integer>
 
     <!-- The duration (in ms) of the fade animation on the object outlines, used when
          we are dragging objects around on the home screen. -->
diff --git a/src/com/android/launcher2/AppsCustomizePagedView.java b/src/com/android/launcher2/AppsCustomizePagedView.java
index 8db1ab6..7cfe3be 100644
--- a/src/com/android/launcher2/AppsCustomizePagedView.java
+++ b/src/com/android/launcher2/AppsCustomizePagedView.java
@@ -201,6 +201,7 @@
     private Canvas mCanvas;
     private Drawable mDefaultWidgetBackground;
     private IconCache mIconCache;
+    private int mDragViewMultiplyColor;
 
     // Dimens
     private int mContentWidth;
@@ -242,6 +243,7 @@
         Resources resources = context.getResources();
         mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
         mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
+        mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color);
 
         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
         mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
@@ -578,6 +580,9 @@
         // Save the preview for the outline generation, then dim the preview
         outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
                 false);
+        mCanvas.setBitmap(preview);
+        mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
+        mCanvas.setBitmap(null);
 
         // Start the drag
         alphaClipPaint = null;
@@ -903,6 +908,9 @@
             d.setBounds(x, y, x + w, y + h);
             d.draw(c);
             d.setBounds(oldBounds); // Restore the bounds
+            if (multiplyColor != 0xFFFFFFFF) {
+                c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
+            }
             c.setBitmap(null);
         }
     }
diff --git a/src/com/android/launcher2/DragController.java b/src/com/android/launcher2/DragController.java
index 425f301..8658eeb 100644
--- a/src/com/android/launcher2/DragController.java
+++ b/src/com/android/launcher2/DragController.java
@@ -394,9 +394,7 @@
                 listener.onDragEnd();
             }
             if (mDragObject.dragView != null) {
-                if (!mDragObject.deferDragViewCleanupPostAnimation) {
-                    mDragObject.dragView.remove();
-                }
+                mDragObject.dragView.remove();
                 mDragObject.dragView = null;
             }
         }
diff --git a/src/com/android/launcher2/DragLayer.java b/src/com/android/launcher2/DragLayer.java
index 05596ef..c315b60 100644
--- a/src/com/android/launcher2/DragLayer.java
+++ b/src/com/android/launcher2/DragLayer.java
@@ -62,10 +62,13 @@
     private ValueAnimator mDropAnim = null;
     private ValueAnimator mFadeOutAnim = null;
     private TimeInterpolator mCubicEaseOutInterpolator = new DecelerateInterpolator(1.5f);
-    private DragView mDropView = null;
+    private View 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;
@@ -487,7 +490,7 @@
                 onCompleteRunnable, true, duration, anchorView);
     }
 
-    private void animateViewIntoPosition(final DragView view, final int fromX, final int fromY,
+    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, View anchorView) {
         Rect from = new Rect(fromX, fromY, fromX +
@@ -518,10 +521,10 @@
      *        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 finalScale, int duration,
-            final Interpolator motionInterpolator, final Interpolator alphaInterpolator,
-            final Runnable onCompleteRunnable, final boolean fadeOut, View anchorView) {
+    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, 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));
@@ -544,10 +547,8 @@
             mFadeOutAnim.cancel();
         }
 
-        // Show the drop view if it was previously hidden
         mDropView = view;
-        mDropView.cancelAnimation();
-        mDropView.resetLayoutParams();
+        final float initialAlpha = view.getAlpha();
         mDropAnim = new ValueAnimator();
         if (alphaInterpolator == null || motionInterpolator == null) {
             mDropAnim.setInterpolator(mCubicEaseOutInterpolator);
@@ -558,39 +559,29 @@
         }
         mAnchorView = anchorView;
 
-        final float initialAlpha = view.getAlpha();
-        final float initialScale = 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();
-                final int width = view.getMeasuredWidth();
-                final int height = view.getMeasuredHeight();
+                // Invalidate the old position
+                int width = view.getMeasuredWidth();
+                int height = view.getMeasuredHeight();
+                invalidate(mDropViewPos[0], mDropViewPos[1],
+                        mDropViewPos[0] + width, mDropViewPos[1] + height);
 
                 float alphaPercent = alphaInterpolator == null ? percent :
                         alphaInterpolator.getInterpolation(percent);
                 float motionPercent = motionInterpolator == null ? percent :
                         motionInterpolator.getInterpolation(percent);
-                float scale = finalScale * percent + initialScale * (1 - percent);
-                float alpha = finalAlpha * alphaPercent + initialAlpha * (1 - alphaPercent);
 
-                float fromLeft = from.left + (initialScale - 1f) * width / 2;
-                float fromTop = from.top + (initialScale - 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(scale);
-                mDropView.setScaleY(scale);
-                mDropView.setAlpha(alpha);
-                invalidate();
+                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);
             }
         });
         mDropAnim.addListener(new AnimatorListenerAdapter() {
@@ -601,7 +592,6 @@
                 if (fadeOut) {
                     fadeOutDragView();
                 } else {
-                    mDropView.remove();
                     mDropView = null;
                 }
             }
@@ -617,15 +607,15 @@
         mFadeOutAnim.addUpdateListener(new AnimatorUpdateListener() {
             public void onAnimationUpdate(ValueAnimator animation) {
                 final float percent = (Float) animation.getAnimatedValue();
-
-                float alpha = 1 - percent;
-                mDropView.setAlpha(alpha);
-                invalidate();
+                mDropViewAlpha = 1 - percent;
+                int width = mDropView.getMeasuredWidth();
+                int height = mDropView.getMeasuredHeight();
+                invalidate(mDropViewPos[0], mDropViewPos[1],
+                        mDropViewPos[0] + width, mDropViewPos[1] + height);
             }
         });
         mFadeOutAnim.addListener(new AnimatorListenerAdapter() {
             public void onAnimationEnd(Animator animation) {
-                mDropView.remove();
                 mDropView = null;
             }
         });
@@ -675,4 +665,25 @@
             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() + (mAnchorView != null
+                    ? (mAnchorViewInitialScrollX - mAnchorView.getScrollX()) : 0);
+            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();
+        }
+    }
 }
diff --git a/src/com/android/launcher2/DragView.java b/src/com/android/launcher2/DragView.java
index 7be70a2..a3063b6 100644
--- a/src/com/android/launcher2/DragView.java
+++ b/src/com/android/launcher2/DragView.java
@@ -32,8 +32,6 @@
 import com.android.launcher.R;
 
 public class DragView extends View {
-    private static float sDragAlpha = 0.8f;
-
     private Bitmap mBitmap;
     private Paint mPaint;
     private int mRegistrationX;
@@ -67,13 +65,20 @@
         mDragLayer = launcher.getDragLayer();
 
         final Resources res = getResources();
-        final float scale = res.getInteger(R.integer.config_dragViewScaleFactor) / 100f;
-        final float offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
-        final float offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
+        final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels);
+
+        Matrix scale = new Matrix();
+        final float scaleFactor = (width + dragScale) / width;
+        if (scaleFactor != 1.0f) {
+            scale.setScale(scaleFactor, scaleFactor);
+        }
+
+        final int offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
+        final int offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
 
         // Animate the view into the correct position
         mAnim = ValueAnimator.ofFloat(0.0f, 1.0f);
-        mAnim.setDuration(150);
+        mAnim.setDuration(110);
         mAnim.setInterpolator(new DecelerateInterpolator(2.5f));
         mAnim.addUpdateListener(new AnimatorUpdateListener() {
             @Override
@@ -85,9 +90,6 @@
 
                 mOffsetX += deltaX;
                 mOffsetY += deltaY;
-                setScaleX(1f + (value * (scale - 1f)));
-                setScaleY(1f + (value * (scale - 1f)));
-                setAlpha(sDragAlpha * value + (1f - value));
 
                 if (getParent() == null) {
                     animation.cancel();
@@ -95,14 +97,12 @@
                     DragLayer.LayoutParams lp = mLayoutParams;
                     lp.x += deltaX;
                     lp.y += deltaY;
-                    lp.width = mBitmap.getWidth();
-                    lp.height = mBitmap.getHeight();
                     mDragLayer.requestLayout();
                 }
             }
         });
 
-        mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
+        mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
         setDragRegion(new Rect(0, 0, width, height));
 
         // The point in our scaled bitmap that the touch events are located
@@ -208,18 +208,6 @@
         mAnim.start();
     }
 
-    public void cancelAnimation() {
-        if (mAnim != null && mAnim.isRunning()) {
-            mAnim.cancel();
-        }
-    }
-
-    public void resetLayoutParams() {
-        DragLayer.LayoutParams lp = mLayoutParams;
-        lp.x = lp.y = 0;
-        mOffsetX = mOffsetY = 0;
-    }
-
     /**
      * Move the window containing this view.
      *
@@ -234,9 +222,7 @@
     }
 
     void remove() {
-        if (getParent() != null) {
-            mDragLayer.removeView(DragView.this);
-        }
+        mDragLayer.removeView(DragView.this);
     }
 
     int[] getPosition(int[] result) {
diff --git a/src/com/android/launcher2/DropTarget.java b/src/com/android/launcher2/DropTarget.java
index e49f782..4172da2 100644
--- a/src/com/android/launcher2/DropTarget.java
+++ b/src/com/android/launcher2/DropTarget.java
@@ -55,9 +55,6 @@
         /** Indicates that the drag operation was cancelled */
         public boolean cancelled = false;
 
-        /** Defers removing the DragView from the DragLayer until after the drop animation. */
-        public boolean deferDragViewCleanupPostAnimation = true;
-
         public DragObject() {
         }
     }
diff --git a/src/com/android/launcher2/FolderIcon.java b/src/com/android/launcher2/FolderIcon.java
index a023516..2a711f8 100644
--- a/src/com/android/launcher2/FolderIcon.java
+++ b/src/com/android/launcher2/FolderIcon.java
@@ -295,14 +295,14 @@
     }
 
     public void performCreateAnimation(final ShortcutInfo destInfo, final View destView,
-            final ShortcutInfo srcInfo, final DragView srcView, Rect dstRect,
+            final ShortcutInfo srcInfo, final View srcView, Rect dstRect,
             float scaleRelativeToDragLayer, Runnable postAnimationRunnable) {
 
         Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
         computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), destView.getMeasuredWidth());
 
         // This will animate the dragView (srcView) into the new folder
-        onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable, null);
+        onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable);
 
         // This will animate the first item from it's position as an icon into its
         // position as the first item in the preview
@@ -320,9 +320,8 @@
         mFolderRingAnimator.animateToNaturalState();
     }
 
-    private void onDrop(final ShortcutInfo item, DragView animateView, Rect finalRect,
-            float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable,
-            DragObject d) {
+    private void onDrop(final ShortcutInfo item, View animateView, Rect finalRect,
+            float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable) {
         item.cellX = -1;
         item.cellY = -1;
 
@@ -383,7 +382,7 @@
             item = (ShortcutInfo) d.dragInfo;
         }
         mFolder.notifyDrop();
-        onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable, d);
+        onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable);
     }
 
     public DropTarget getDropTargetDelegate(DragObject d) {
diff --git a/src/com/android/launcher2/InfoDropTarget.java b/src/com/android/launcher2/InfoDropTarget.java
index 21fe8ba..dba845b 100644
--- a/src/com/android/launcher2/InfoDropTarget.java
+++ b/src/com/android/launcher2/InfoDropTarget.java
@@ -29,7 +29,6 @@
 import android.view.ViewGroup;
 
 import com.android.launcher.R;
-import com.android.launcher2.DropTarget.DragObject;
 
 public class InfoDropTarget extends ButtonDropTarget {
 
@@ -86,9 +85,6 @@
         if (componentName != null) {
             mLauncher.startApplicationDetailsActivity(componentName);
         }
-
-        // There is no post-drop animation, so clean up the DragView now
-        d.deferDragViewCleanupPostAnimation = false;
         return false;
     }
 
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index 41a2bfb..746c682 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -169,6 +169,7 @@
     private Bitmap mDragOutline = null;
     private final Rect mTempRect = new Rect();
     private final int[] mTempXY = new int[2];
+    private int mDragViewMultiplyColor;
     private float mOverscrollFade = 0;
 
     // Paint used to draw external drop outline
@@ -290,6 +291,7 @@
 
         mSpringLoadedShrinkFactor =
             res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
+        mDragViewMultiplyColor = res.getColor(R.color.drag_view_multiply_color);
 
         // if the value is manually specified, use that instead
         cellCountX = a.getInt(R.styleable.Workspace_cellCountX, cellCountX);
@@ -1792,6 +1794,7 @@
         canvas.setBitmap(b);
         drawDragView(v, canvas, padding, true);
         mOutlineHelper.applyOuterBlur(b, canvas, outlineColor);
+        canvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
         canvas.setBitmap(null);
 
         return b;
@@ -2244,7 +2247,6 @@
                 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration,
                         disableHardwareLayersRunnable, this);
             } else {
-                d.deferDragViewCleanupPostAnimation = false;
                 cell.setVisibility(VISIBLE);
             }
             parent.onDropChild(cell);