1 /*
2  * Copyright (C) 2021 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.icons;
18 
19 import android.graphics.Canvas;
20 import android.graphics.Path;
21 import android.graphics.Rect;
22 import android.graphics.RectF;
23 import android.graphics.drawable.Drawable;
24 import android.graphics.drawable.DrawableWrapper;
25 
26 /**
27  * A drawable which clips rounded corner around a child drawable
28  */
29 public class RoundDrawableWrapper extends DrawableWrapper {
30 
31     private final RectF mTempRect = new RectF();
32     private final Path mClipPath = new Path();
33     private final float mRoundedCornersRadius;
34 
RoundDrawableWrapper(Drawable dr, float radius)35     public RoundDrawableWrapper(Drawable dr, float radius) {
36         super(dr);
37         mRoundedCornersRadius = radius;
38     }
39 
40     @Override
onBoundsChange(Rect bounds)41     protected void onBoundsChange(Rect bounds) {
42         mTempRect.set(getBounds());
43         mClipPath.reset();
44         mClipPath.addRoundRect(mTempRect, mRoundedCornersRadius,
45                 mRoundedCornersRadius, Path.Direction.CCW);
46         super.onBoundsChange(bounds);
47     }
48 
49     @Override
draw(Canvas canvas)50     public final void draw(Canvas canvas) {
51         int saveCount = canvas.save();
52         canvas.clipPath(mClipPath);
53         super.draw(canvas);
54         canvas.restoreToCount(saveCount);
55     }
56 }
57