blob: af581e1aa53b992d09874ad71ad57832eeb3da15 [file] [log] [blame]
Matt Pietal5bdfba42020-02-14 09:14:43 -05001/*
2 * Copyright (C) 2020 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
17package com.android.systemui.controls.ui
18
19import android.graphics.Canvas
20import android.graphics.Path
21import android.graphics.Rect
22import android.graphics.RectF
23import android.graphics.drawable.Drawable
24import android.graphics.drawable.DrawableWrapper
25
26/**
27 * Use a path to add mask for corners around the drawable, to match the radius
28 * of the underlying shape.
29 */
30class CornerDrawable(val wrapped: Drawable, val cornerRadius: Float) : DrawableWrapper(wrapped) {
31 val path: Path = Path()
32
33 init {
34 val b = getBounds()
35 updatePath(RectF(b))
36 }
37
38 override fun draw(canvas: Canvas) {
39 canvas.clipPath(path)
40 super.draw(canvas)
41 }
42
43 override fun setBounds(l: Int, t: Int, r: Int, b: Int) {
44 updatePath(RectF(l.toFloat(), t.toFloat(), r.toFloat(), b.toFloat()))
45 super.setBounds(l, t, r, b)
46 }
47
48 override fun setBounds(r: Rect) {
49 updatePath(RectF(r))
50 super.setBounds(r)
51 }
52
53 private fun updatePath(r: RectF) {
54 path.reset()
55 path.addRoundRect(r, cornerRadius, cornerRadius, Path.Direction.CW)
56 }
57}