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 val colorUnselected = 33 context.resources.getColor(R.color.system_surface_container_high, null) 34 private val colorSelected = context.resources.getColor(R.color.system_primary, 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 setProgressnull42 fun setProgress(progress: Float) { 43 this.progress = progress 44 invalidate() 45 } 46 onDrawnull47 override fun onDraw(canvas: Canvas) { 48 super.onDraw(canvas) 49 50 val width = width.toFloat() 51 val height = height.toFloat() 52 val cornerRadius = (width / 2) * (1f - 0.25f * progress) 53 paint.color = argbEvaluator.evaluate(progress, colorUnselected, colorSelected) as Int 54 55 canvas.drawRoundRect(0f, 0f, width, height, cornerRadius, cornerRadius, paint) 56 } 57 } 58