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 17 package com.android.launcher3.shortcuts; 18 19 import android.graphics.Bitmap; 20 import android.graphics.Canvas; 21 import android.graphics.Point; 22 import android.graphics.Rect; 23 import android.graphics.drawable.Drawable; 24 import android.view.View; 25 26 import com.android.launcher3.Launcher; 27 import com.android.launcher3.Utilities; 28 import com.android.launcher3.graphics.DragPreviewProvider; 29 30 /** 31 * Extension of {@link DragPreviewProvider} which generates bitmaps scaled to the default icon size. 32 */ 33 public class ShortcutDragPreviewProvider extends DragPreviewProvider { 34 35 private final Point mPositionShift; 36 ShortcutDragPreviewProvider(View icon, Point shift)37 public ShortcutDragPreviewProvider(View icon, Point shift) { 38 super(icon); 39 mPositionShift = shift; 40 } 41 createDragBitmap()42 public Bitmap createDragBitmap() { 43 Drawable d = mView.getBackground(); 44 Rect bounds = getDrawableBounds(d); 45 46 int size = Launcher.getLauncher(mView.getContext()).getDeviceProfile().iconSizePx; 47 final Bitmap b = Bitmap.createBitmap( 48 size + blurSizeOutline, 49 size + blurSizeOutline, 50 Bitmap.Config.ARGB_8888); 51 52 Canvas canvas = new Canvas(b); 53 canvas.translate(blurSizeOutline / 2, blurSizeOutline / 2); 54 canvas.scale(((float) size) / bounds.width(), ((float) size) / bounds.height(), 0, 0); 55 canvas.translate(bounds.left, bounds.top); 56 d.draw(canvas); 57 return b; 58 } 59 60 @Override getScaleAndPosition(Bitmap preview, int[] outPos)61 public float getScaleAndPosition(Bitmap preview, int[] outPos) { 62 Launcher launcher = Launcher.getLauncher(mView.getContext()); 63 int iconSize = getDrawableBounds(mView.getBackground()).width(); 64 float scale = launcher.getDragLayer().getLocationInDragLayer(mView, outPos); 65 66 int iconLeft = mView.getPaddingStart(); 67 if (Utilities.isRtl(mView.getResources())) { 68 iconLeft = mView.getWidth() - iconSize - iconLeft; 69 } 70 71 outPos[0] += Math.round(scale * iconLeft + (scale * iconSize - preview.getWidth()) / 2 + 72 mPositionShift.x); 73 outPos[1] += Math.round((scale * mView.getHeight() - preview.getHeight()) / 2 74 + mPositionShift.y); 75 float size = launcher.getDeviceProfile().iconSizePx; 76 return scale * iconSize / size; 77 } 78 } 79