1 /*
2  * Copyright 2022 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.quickstep.views;
18 
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapShader;
22 import android.graphics.Canvas;
23 import android.graphics.Matrix;
24 import android.graphics.Paint;
25 import android.graphics.Shader;
26 import android.util.AttributeSet;
27 import android.view.View;
28 
29 import androidx.annotation.Nullable;
30 
31 /**
32  * A child view of {@link com.android.quickstep.views.FloatingTaskView} to draw the thumbnail in a
33  * rounded corner frame. While the purpose of this class sounds similar to
34  * {@link TaskThumbnailViewDeprecated}, it doesn't need a lot of complex logic in {@link TaskThumbnailViewDeprecated}
35  * in relation to moving with {@link RecentsView}.
36  */
37 public class FloatingTaskThumbnailView extends View {
38 
39     private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
40     private final Matrix mMatrix = new Matrix();
41 
42     private @Nullable BitmapShader mBitmapShader;
43     private @Nullable Bitmap mBitmap;
44 
FloatingTaskThumbnailView(Context context)45     public FloatingTaskThumbnailView(Context context) {
46         this(context, null);
47     }
48 
FloatingTaskThumbnailView(Context context, AttributeSet attrs)49     public FloatingTaskThumbnailView(Context context, AttributeSet attrs) {
50         this(context, attrs, 0);
51     }
52 
FloatingTaskThumbnailView(Context context, AttributeSet attrs, int defStyleAttr)53     public FloatingTaskThumbnailView(Context context, AttributeSet attrs, int defStyleAttr) {
54         super(context, attrs, defStyleAttr);
55     }
56 
57     @Override
onDraw(Canvas canvas)58     protected void onDraw(Canvas canvas) {
59         if (mBitmap == null) {
60             return;
61         }
62 
63         // Scale down the bitmap to fix x, and crop in y.
64         float scale = 1.0f * getMeasuredWidth() / mBitmap.getWidth();
65         mMatrix.reset();
66         mMatrix.postScale(scale, scale);
67         mBitmapShader.setLocalMatrix(mMatrix);
68 
69         FloatingTaskView parent = (FloatingTaskView) getParent();
70         parent.drawRoundedRect(canvas, mPaint);
71     }
72 
setThumbnail(Bitmap bitmap)73     public void setThumbnail(Bitmap bitmap) {
74         mBitmap = bitmap;
75         if (bitmap != null) {
76             mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
77             mPaint.setShader(mBitmapShader);
78         }
79     }
80 }
81