Thursday 15 August 2013

Android Progressbar (custom view) value animation not working - Object Animator in View after invalidate -



Android Progressbar (custom view) value animation not working - Object Animator in View after invalidate -

i need animate progress bar value changed while valueproperty gets changed, custom view given below,

public class progressbar extends view { public progressbar(context context) { this(context, null); } public progressbar(context context, attributeset attrs) { super(context, attrs); } objectanimator animator; double value = 50; public double getvalue() { homecoming value; } public void settargetvalue(double targetvalue) { animator = objectanimator.offloat(this, "value", (float) this.value,(float) targetvalue); animator.setduration(1500); animator.start(); this.value = targetvalue; } public void setvalue(double tempvalue) { settargetvalue(tempvalue); this.invalidate(); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); paint paint = new paint(); paint.setstrokewidth(3); rectf borderrectf = new rectf(50,100,400,200); rectf backgroundrectf = new rectf(53,103,397,197); rectf filledrectf = new rectf(53,103,53,197); paint.setcolor(color.ltgray); canvas.drawrect(borderrectf, paint); paint.setstrokewidth(0); paint.setcolor(color.white); canvas.drawrect(backgroundrectf, paint); paint.setcolor(color.blue); filledrectf = getfilledrect(); canvas.drawrect(filledrectf, paint); } private rectf getfilledrect(){ float filledvalue = (float)(53+(3.44 * this.value)); filledvalue = math.min(filledvalue,397); homecoming new rectf(53,103,filledvalue,197); } }

but animation not working, miss something, or can differently?

you need 2 functions instead of one. 1 function should called new target value, , other function should used implement each step along way. utilize objectanimator in first one, , phone call sec function many times each incremental step. this:

public void setprogress(float progress) { animator = objectanimator.offloat(this, "value", this.value, progress); animator.setduration(1500); animator.start(); } public void setvalue(float value) { this.value = value; invalidate(); } private rectf getfilledrect() { float filledvalue = 53f + (3.44f * this.value); filledvalue = math.min(filledvalue, 397f); homecoming new rectf(53f, 103f, filledvalue, 197f); }

a few notes:

you don't need phone call "settarget(this)" because set target, in first parameter offloat. you might want delay setting field "value" until animation complete. can utilize animationlistener this, overriding onanimationend. is, field set new target value before ui represents this. the signature of "setvalue" must shown, because how objectanimator.offloat works: looks setter of named property takes float , returns void.

edit

ah, see, you're making own progress bar. in case, right phone call invalidate, since overriding ondraw. i've modified reply above, changing "settargetvalue" "setprogress". function should called outside class -- whoever knows progress is. not want setprogress phone call setvalue, or vice versa.

new notes:

you should utilize float values everywhere , not double, since used in rectf anyway. if add together "f" after number, java interprets float , not double, , don't have casting in equations. since overriding ondraw, intermediate setvalue function needs set field "value", , invalidate progress bar.

android android-animation android-custom-view objectanimator

No comments:

Post a Comment