Android SweepGradient and Drawing Lines in a circular pattern -
i trying achieve circular progress bar gradient intervals ui effect
currently have been able achieve this.
the code current implementation below.
import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.colorfilter; import android.graphics.lineargradient; import android.graphics.paint; import android.graphics.path; import android.graphics.pixelformat; import android.graphics.rect; import android.graphics.rectf; import android.graphics.shader; import android.graphics.sweepgradient; import android.graphics.drawable.animationdrawable; import android.support.annotation.intrange; import android.support.annotation.nonnull; import android.support.annotation.nullable; public class cbpdrawable extends animationdrawable { paint paint = new paint(); rectf clip = new rectf(); lineargradient shader; path path; private static final int max = 360; int dots = 75; int dotradius = 20; rectf dotrect; context mcontext; public cbpdrawable(context context){ mcontext = context; } shader gradient; private int mtickoffset = 0; private int mticklength = 15; private int marcradius = 10; double slope, starttickx, startticky, endtickx, endticky, midtickx, midticky, thetainradians; double radiusoffset = marcradius + mtickoffset; @override protected void onboundschange(rect bounds) { super.onboundschange(bounds); path = new path(); dotrect = new rectf(0, 0, dotradius, dotradius); /* (int = 0; < dots; ++i) { start(); path.addroundrect(dotrect, dotradius, dotradius, path.direction.ccw); stop(); } */ (int = 360; >= 0; -= 5) { thetainradians = math.toradians(360 - i); slope = math.tan(thetainradians); starttickx = (radiusoffset * math.cos(thetainradians)); startticky = slope * starttickx; endtickx = starttickx + ((mticklength) * math.cos(thetainradians)); endticky = slope * endtickx; rectf r = new rectf(); r.set((float) starttickx, (float) startticky, (float) endtickx, (float) endticky); path.addroundrect(r, (int)radiusoffset, (int)radiusoffset, path.direction.ccw); } } @override public void draw(@nonnull canvas canvas) { rect b = getbounds(); final int width = canvas.getwidth(); final int height = canvas.getheight(); final int squareside = math.min(width, height); canvas.translate(width / 2f, height / 2f); // moving center of view canvas.rotate(270); final float outerradius = squareside / 2f; final float innerradius = outerradius - dotradius; final float anglefactor = 360f / 72; int[] colors = new int[]{color.blue, color.red}; float[] positions = new float[]{0,0.4f}; gradient = new sweepgradient(outerradius / 2f, outerradius / 2f,colors,null); (int = 0; < 72; ++i) { canvas.save(); // creating "checkpoint" canvas.rotate(anglefactor * i); canvas.translate(innerradius, 0); //moving edge of big circle clip.set(b); paint.setcolor(color.gray); if(anglefactor * <= 200){ paint.setshader(gradient); }else{ paint.setshader(null); } //canvas.cliprect(clip, region.op.replace); canvas.drawpath(path, paint); canvas.restore(); //restoring "checkpoint" //stop(); } } @override public void setalpha(@intrange(from = 0, = 255) int i) { } @override public void setcolorfilter(@nullable colorfilter colorfilter) { } @override public int getopacity() { return pixelformat.opaque; } }
as can seen 2 images, unable intervals correctly rectangle or line.
and, gradient not show, in spite of using sweepgradient.
how should go achieving desired effect?
thanks.
Comments
Post a Comment