Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set gradient style to paint object?

The code for drawing an arrow with Style: Fill is given below:

paint.setColor(Color.parseColor("#bdc0dc"));
paint.setStyle(Style.FILL);
canvas.drawPath(arrowPath, paint);
paint.setColor(Color.BLACK);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
canvas.drawPath(arrowPath, paint);

And the output I obtain is this :

enter image description here

Now what I want do is set style to Gradient(Style.gradient not present in android...) to obtain the arrow similar to the image given below :

enter image description here

How do i do it ? I tried adding style in style.xml but was unable to add gradient there as it accepts item as parameter..

like image 290
Rohan K Avatar asked Dec 22 '11 07:12

Rohan K


People also ask

How do I change gradient in paint?

To draw a gradient, select the Gradient tool and click and drag on the canvas. The gradient will be drawn as a transition between the Primary and Secondary colors ( Color Mode) as the mouse moves. After the mouse button is released, the gradient can be adjusted by dragging the Control Nubs .

How do you add gradient in custom paint flutter?

Flutter painting gradient dart'; // In your paint method final paint = Paint() .. shader = RadialGradient( colors: [ color1, color2, ], ). createShader(Rect. fromCircle( center: offset, radius: radius, ));


2 Answers

use the code below..

paint.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));
    canvas.drawPath(arrowPath, paint);
like image 87
Sujit Avatar answered Oct 08 '22 17:10

Sujit


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)
like image 34
Hitesh Sahu Avatar answered Oct 08 '22 17:10

Hitesh Sahu