2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.launcher3.folder;
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;
28 import com.android.launcher3.Launcher;
29 import com.android.launcher3.R;
30 import com.android.launcher3.dragndrop.DragLayer;
33 * A temporary view which displays the a bitmap (used for folder icon animation)
35 public class PreviewImageView extends ImageView {
37 private final Rect mTempRect = new Rect();
38 private final DragLayer mParent;
40 private Bitmap mBitmap;
41 private Canvas mCanvas;
43 public PreviewImageView(DragLayer parent) {
44 super(parent.getContext());
48 public void copy(View view) {
49 final int width = view.getMeasuredWidth();
50 final int height = view.getMeasuredHeight();
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);
57 DragLayer.LayoutParams lp;
58 if (getLayoutParams() instanceof DragLayer.LayoutParams) {
59 lp = (DragLayer.LayoutParams) getLayoutParams();
61 lp = new DragLayer.LayoutParams(width, height);
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;
70 lp.width = (int) (scale * width);
71 lp.height = (int) (scale * height);
73 mCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
75 setImageBitmap(mBitmap);
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.
80 mParent.addView(this, lp);
83 public void removeFromParent() {
84 if (mParent.indexOfChild(this) != -1) {
85 mParent.removeView(this);
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);
93 view = new PreviewImageView(dragLayer);
94 dragLayer.setTag(R.id.preview_image_id, view);