androidautomatic-ref-countinggradientshapedrawable

How can I draw a progressing ring with a gradient in Android?


I have a ShapeDrawable Object in my class in the form of an arc.

ShapeDrawable progressArc = new ShapeDrawable(new ArcShape(120, 12));

Now I would like to use a radial gradient effect on the color and also display just a ring instead of a filled arc. Does the Android SDK provide such a feature?

Currently using the below code, I get a gradient on the circle.

progressArc.setIntrinsicHeight(500);
progressArc.setIntrinsicWidth(500);

Shader shader1 = new LinearGradient(5, -5, 35, 0, new int[] { 0xFF33B5E5,0xFFFFFFFF }, null, Shader.TileMode.MIRROR);
progressArc.getPaint().setShader(shader1);
progressArc.getPaint().setStrokeWidth(1);
progressArc.getPaint().setColor(Color.RED);
progressArc.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);

Solution

  • You can use a SweepGradient to get a radial gradient:

    Shader s = new SweepGradient(0, 0, new int[] { 0xFF33B5E5,0xFFFFFFFF }, null);
    //or if you only use two colors:
    Shader s = new SweepGradient(0, 0, 0xFF33B5E5, 0xFFFFFFFF);
    

    To avoid filling, just use the Paint.Style.STROKE style, with appropriate strokeWidth.