1 /* 2 * Copyright (C) 2023 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.car.carlauncher; 18 19 import android.graphics.Canvas; 20 import android.graphics.Point; 21 import android.graphics.drawable.Drawable; 22 import android.view.View; 23 24 import androidx.annotation.NonNull; 25 26 /** 27 * Custom View.DragShadowBuilder that handles the drawing and deploying of drag shadow when an 28 * app icon is long pressed and dragged. 29 */ 30 public class AppItemDragShadowBuilder extends View.DragShadowBuilder{ 31 private final Drawable mIcon; 32 private final int mScaledSize; 33 private final float mTouchPointX; 34 private final float mTouchPointY; 35 36 /** 37 * @param icon Drawable to be drawn as the drag shadow to represent the view being dragged. 38 */ AppItemDragShadowBuilder(Drawable icon, float touchPointX, float touchPointY, int scaledSize)39 public AppItemDragShadowBuilder(Drawable icon, float touchPointX, float touchPointY, 40 int scaledSize) { 41 super(); 42 mIcon = icon; 43 mScaledSize = scaledSize; 44 mTouchPointX = touchPointX; 45 mTouchPointY = touchPointY; 46 } 47 48 @Override onProvideShadowMetrics(Point outShadowSize, Point outShadowTouchPoint)49 public void onProvideShadowMetrics(Point outShadowSize, Point outShadowTouchPoint) { 50 outShadowSize.set(mScaledSize, mScaledSize); 51 outShadowTouchPoint.set((int) mTouchPointX, (int) mTouchPointY); 52 } 53 54 @Override onDrawShadow(@onNull Canvas canvas)55 public void onDrawShadow(@NonNull Canvas canvas) { 56 mIcon.setBounds(0, 0, mScaledSize, mScaledSize); 57 mIcon.draw(canvas); 58 } 59 } 60