How to set gradient style to paint object?

Solution 1:

use the code below..

paint.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));
    canvas.drawPath(arrowPath, paint);

Solution 2:

If you want more than one color:

// Gradient Shade colors distribution setting uniform for now
private val positions = null //floatArrayOf(0f, 0.3f, 0.6f)

// Gradient Shade colors
private val colors = intArrayOf(
        ContextCompat.getColor(context,
                R.color.divider_gradient_start_color),
        ContextCompat.getColor(context,
                R.color.divider_gradient_center_color),
        ContextCompat.getColor(context,
                R.color.divider_gradient_end_color))

in OnDraw()

// Add Shader
gradientPaint.shader = LinearGradient(0f, 0f, measuredWidth.toFloat(),0f, 
                colors, 
                positions,
                Shader.TileMode.CLAMP)

canvas.drawPath(path, gradientPaint)