Skip to content

SergeyBurlaka/Arc-Gradient-Sample-Android-canvas-Gradient-Geometric-Arc-Gui

Repository files navigation

Arc-Gradient-Sample-Android-canvas-Gradient-Geometric-Arc-Gui

Screenshot

describing

In my cause i I had to draw someone like this:

Maybe you too.

So, let's think! How does Sweep Gradient works? If you draw rect via this:

         private val colors = intArrayOf(ContextCompat.getColor(context, R.color.progress_from_color),
     ContextCompat.getColor(context, R.color.progress_to_color))
    
        private var positions = floatArrayOf(0.0f, 1.0f)
        private var sweepGradient : SweepGradient? = null
        
        sweepGradient = SweepGradient(w / 2F,h / 2F,colors,  positions)
        

It will be:

enter image description here

So the idea is to rotate it!

     sweepGradient.apply {
                val rotate = 270f
                val gradientMatrix = Matrix()
                gradientMatrix.preRotate(rotate, mWidth / 2F, mHeight / 2F)
                setLocalMatrix(gradientMatrix)
            }

Finally we can draw our arc:

     mPaint.shader = gradient
     canvas.drawArc(rectF, startAngle, finishedSweepAngle, false, mPaint)

Releases

No releases published

Packages

No packages published

Languages