1 /*
2  * Copyright (C) 2024 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.wallpaper.customization.ui.view
18 
19 import android.graphics.Canvas
20 import android.graphics.ColorFilter
21 import android.graphics.Matrix
22 import android.graphics.Paint
23 import android.graphics.Path
24 import android.graphics.PixelFormat
25 import android.graphics.Rect
26 import android.graphics.drawable.Drawable
27 import androidx.core.graphics.PathParser
28 
29 /**
30  * Drawable that draws a shape tile with a given path.
31  *
32  * @param path Path of the shape assuming drawing on a 100x100 canvas.
33  */
34 class ShapeTileDrawable(path: String) : Drawable() {
35 
36     private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
37     private val path = PathParser.createPathFromPathData(path)
38     // The path scaled with regard to the update of drawable bounds
39     private val scaledPath = Path(this.path)
40     private val scaleMatrix = Matrix()
41 
onBoundsChangenull42     override fun onBoundsChange(bounds: Rect) {
43         super.onBoundsChange(bounds)
44         scaleMatrix.setScale(bounds.width() / PATH_SIZE, bounds.height() / PATH_SIZE)
45         path.transform(scaleMatrix, scaledPath)
46     }
47 
drawnull48     override fun draw(canvas: Canvas) {
49         canvas.drawPath(scaledPath, paint)
50     }
51 
setAlphanull52     override fun setAlpha(alpha: Int) {
53         paint.alpha = alpha
54     }
55 
setColorFilternull56     override fun setColorFilter(colorFilter: ColorFilter?) {
57         paint.setColorFilter(colorFilter)
58     }
59 
60     @Deprecated(
61         "getOpacity() is deprecated",
62         ReplaceWith("setAlpha(int)", "android.graphics.drawable.Drawable"),
63     )
getOpacitynull64     override fun getOpacity(): Int {
65         return PixelFormat.TRANSLUCENT
66     }
67 
68     companion object {
69         const val PATH_SIZE = 100f
70     }
71 }
72