I need to animate progress bar value changed while value
property gets changed, my custom view is 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() {
return 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);
return new RectF(53,103,filledValue,197);
}
}
but animation is not working, am i miss something, or can i do it differently?
You need two functions instead of this one. One function should be called with the new target for your value, and the other function should be used to implement each step along the way. Use ObjectAnimator in the first one, and it will call your second function many times for each incremental step. Like 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);
return new RectF(53f, 103f, filledValue, 197f);
}
A few notes:
EDIT
Ah, I see, you're making your own progress bar. In that case, you were right to call invalidate, since you are overriding onDraw. I've modified my answer above, changing "setTargetValue" to "setProgress". This is a function that should only be called from OUTSIDE this class -- whoever knows what the progress is. You do NOT want setProgress to call setValue, or vice versa.
New notes: