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.folder; 18 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.Canvas; 22 import android.graphics.PorterDuff; 23 import android.graphics.Rect; 24 import android.view.View; 25 import android.view.ViewGroup; 26 import android.widget.ImageView; 27 28 import com.android.launcher3.Launcher; 29 import com.android.launcher3.R; 30 import com.android.launcher3.dragndrop.DragLayer; 31 32 /** 33 * A temporary view which displays the a bitmap (used for folder icon animation) 34 */ 35 public class PreviewImageView extends ImageView { 36 37 private final Rect mTempRect = new Rect(); 38 private final DragLayer mParent; 39 40 private Bitmap mBitmap; 41 private Canvas mCanvas; 42 PreviewImageView(DragLayer parent)43 public PreviewImageView(DragLayer parent) { 44 super(parent.getContext()); 45 mParent = parent; 46 } 47 copy(View view)48 public void copy(View view) { 49 final int width = view.getMeasuredWidth(); 50 final int height = view.getMeasuredHeight(); 51 52 if (mBitmap == null || mBitmap.getWidth() != width || mBitmap.getHeight() != height) { 53 mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 54 mCanvas = new Canvas(mBitmap); 55 } 56 57 DragLayer.LayoutParams lp; 58 if (getLayoutParams() instanceof DragLayer.LayoutParams) { 59 lp = (DragLayer.LayoutParams) getLayoutParams(); 60 } else { 61 lp = new DragLayer.LayoutParams(width, height); 62 } 63 64 // The layout from which the folder is being opened may be scaled, adjust the starting 65 // view size by this scale factor. 66 float scale = mParent.getDescendantRectRelativeToSelf(view, mTempRect); 67 lp.customPosition = true; 68 lp.x = mTempRect.left; 69 lp.y = mTempRect.top; 70 lp.width = (int) (scale * width); 71 lp.height = (int) (scale * height); 72 73 mCanvas.drawColor(0, PorterDuff.Mode.CLEAR); 74 view.draw(mCanvas); 75 setImageBitmap(mBitmap); 76 77 // Just in case this image view is still in the drag layer from a previous animation, 78 // we remove it and re-add it. 79 removeFromParent(); 80 mParent.addView(this, lp); 81 } 82 removeFromParent()83 public void removeFromParent() { 84 if (mParent.indexOfChild(this) != -1) { 85 mParent.removeView(this); 86 } 87 } 88 get(Context context)89 public static PreviewImageView get(Context context) { 90 DragLayer dragLayer = Launcher.getLauncher(context).getDragLayer(); 91 PreviewImageView view = (PreviewImageView) dragLayer.getTag(R.id.preview_image_id); 92 if (view == null) { 93 view = new PreviewImageView(dragLayer); 94 dragLayer.setTag(R.id.preview_image_id, view); 95 } 96 return view; 97 } 98 } 99