用Kotlin帮我写一个Android自定义View进度条,进度条颜色是渐变色,宽高可以自行设置
时间: 2023-04-10 17:02:38 浏览: 65
好的,以下是用 Kotlin 编写的 Android 自定义 View 进度条的代码:
```kotlin
class GradientProgressBar(context: Context, attrs: AttributeSet) : View(context, attrs) {
private var progress = 0f
private var maxProgress = 100f
private var barHeight = 10.dpToPx()
private var barCornerRadius = 5.dpToPx()
private var barBackgroundColor = Color.GRAY
private var barGradientColors = intArrayOf(Color.BLUE, Color.GREEN, Color.YELLOW, Color.RED)
private val backgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.FILL
color = barBackgroundColor
}
private val gradientPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.FILL
}
private val rectF = RectF()
init {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.GradientProgressBar)
barHeight = typedArray.getDimensionPixelSize(R.styleable.GradientProgressBar_barHeight, barHeight)
barCornerRadius = typedArray.getDimensionPixelSize(R.styleable.GradientProgressBar_barCornerRadius, barCornerRadius)
barBackgroundColor = typedArray.getColor(R.styleable.GradientProgressBar_barBackgroundColor, barBackgroundColor)
barGradientColors = typedArray.getColorArray(R.styleable.GradientProgressBar_barGradientColors) ?: barGradientColors
typedArray.recycle()
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val height = paddingTop + paddingBottom + barHeight
val width = View.getDefaultSize(suggestedMinimumWidth, widthMeasureSpec)
setMeasuredDimension(width, height)
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
rectF.set(paddingLeft.toFloat(), paddingTop.toFloat(), (width - paddingRight).toFloat(), (height - paddingBottom).toFloat())
// Draw background
canvas.drawRoundRect(rectF, barCornerRadius.toFloat(), barCornerRadius.toFloat(), backgroundPaint)
// Draw gradient
val gradient = LinearGradient(rectF.left, rectF.top, rectF.right, rectF.bottom, barGradientColors, null, Shader.TileMode.CLAMP)
gradientPaint.shader = gradient
val progressWidth = progress / maxProgress * rectF.width()
canvas.drawRoundRect(rectF.left, rectF.top, rectF.left + progressWidth, rectF.bottom, barCornerRadius.toFloat(), barCornerRadius.toFloat(), gradientPaint)
}
fun setProgress(progress: Float) {
this.progress = progress
invalidate()
}
fun setMaxProgress(maxProgress: Float) {
this.maxProgress = maxProgress
invalidate()
}
private fun Int.dpToPx(): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), resources.displayMetrics).toInt()
}
private fun TypedArray.getColorArray(@StyleableRes index: Int): IntArray? {
val resourceId = getResourceId(index, 0)
return if (resourceId != 0) {
resources.getIntArray(resourceId).map { Color.parseColor("#$it") }.toIntArray()
} else {
null
}
}
}
```
在 XML 中使用时,可以这样:
```xml
<com.example.GradientProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:barBackgroundColor="#CCCCCC"
app:barGradientColors="@array/gradient_colors"
app:barHeight="20dp"
app:barCornerRadius="10dp"/>
```
其中,`app:barGradientColors` 是一个颜色数组,可以在 `res/values/arrays.xml` 中定义:
```xml
<array name="gradient_colors">
<item>#FF0000</item>
<item>#FFFF00</item>
<item>#00FF00</item>
<item>#0000FF</item>
</array>
```