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.picker.option.ui.view 18 19 import android.animation.ArgbEvaluator 20 import android.content.Context 21 import android.graphics.Canvas 22 import android.graphics.Paint 23 import android.util.AttributeSet 24 import android.view.View 25 import com.android.wallpaper.R 26 27 open class OptionItemBackground 28 @JvmOverloads 29 constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : 30 View(context, attrs, defStyleAttr) { 31 32 private var colorUnselected = 33 context.resources.getColor(R.color.system_surface_container_high, null) 34 private var colorSelected = context.resources.getColor(R.color.system_primary_fixed_dim, null) 35 private val argbEvaluator = ArgbEvaluator() <lambda>null36 private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL } 37 38 // progress 0 is unselected and 1 is selected 39 var progress = 0f 40 private set 41 setUnselectedColornull42 fun setUnselectedColor(unselectedColor: Int) { 43 this.colorUnselected = unselectedColor 44 } 45 setSelectedColornull46 fun setSelectedColor(selectedColor: Int) { 47 this.colorSelected = selectedColor 48 } 49 setProgressnull50 fun setProgress(progress: Float) { 51 this.progress = progress 52 invalidate() 53 } 54 onDrawnull55 override fun onDraw(canvas: Canvas) { 56 super.onDraw(canvas) 57 58 val width = width.toFloat() 59 val height = height.toFloat() 60 val cornerRadius = (width / 2) * (1f - 0.25f * progress) 61 paint.color = argbEvaluator.evaluate(progress, colorUnselected, colorSelected) as Int 62 63 canvas.drawRoundRect(0f, 0f, width, height, cornerRadius, cornerRadius, paint) 64 } 65 } 66