summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/graphics/DragPreviewProvider.java
diff options
context:
space:
mode:
authorSunny Goyal <sunnygoyal@google.com>2016-07-20 14:55:26 -0700
committerSunny Goyal <sunnygoyal@google.com>2016-07-21 15:18:24 -0700
commit157793dda450b69da388b859d1c1a7a1083c4ec9 (patch)
treea8d227d6aba5d3bf4a1f4c54ee3ad6daa0d5a1a0 /src/com/android/launcher3/graphics/DragPreviewProvider.java
parent4a44b6e59fc662568a92da36725e940e1dd844db (diff)
downloadandroid_packages_apps_Trebuchet-157793dda450b69da388b859d1c1a7a1083c4ec9.tar.gz
android_packages_apps_Trebuchet-157793dda450b69da388b859d1c1a7a1083c4ec9.tar.bz2
android_packages_apps_Trebuchet-157793dda450b69da388b859d1c1a7a1083c4ec9.zip
Increasing the size of the drag icon when a shortcut is dragged
Change-Id: I7d768657300d3229e05d1eb18aec3720a9098ffc
Diffstat (limited to 'src/com/android/launcher3/graphics/DragPreviewProvider.java')
-rw-r--r--src/com/android/launcher3/graphics/DragPreviewProvider.java155
1 files changed, 155 insertions, 0 deletions
diff --git a/src/com/android/launcher3/graphics/DragPreviewProvider.java b/src/com/android/launcher3/graphics/DragPreviewProvider.java
new file mode 100644
index 000000000..a00bc923a
--- /dev/null
+++ b/src/com/android/launcher3/graphics/DragPreviewProvider.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.launcher3.graphics;
+
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Rect;
+import android.graphics.Region.Op;
+import android.graphics.drawable.Drawable;
+import android.view.View;
+import android.widget.TextView;
+
+import com.android.launcher3.HolographicOutlineHelper;
+import com.android.launcher3.PreloadIconDrawable;
+import com.android.launcher3.R;
+import com.android.launcher3.Workspace;
+import com.android.launcher3.folder.FolderIcon;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * A utility class to generate preview bitmap for dragging.
+ */
+public class DragPreviewProvider {
+
+ public static final int DRAG_BITMAP_PADDING = 2;
+
+ private final Rect mTempRect = new Rect();
+
+ protected final View mView;
+
+ // The padding added to the drag view during the preview generation.
+ public final int previewPadding;
+
+ public DragPreviewProvider(View view) {
+ mView = view;
+
+ if (mView instanceof TextView) {
+ Drawable d = Workspace.getTextViewIcon((TextView) mView);
+ Rect bounds = getDrawableBounds(d);
+ previewPadding = DRAG_BITMAP_PADDING - bounds.left - bounds.top;
+ } else {
+ previewPadding = DRAG_BITMAP_PADDING;
+ }
+ }
+
+ /**
+ * Draw the View v into the given Canvas.
+ *
+ * @param destCanvas the canvas to draw on
+ */
+ private void drawDragView(Canvas destCanvas) {
+ destCanvas.save();
+ if (mView instanceof TextView) {
+ Drawable d = Workspace.getTextViewIcon((TextView) mView);
+ Rect bounds = getDrawableBounds(d);
+ destCanvas.translate(DRAG_BITMAP_PADDING / 2 - bounds.left,
+ DRAG_BITMAP_PADDING / 2 - bounds.top);
+ d.draw(destCanvas);
+ } else {
+ final Rect clipRect = mTempRect;
+ mView.getDrawingRect(clipRect);
+
+ boolean textVisible = false;
+ if (mView instanceof FolderIcon) {
+ // For FolderIcons the text can bleed into the icon area, and so we need to
+ // hide the text completely (which can't be achieved by clipping).
+ if (((FolderIcon) mView).getTextVisible()) {
+ ((FolderIcon) mView).setTextVisible(false);
+ textVisible = true;
+ }
+ }
+ destCanvas.translate(-mView.getScrollX() + DRAG_BITMAP_PADDING / 2,
+ -mView.getScrollY() + DRAG_BITMAP_PADDING / 2);
+ destCanvas.clipRect(clipRect, Op.REPLACE);
+ mView.draw(destCanvas);
+
+ // Restore text visibility of FolderIcon if necessary
+ if (textVisible) {
+ ((FolderIcon) mView).setTextVisible(true);
+ }
+ }
+ destCanvas.restore();
+ }
+
+ /**
+ * Returns a new bitmap to show when the given View is being dragged around.
+ * Responsibility for the bitmap is transferred to the caller.
+ */
+ public Bitmap createDragBitmap(Canvas canvas) {
+ Bitmap b;
+
+ if (mView instanceof TextView) {
+ Drawable d = Workspace.getTextViewIcon((TextView) mView);
+ Rect bounds = getDrawableBounds(d);
+ b = Bitmap.createBitmap(bounds.width() + DRAG_BITMAP_PADDING,
+ bounds.height() + DRAG_BITMAP_PADDING, Bitmap.Config.ARGB_8888);
+ } else {
+ b = Bitmap.createBitmap(mView.getWidth() + DRAG_BITMAP_PADDING,
+ mView.getHeight() + DRAG_BITMAP_PADDING, Bitmap.Config.ARGB_8888);
+ }
+
+ canvas.setBitmap(b);
+ drawDragView(canvas);
+ canvas.setBitmap(null);
+
+ return b;
+ }
+
+ /**
+ * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
+ * Responsibility for the bitmap is transferred to the caller.
+ */
+ public Bitmap createDragOutline(Canvas canvas) {
+ final int outlineColor = mView.getResources().getColor(R.color.outline_color);
+ final Bitmap b = Bitmap.createBitmap(mView.getWidth() + DRAG_BITMAP_PADDING,
+ mView.getHeight() + DRAG_BITMAP_PADDING, Bitmap.Config.ARGB_8888);
+
+ canvas.setBitmap(b);
+ drawDragView(canvas);
+ HolographicOutlineHelper.obtain(mView.getContext())
+ .applyExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor);
+ canvas.setBitmap(null);
+ return b;
+ }
+
+ protected static Rect getDrawableBounds(Drawable d) {
+ Rect bounds = new Rect();
+ d.copyBounds(bounds);
+ if (bounds.width() == 0 || bounds.height() == 0) {
+ bounds.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
+ } else {
+ bounds.offsetTo(0, 0);
+ }
+ if (d instanceof PreloadIconDrawable) {
+ int inset = -((PreloadIconDrawable) d).getOutset();
+ bounds.inset(inset, inset);
+ }
+ return bounds;
+ }
+}