android - Xamarin - Radial Progress Component Issue -
i 've been trying implement radialprogress component (https://components.xamarin.com/view/radialprogress) on app. managed on screen, , change progress colour can't find way change inner colour of circle.
the radialprogressview object has backgroundtintmode field takes duffporter.mode whenever try set background tint mode app breaks message (message = "no method name='setbackgroundtintmode' signature='(landroid/graphics/porterduff$mode;))
is there way want?
thanks!
yes, can done. although not in straight-forward way or maintainable way.
firstly, let's dig little radialprogressview
's drawing code (as exposed xamarin studio assembly browser):
protected override void ondraw(canvas canvas) { // ... there's more stuff here, idea canvas.drawcircle(this.bgcx, this.bgcy, this.radius, this.bgcirclepaint); canvas.drawcircle(this.bgcx, this.bgcy, this.innerlineradius, this.bgborderpaint); canvas.drawtext(this.valuetext, this.textx, this.texty, this.textpaint); }
we notice colors here, bgcirclepaint
and bgborderpaint
. if able change value of these variables, able change color progressview painted.
the problem radialprogressview
not expose fields – private, inheriting radialprogressview
not allow set them new value.
however, can make use of reflection change these private fields, so:
var textpaintmember = typeof(radialprogressview).getfield("textpaint", bindingflags.instance | bindingflags.nonpublic); textpaintmember.setvalue(instance, mynewsupercoolcolorpaint);
by combining two, can come new, customizable class this:
public class customizableradialprogressview : radialprogressview { public customizableradialprogressview(context context) : base(context) { } public void settextcolor(color color) { var paint = new paint(); paint.settypeface(typeface.defaultbold); paint.color = color; paint.antialias = true; var textpaintmember = typeof(radialprogressview).getfield("textpaint", bindingflags.instance | bindingflags.nonpublic); textpaintmember.setvalue(this, paint); } public void setcirclecolor(color color) { var paint = new paint(); paint.setstyle(paint.style.fill); paint.color = color; paint.antialias = true; var circlepaintmember = typeof(radialprogressview).getfield("bgcirclepaint", bindingflags.instance | bindingflags.nonpublic); circlepaintmember.setvalue(this, paint); } public void setbordercolor(color color) { var paint = new paint(); paint.setstyle(paint.style.stroke); paint.color = color; paint.antialias = true; var circlepaintmember = typeof(radialprogressview).getfield("bgborderpaint", bindingflags.instance | bindingflags.nonpublic); circlepaintmember.setvalue(this, paint); } public void setprogresspackgroundcolor(color color) { var paint = new paint(); paint.setstyle(paint.style.stroke); paint.color = color; paint.antialias = true; var circlepaintmember = typeof(radialprogressview).getfield("bgprogresspaint", bindingflags.instance | bindingflags.nonpublic); circlepaintmember.setvalue(this, paint); } }
this result we're after:
note: wise notice making improper use of private fields: we're manipulating them outside of class live in. if xamarin ever decides change way radialprogressview
is implemented, or renames 1 of private variables, our code fail @ runtime. better way approach problem ask xamarin provide getters/setters need. but, hey, it's cooler way ;)
Comments
Post a Comment