Files
hackathon-gituber-4fbb7edea…/app/src/main/java/com/example/basurapp/views/TruckProgressView.kt
2026-05-22 22:09:49 -06:00

83 lines
2.4 KiB
Kotlin

package com.example.basurapp.views
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.example.basurapp.R
class TruckProgressView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private var stopCount: Int = 4
private var truckPosition: Int = 0
private val linePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.BLACK
strokeWidth = 6f
}
private val stopPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.BLACK
style = Paint.Style.FILL
}
private val truckBodyPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = context.getColor(R.color.green_primary)
style = Paint.Style.FILL
}
private val truckCabinPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = context.getColor(R.color.green_dark)
style = Paint.Style.FILL
}
private val wheelPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.BLACK
style = Paint.Style.FILL
}
fun setRoute(stops: Int, position: Int) {
stopCount = stops.coerceAtLeast(2)
truckPosition = position.coerceIn(0, stopCount - 1)
invalidate()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val w = width.toFloat()
val h = height.toFloat()
val padding = 40f
val lineY = h / 2 + 20
// Línea horizontal
canvas.drawLine(padding, lineY, w - padding, lineY, linePaint)
// Paradas (círculos)
val step = (w - padding * 2) / (stopCount - 1)
for (i in 0 until stopCount) {
val cx = padding + step * i
canvas.drawCircle(cx, lineY, 14f, stopPaint)
}
// Camión sobre la parada actual
val truckX = padding + step * truckPosition
drawTruck(canvas, truckX, lineY - 30f)
}
private fun drawTruck(canvas: Canvas, x: Float, y: Float) {
// Caja del camión
canvas.drawRect(x - 30f, y - 20f, x + 10f, y + 10f, truckBodyPaint)
// Cabina
canvas.drawRect(x + 10f, y - 10f, x + 30f, y + 10f, truckCabinPaint)
// Ruedas
canvas.drawCircle(x - 20f, y + 12f, 6f, wheelPaint)
canvas.drawCircle(x + 20f, y + 12f, 6f, wheelPaint)
}
}