Search code examples
androidobjectanimator

ValueAnimator memory issue


I'm using ValueAnimator of float values.

ValueAnimator anim = ValueAnimator.ofFloat(-1, 1);
anim.addUpdateListener(new AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float f = (float) animation.getAnimatedValue();
        }
    });

Here is the issue - memory allocation tracker detected that on each call of getAnimatedValue() a new Float object is created. I'm using ValueAnimator in INFINITE repeat mode and creating new objects constantly is kind of a problem. Is there any way to prevent creating new object all the time?

P.S.: I know that such kind of memory leak is not critical at all, just interested in optimization.


Solution

  • There is a way to prevent creating new Float objects on each time onAnimationUpdate() method is invoked. In my case it looks like:

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float t = 2f * animation.getAnimatedFraction() - 1f;
    }