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.

Hwang
  • 206
  • 3
  • 9

1 Answers1

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.

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
  • What about ViewPropertyAnimator.translationX()? Does that mean ViewPropertyAnimator.x() and translationX() are actually the same? – Hwang Jul 05 '16 at 14:56
  • 1
    translationX also affects the translationX, just without compensating with the view's mLeft value. – Gil Moshayof Jul 05 '16 at 15:15
  • 1
    I don't think this answer is correct. Do you have any reference? – black Sep 27 '16 at 12:36
  • Yes, the android source code (see method setValue(int, float)): https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/ViewPropertyAnimator.java – Gil Moshayof Sep 27 '16 at 12:42
  • How about using ViewPropertyAnimator.x() ? This will play with the actual position of the view. – CopsOnRoad Oct 04 '17 at 16:31
  • Please check this issue https://stackoverflow.com/questions/49044088/android-object-animator-frame-issue @GilMoshayof – Mani murugan Mar 01 '18 at 06:59
  • i don't think this is true anymore (Android 28 code does not do this) – hmac Jul 11 '19 at 14:16