Matt Pietal | 5bdfba4 | 2020-02-14 09:14:43 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.controls.ui |
| 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 | * Use a path to add mask for corners around the drawable, to match the radius |
| 28 | * of the underlying shape. |
| 29 | */ |
| 30 | class 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 | } |