Created
October 5, 2021 15:44
-
-
Save akexorcist/d52db127446fa173e518c4d347388d45 to your computer and use it in GitHub Desktop.
Gradient color view for HSL color picker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example