Skip to content

Instantly share code, notes, and snippets.

@akexorcist
Created October 5, 2021 15:44
Show Gist options
  • Save akexorcist/d52db127446fa173e518c4d347388d45 to your computer and use it in GitHub Desktop.
Save akexorcist/d52db127446fa173e518c4d347388d45 to your computer and use it in GitHub Desktop.
Gradient color view for HSL color picker
class QuadGradientView : FrameLayout {
private var currentHue = 0f
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
setWillNotDraw(false)
}
constructor(context: Context) : super(context) {
setWillNotDraw(false)
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
setWillNotDraw(false)
}
private val paint: Paint by lazy {
Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.FILL
shader = ComposeShader(
LinearGradient(
0f,
0f,
width.toFloat(),
0f,
Color.argb(255, 255, 255, 255),
Color.argb(0, 255, 255, 255),
Shader.TileMode.CLAMP
),
LinearGradient(
0f,
height.toFloat(),
0f,
0f,
Color.argb(255, 0, 0, 0),
Color.argb(0, 0, 0, 0),
Shader.TileMode.CLAMP
),
PorterDuff.Mode.SRC_OVER
)
}
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.drawColor(Color.HSVToColor(floatArrayOf(currentHue, 1f, 1f)))
canvas.drawPaint(paint)
}
}
@akexorcist
Copy link
Author

Example

view

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment