2016-07-20 14:55:26 -07:00
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
|
|
2019-08-15 14:53:41 -07:00
|
|
|
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
|
|
|
|
|
|
2016-10-12 20:35:59 -07:00
|
|
|
import android.content.Context;
|
2016-07-20 14:55:26 -07:00
|
|
|
import android.graphics.Bitmap;
|
2017-09-26 12:43:16 -07:00
|
|
|
import android.graphics.BlurMaskFilter;
|
2016-07-20 14:55:26 -07:00
|
|
|
import android.graphics.Canvas;
|
2017-09-26 12:43:16 -07:00
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
|
import android.graphics.PorterDuffXfermode;
|
2016-07-20 14:55:26 -07:00
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
|
2017-06-23 14:57:38 -07:00
|
|
|
import com.android.launcher3.BubbleTextView;
|
2016-07-22 10:50:11 -07:00
|
|
|
import com.android.launcher3.Launcher;
|
2016-10-12 20:35:59 -07:00
|
|
|
import com.android.launcher3.R;
|
2017-03-06 16:56:39 -08:00
|
|
|
import com.android.launcher3.config.FeatureFlags;
|
2020-02-19 08:40:49 -08:00
|
|
|
import com.android.launcher3.dragndrop.DraggableView;
|
2018-11-07 13:15:46 -08:00
|
|
|
import com.android.launcher3.icons.BitmapRenderer;
|
2018-03-06 10:28:34 -08:00
|
|
|
import com.android.launcher3.widget.LauncherAppWidgetHostView;
|
2016-07-20 14:55:26 -07:00
|
|
|
|
2017-09-26 12:43:16 -07:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
|
2016-07-20 14:55:26 -07:00
|
|
|
/**
|
|
|
|
|
* A utility class to generate preview bitmap for dragging.
|
|
|
|
|
*/
|
|
|
|
|
public class DragPreviewProvider {
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2020-02-19 08:40:49 -08:00
|
|
|
public final int blurSizeOutline;
|
2016-10-12 20:35:59 -07:00
|
|
|
|
2017-09-26 12:43:16 -07:00
|
|
|
private OutlineGeneratorCallback mOutlineGeneratorCallback;
|
2016-10-05 16:27:48 -07:00
|
|
|
public Bitmap generatedDragOutline;
|
2016-08-11 16:02:02 -07:00
|
|
|
|
2016-07-20 14:55:26 -07:00
|
|
|
public DragPreviewProvider(View view) {
|
2016-10-12 20:35:59 -07:00
|
|
|
this(view, view.getContext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DragPreviewProvider(View view, Context context) {
|
2016-07-20 14:55:26 -07:00
|
|
|
mView = view;
|
2016-10-12 20:35:59 -07:00
|
|
|
blurSizeOutline =
|
|
|
|
|
context.getResources().getDimensionPixelSize(R.dimen.blur_size_medium_outline);
|
2020-02-19 08:40:49 -08:00
|
|
|
previewPadding = blurSizeOutline;
|
2016-07-20 14:55:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-07-22 10:50:11 -07:00
|
|
|
* Draws the {@link #mView} into the given {@param destCanvas}.
|
2016-07-20 14:55:26 -07:00
|
|
|
*/
|
2018-03-26 18:29:17 +02:00
|
|
|
protected void drawDragView(Canvas destCanvas, float scale) {
|
2020-02-19 08:40:49 -08:00
|
|
|
int saveCount = destCanvas.save();
|
2017-11-08 16:52:34 -08:00
|
|
|
destCanvas.scale(scale, scale);
|
|
|
|
|
|
2020-02-19 08:40:49 -08:00
|
|
|
if (mView instanceof DraggableView) {
|
|
|
|
|
DraggableView dv = (DraggableView) mView;
|
|
|
|
|
dv.prepareDrawDragView();
|
2020-05-07 11:55:19 -07:00
|
|
|
dv.getSourceVisualDragBounds(mTempRect);
|
2020-02-19 08:40:49 -08:00
|
|
|
destCanvas.translate(blurSizeOutline / 2 - mTempRect.left,
|
|
|
|
|
blurSizeOutline / 2 - mTempRect.top);
|
2016-07-20 14:55:26 -07:00
|
|
|
mView.draw(destCanvas);
|
|
|
|
|
}
|
2020-02-19 08:40:49 -08:00
|
|
|
destCanvas.restoreToCount(saveCount);
|
2016-07-20 14:55:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-07-22 10:50:11 -07:00
|
|
|
* Returns a new bitmap to show when the {@link #mView} is being dragged around.
|
2016-07-20 14:55:26 -07:00
|
|
|
* Responsibility for the bitmap is transferred to the caller.
|
|
|
|
|
*/
|
2017-09-26 12:43:16 -07:00
|
|
|
public Bitmap createDragBitmap() {
|
2020-02-19 08:40:49 -08:00
|
|
|
int width = 0;
|
|
|
|
|
int height = 0;
|
2020-04-08 18:00:30 -07:00
|
|
|
// Assume scaleX == scaleY, which is always the case for workspace items.
|
|
|
|
|
float scale = mView.getScaleX();
|
2020-02-19 08:40:49 -08:00
|
|
|
if (mView instanceof DraggableView) {
|
2020-05-07 11:55:19 -07:00
|
|
|
((DraggableView) mView).getSourceVisualDragBounds(mTempRect);
|
2020-02-19 08:40:49 -08:00
|
|
|
width = mTempRect.width();
|
|
|
|
|
height = mTempRect.height();
|
|
|
|
|
} else {
|
|
|
|
|
width = mView.getWidth();
|
|
|
|
|
height = mView.getHeight();
|
2017-11-08 16:52:34 -08:00
|
|
|
}
|
2016-07-20 14:55:26 -07:00
|
|
|
|
2018-03-06 10:28:34 -08:00
|
|
|
return BitmapRenderer.createHardwareBitmap(width + blurSizeOutline,
|
2020-04-08 18:00:30 -07:00
|
|
|
height + blurSizeOutline, (c) -> drawDragView(c, scale));
|
2016-07-20 14:55:26 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-26 12:43:16 -07:00
|
|
|
public final void generateDragOutline(Bitmap preview) {
|
2020-02-14 14:15:13 -08:00
|
|
|
if (FeatureFlags.IS_STUDIO_BUILD && mOutlineGeneratorCallback != null) {
|
2016-08-11 16:02:02 -07:00
|
|
|
throw new RuntimeException("Drag outline generated twice");
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 12:43:16 -07:00
|
|
|
mOutlineGeneratorCallback = new OutlineGeneratorCallback(preview);
|
2019-08-15 14:53:41 -07:00
|
|
|
UI_HELPER_EXECUTOR.post(mOutlineGeneratorCallback);
|
2016-07-20 14:55:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
return bounds;
|
|
|
|
|
}
|
2016-07-22 10:50:11 -07:00
|
|
|
|
|
|
|
|
public float getScaleAndPosition(Bitmap preview, int[] outPos) {
|
|
|
|
|
float scale = Launcher.getLauncher(mView.getContext())
|
|
|
|
|
.getDragLayer().getLocationInDragLayer(mView, outPos);
|
2016-12-05 12:04:44 -08:00
|
|
|
if (mView instanceof LauncherAppWidgetHostView) {
|
|
|
|
|
// App widgets are technically scaled, but are drawn at their expected size -- so the
|
|
|
|
|
// app widget scale should not affect the scale of the preview.
|
2016-12-19 14:12:05 -08:00
|
|
|
scale /= ((LauncherAppWidgetHostView) mView).getScaleToFit();
|
2016-12-05 12:04:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outPos[0] = Math.round(outPos[0] -
|
|
|
|
|
(preview.getWidth() - scale * mView.getWidth() * mView.getScaleX()) / 2);
|
|
|
|
|
outPos[1] = Math.round(outPos[1] - (1 - scale) * preview.getHeight() / 2
|
|
|
|
|
- previewPadding / 2);
|
2016-07-22 10:50:11 -07:00
|
|
|
return scale;
|
|
|
|
|
}
|
2017-09-26 12:43:16 -07:00
|
|
|
|
|
|
|
|
protected Bitmap convertPreviewToAlphaBitmap(Bitmap preview) {
|
|
|
|
|
return preview.copy(Bitmap.Config.ALPHA_8, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class OutlineGeneratorCallback implements Runnable {
|
|
|
|
|
|
|
|
|
|
private final Bitmap mPreviewSnapshot;
|
|
|
|
|
private final Context mContext;
|
2019-09-25 13:30:55 -07:00
|
|
|
private final boolean mIsIcon;
|
2017-09-26 12:43:16 -07:00
|
|
|
|
|
|
|
|
OutlineGeneratorCallback(Bitmap preview) {
|
|
|
|
|
mPreviewSnapshot = preview;
|
|
|
|
|
mContext = mView.getContext();
|
2019-09-25 13:30:55 -07:00
|
|
|
mIsIcon = mView instanceof BubbleTextView;
|
2017-09-26 12:43:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
Bitmap preview = convertPreviewToAlphaBitmap(mPreviewSnapshot);
|
2019-09-25 13:30:55 -07:00
|
|
|
if (mIsIcon) {
|
|
|
|
|
int size = Launcher.getLauncher(mContext).getDeviceProfile().iconSizePx;
|
|
|
|
|
preview = Bitmap.createScaledBitmap(preview, size, size, false);
|
|
|
|
|
}
|
|
|
|
|
//else case covers AppWidgetHost (doesn't drag/drop across different device profiles)
|
2017-09-26 12:43:16 -07:00
|
|
|
|
|
|
|
|
// We start by removing most of the alpha channel so as to ignore shadows, and
|
|
|
|
|
// other types of partial transparency when defining the shape of the object
|
|
|
|
|
byte[] pixels = new byte[preview.getWidth() * preview.getHeight()];
|
|
|
|
|
ByteBuffer buffer = ByteBuffer.wrap(pixels);
|
|
|
|
|
buffer.rewind();
|
|
|
|
|
preview.copyPixelsToBuffer(buffer);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < pixels.length; i++) {
|
|
|
|
|
if ((pixels[i] & 0xFF) < 188) {
|
|
|
|
|
pixels[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer.rewind();
|
|
|
|
|
preview.copyPixelsFromBuffer(buffer);
|
|
|
|
|
|
|
|
|
|
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
|
|
|
|
|
Canvas canvas = new Canvas();
|
|
|
|
|
|
|
|
|
|
// calculate the outer blur first
|
|
|
|
|
paint.setMaskFilter(new BlurMaskFilter(blurSizeOutline, BlurMaskFilter.Blur.OUTER));
|
|
|
|
|
int[] outerBlurOffset = new int[2];
|
|
|
|
|
Bitmap thickOuterBlur = preview.extractAlpha(paint, outerBlurOffset);
|
|
|
|
|
|
|
|
|
|
paint.setMaskFilter(new BlurMaskFilter(
|
|
|
|
|
mContext.getResources().getDimension(R.dimen.blur_size_thin_outline),
|
|
|
|
|
BlurMaskFilter.Blur.OUTER));
|
|
|
|
|
int[] brightOutlineOffset = new int[2];
|
|
|
|
|
Bitmap brightOutline = preview.extractAlpha(paint, brightOutlineOffset);
|
|
|
|
|
|
|
|
|
|
// calculate the inner blur
|
|
|
|
|
canvas.setBitmap(preview);
|
|
|
|
|
canvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT);
|
|
|
|
|
paint.setMaskFilter(new BlurMaskFilter(blurSizeOutline, BlurMaskFilter.Blur.NORMAL));
|
|
|
|
|
int[] thickInnerBlurOffset = new int[2];
|
|
|
|
|
Bitmap thickInnerBlur = preview.extractAlpha(paint, thickInnerBlurOffset);
|
|
|
|
|
|
|
|
|
|
// mask out the inner blur
|
|
|
|
|
paint.setMaskFilter(null);
|
|
|
|
|
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
|
|
|
|
|
canvas.setBitmap(thickInnerBlur);
|
|
|
|
|
canvas.drawBitmap(preview, -thickInnerBlurOffset[0],
|
|
|
|
|
-thickInnerBlurOffset[1], paint);
|
|
|
|
|
canvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(), paint);
|
|
|
|
|
canvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1], paint);
|
|
|
|
|
|
|
|
|
|
// draw the inner and outer blur
|
|
|
|
|
paint.setXfermode(null);
|
|
|
|
|
canvas.setBitmap(preview);
|
|
|
|
|
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
|
|
|
|
|
canvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1],
|
|
|
|
|
paint);
|
|
|
|
|
canvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1], paint);
|
|
|
|
|
|
|
|
|
|
// draw the bright outline
|
|
|
|
|
canvas.drawBitmap(brightOutline, brightOutlineOffset[0], brightOutlineOffset[1], paint);
|
|
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
|
canvas.setBitmap(null);
|
|
|
|
|
brightOutline.recycle();
|
|
|
|
|
thickOuterBlur.recycle();
|
|
|
|
|
thickInnerBlur.recycle();
|
|
|
|
|
|
|
|
|
|
generatedDragOutline = preview;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-20 14:55:26 -07:00
|
|
|
}
|