1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.launcher3.shortcuts; 18 19import android.graphics.Bitmap; 20import android.graphics.Canvas; 21import android.graphics.Point; 22import android.graphics.Rect; 23import android.graphics.drawable.Drawable; 24import android.view.View; 25 26import com.android.launcher3.graphics.HolographicOutlineHelper; 27import com.android.launcher3.Launcher; 28import com.android.launcher3.Utilities; 29import com.android.launcher3.graphics.DragPreviewProvider; 30 31/** 32 * Extension of {@link DragPreviewProvider} which generates bitmaps scaled to the default icon size. 33 */ 34public class ShortcutDragPreviewProvider extends DragPreviewProvider { 35 36 private final Point mPositionShift; 37 38 public ShortcutDragPreviewProvider(View icon, Point shift) { 39 super(icon); 40 mPositionShift = shift; 41 } 42 43 @Override 44 public Bitmap createDragOutline(Canvas canvas) { 45 Bitmap b = drawScaledPreview(canvas, Bitmap.Config.ALPHA_8); 46 47 HolographicOutlineHelper.getInstance(mView.getContext()) 48 .applyExpensiveOutlineWithBlur(b, canvas); 49 canvas.setBitmap(null); 50 return b; 51 } 52 53 @Override 54 public Bitmap createDragBitmap(Canvas canvas) { 55 Bitmap b = drawScaledPreview(canvas, Bitmap.Config.ARGB_8888); 56 canvas.setBitmap(null); 57 return b; 58 } 59 60 private Bitmap drawScaledPreview(Canvas canvas, Bitmap.Config config) { 61 Drawable d = mView.getBackground(); 62 Rect bounds = getDrawableBounds(d); 63 64 int size = Launcher.getLauncher(mView.getContext()).getDeviceProfile().iconSizePx; 65 66 final Bitmap b = Bitmap.createBitmap( 67 size + blurSizeOutline, 68 size + blurSizeOutline, 69 config); 70 71 canvas.setBitmap(b); 72 canvas.save(Canvas.MATRIX_SAVE_FLAG); 73 canvas.translate(blurSizeOutline / 2, blurSizeOutline / 2); 74 canvas.scale(((float) size) / bounds.width(), ((float) size) / bounds.height(), 0, 0); 75 canvas.translate(bounds.left, bounds.top); 76 d.draw(canvas); 77 canvas.restore(); 78 return b; 79 } 80 81 @Override 82 public float getScaleAndPosition(Bitmap preview, int[] outPos) { 83 Launcher launcher = Launcher.getLauncher(mView.getContext()); 84 int iconSize = getDrawableBounds(mView.getBackground()).width(); 85 float scale = launcher.getDragLayer().getLocationInDragLayer(mView, outPos); 86 87 int iconLeft = mView.getPaddingStart(); 88 if (Utilities.isRtl(mView.getResources())) { 89 iconLeft = mView.getWidth() - iconSize - iconLeft; 90 } 91 92 outPos[0] += Math.round(scale * iconLeft + (scale * iconSize - preview.getWidth()) / 2 + 93 mPositionShift.x); 94 outPos[1] += Math.round((scale * mView.getHeight() - preview.getHeight()) / 2 95 + mPositionShift.y); 96 float size = launcher.getDeviceProfile().iconSizePx; 97 return scale * iconSize / size; 98 } 99} 100