Hubert Solecki Hubert Solecki - 1 year ago 76
Android Question

Animation duration does not work

I'm creating a custom view which is basically a Rectangle (RectF object). I would like that according to a specified value by the user, to change the rectangle size accordingly with animation. So I've tried that:

public class ObjectView extends View {

public int mCanvasWidth;
public int mCanvasHeight;

public int mVolumeHeight = 0;

public RectF mObjectHead;
public RectF mObjectBody;
public RectF mObjectBodyVolume;
public Canvas mCanvas;

private boolean mInitDone = false;

public ObjectView(Context context) {

public ObjectView(Context context, AttributeSet attrs) {
super(context, attrs);

public void init()


protected void onDraw(Canvas canvas)
this.mCanvas = canvas;
float objectHeadDistanceFromLeft = mCanvasWidth / 3;
float objectHeadWidth = mCanvasWidth / 3;

float objectBodyDistanceFromTop = mCanvasHeight / 5;
float objectHeadHeight = mCanvasHeight / 5;
mBatteryHead = new RectF(batteryHeadDistanceFromLeft,0,2*objectHeadWidth,objectHeadHeight+5);
Paint objectHeadPaint = new Paint();
objectHeadPaint.setColor(ContextCompat.getColor(getContext(), R.color.batifyColor));

mObjectBody = new RectF(0,(int)objetBodyDistanceFromTop,mCanvasWidth,mCanvasHeight);
Paint objectBodyPaint = new Paint();
objectBodyPaint.setColor(ContextCompat.getColor(getContext(), R.color.batifyColor));

mObjectBodyVolume = new RectRectF(12, (int) objectBodyDistanceFromTop + 10, mCanvasWidth - 12, mVolumeHeight);
Paint volumeBodyPaint = new Paint();
volumeBodyPaint.setColor(ContextCompat.getColor(getContext(), R.color.batifyColor));
canvas.drawRect(mObjectBodyVolume, volumeBodyPaint);


public void setStateOnObject(){
this.mVolumeHeight = mCanvasHeight;
ObjectAnimator animateBottom = ObjectAnimator.ofFloat(mObjectBodyVolume, "bottom", mObjectBodyVolume.bottom, this.mVolumeHeight);
animateBottom.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator valueAnimator) {
AnimatorSet rectAnimation = new AnimatorSet();;

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mCanvasWidth = MeasureSpec.getSize(widthMeasureSpec);
mCanvasHeight = MeasureSpec.getSize(heightMeasureSpec);

The problem is that the size is not updating progressively as expected but directly to the final value whatever the duration time of the animation.
Any idea what I'm doing wrong ?

Thanks in advance !

Answer Source

Avoid allocation and calculations in onDraw for the sake of performance

The property bottom requires a setter. RectF does not have one. So have this

public class AnimatableRectF extends RectF{

    public AnimatableRectF(float left, float top, float right, float bottom) {
        super(left, top, right, bottom);

    public AnimatableRectF(RectF r) {

    public AnimatableRectF(Rect r) {

    public void setTop(float top){ = top;
    public void setBottom(float bottom){

        this.bottom = bottom;
    public void setRight(float right){
        this.right = right;
    public void setLeft(float left){
        this.left = left;



 mObjectBodyVolume = new AnimatableRectF(12, (int) objectBodyDistanceFromTop + 10, mCanvasWidth - 12, mVolumeHeight);

The animation part remains the same

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download