1 /*
2  * Copyright (C) 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 
18 package com.android.systemui.common.ui.drawable
19 
20 import android.graphics.Canvas
21 import android.graphics.Path
22 import android.graphics.Rect
23 import android.graphics.drawable.Drawable
24 import android.graphics.drawable.DrawableWrapper
25 import kotlin.math.min
26 
27 /** Renders the wrapped [Drawable] as a circle. */
28 class CircularDrawable(
29     drawable: Drawable,
30 ) : DrawableWrapper(drawable) {
<lambda>null31     private val path: Path by lazy { Path() }
32 
onBoundsChangenull33     override fun onBoundsChange(bounds: Rect) {
34         super.onBoundsChange(bounds)
35         updateClipPath()
36     }
37 
drawnull38     override fun draw(canvas: Canvas) {
39         canvas.save()
40         canvas.clipPath(path)
41         drawable?.draw(canvas)
42         canvas.restore()
43     }
44 
updateClipPathnull45     private fun updateClipPath() {
46         path.reset()
47         path.addCircle(
48             bounds.centerX().toFloat(),
49             bounds.centerY().toFloat(),
50             min(bounds.width(), bounds.height()) / 2f,
51             Path.Direction.CW
52         )
53     }
54 }
55