I need to migrate my old java code that creates a Drawable instance which is a gradient with a starting color in the upper part and a finishing color in the bottom part.
It worked perfectly in java, but after migrating the code to kotlin and compose, getDrawable function returns an empty transparent drawable:
override fun getDrawable(): Drawable {
val sf: ShaderFactory = object : ShaderFactory() {
override fun resize(width: Int, height: Int): Shader {
return LinearGradient(
startX(width), startY(height), endX(width), endY(height), intArrayOf(
color1, color2
),
null, Shader.TileMode.MIRROR
)
}
}
val p = PaintDrawable()
p.shape = RectShape()
p.shaderFactory = sf
return p
}
fun startX(actualWidth: Int): Float {
when (type) {
Type.SOLID -> return 0f
Type.DEGREE_DOWN_UP, Type.DEGREE_UP_DOWN -> return actualWidth / 2.0f
Type.DEGREE_LEFT_RIGHT -> return 0.0f
Type.DEGREE_RIGHT_LEFT -> return actualWidth.toFloat()
else -> {}
}
return (-1).toFloat()
}
fun startY(actualHeight: Int): Float {
when (type) {
Type.SOLID -> return 0f
Type.DEGREE_DOWN_UP -> return actualHeight.toFloat()
Type.DEGREE_LEFT_RIGHT, Type.DEGREE_RIGHT_LEFT -> return actualHeight / 2.0f
Type.DEGREE_UP_DOWN -> return 0f
else -> {}
}
return (-1).toFloat()
}
fun endX(actualWidth: Int): Float {
when (type) {
Type.SOLID -> return actualWidth.toFloat()
Type.DEGREE_DOWN_UP, Type.DEGREE_UP_DOWN -> return actualWidth / 2.0f
Type.DEGREE_LEFT_RIGHT -> return actualWidth.toFloat()
Type.DEGREE_RIGHT_LEFT -> return 0f
else -> {}
}
return (-1).toFloat()
}
fun endY(actualHeight: Int): Float {
when (type) {
Type.SOLID -> return actualHeight.toFloat()
Type.DEGREE_DOWN_UP -> return 0f
Type.DEGREE_UP_DOWN -> return actualHeight.toFloat()
Type.DEGREE_LEFT_RIGHT, Type.DEGREE_RIGHT_LEFT -> return actualHeight / 2.0f
else -> {}
}
return (-1).toFloat()
}
The type of the drawable is DEGREE_UP_DOWN
What is wrong in the code?