Android ObjectAnimator vs ViewPropertyAnimator
Asked Answered
S

1

6

What's the difference between ObjectAnimator and ViewPropertyAnimator changing property value?

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(myObject, "X", 0.0f, 100.0f);

I tried myObject.getX() while above objectAnimator is ongoing, and I got a on-the-way value between 0.0f to 100.0.

myObject.setX(0.0f);
myObject.animate().x(100.0f);

However, I got precise 100.0 when I myObject.getX()'d while above ViewPropertyAnimator is ongoing.

I can't figure out what makes this difference.

Thanks in advance.

Showcase answered 5/7, 2016 at 13:55 Comment(0)
L
5

When you request to animate the x field using a ViewPropertyAnimator, it doesn't actually animate the x field - it animates the translateX field. This is why you can't see the x field change.

From the Android source code in ViewPropertyAnimator.java:

case X:
    renderNode.setTranslationX(value - mView.mLeft);
    break;

ObjectAnimator on the other hand uses reflection to animate properties - rather than a preset list of supported actions. And so when you tell it to animate the "X" field, it calls "setX" directly.

Leonard answered 5/7, 2016 at 14:14 Comment(7)
What about ViewPropertyAnimator.translationX()? Does that mean ViewPropertyAnimator.x() and translationX() are actually the same?Showcase
translationX also affects the translationX, just without compensating with the view's mLeft value.Leonard
I don't think this answer is correct. Do you have any reference?Kemper
Yes, the android source code (see method setValue(int, float)): github.com/android/platform_frameworks_base/blob/master/core/…Leonard
How about using ViewPropertyAnimator.x() ? This will play with the actual position of the view.Exordium
Please check this issue #49044588 @GilMoshayofSatchel
i don't think this is true anymore (Android 28 code does not do this)Ingraham

© 2022 - 2024 — McMap. All rights reserved.