0

Je crée une vue personnalisée qui est essentiellement un rectangle (objet RectF). Je voudrais que, selon une valeur spécifiée par l'utilisateur, changer la taille du rectangle en conséquence avec l'animation. J'ai donc essayé:Durée de l'animation ne fonctionne pas

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) { 
    super(context); 
} 

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

public void init() 
{ 

} 

@Override 
protected void onDraw(Canvas canvas) 
{ 
    super.onDraw(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)); 
    canvas.drawRect(mObjectHead,objectHeadPaint); 

    mObjectBody = new RectF(0,(int)objetBodyDistanceFromTop,mCanvasWidth,mCanvasHeight); 
    Paint objectBodyPaint = new Paint(); 
    objectBodyPaint.setStyle(Paint.Style.STROKE); 
    objectBodyPaint.setColor(ContextCompat.getColor(getContext(), R.color.batifyColor)); 
    objectBodyPaint.setStrokeWidth(10); 
    canvas.drawRect(mObjectBody,objectBodyPaint); 

    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() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator valueAnimator) { 
      postInvalidate(); 
     } 
    }); 
    AnimatorSet rectAnimation = new AnimatorSet(); 
    rectAnimation.play(animateBottom); 
    rectAnimation.setDuration(10000).start(); 
} 

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

Le problème est que la taille ne soit pas mise à jour progressivement comme prévu, mais directement à la valeur finale quel que soit le temps de la durée de l'animation. Une idée de ce que je fais mal?

Merci d'avance!

+1

J'ai créé un échantillon et essayé le même et il fonctionne comme prévu. – Raghunandan

+0

Mise à jour progressive? –

+1

oui. Je peux voir la mise à jour de l'animation lentement. – Raghunandan

Répondre

1

Éviter l'allocation et les calculs dans onDraw pour des raisons de performances

Le fond de la propriété a besoin d'un poseur. RectF n'en a pas. Donc, cette

public class AnimatableRectF extends RectF{ 


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

    public AnimatableRectF(RectF r) { 
     super(r); 
    } 

    public AnimatableRectF(Rect r) { 
     super(r); 
    } 

    public void setTop(float top){ 
     this.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; 
    } 

} 

Puis

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

La partie animation reste le même