Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ObjectAnimator vs ViewPropertyAnimator

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.

like image 480
Hwang Avatar asked Jul 05 '16 13:07

Hwang


People also ask

What are the two different types of view animations?

There are two types of animations that you can do with the view animation framework: Tween animation: Creates an animation by performing a series of transformations on a single image with an Animation. Frame animation: or creates an animation by showing a sequence of images in order with an AnimationDrawable .

What is Android ValueAnimator?

ValueAnimator provides a timing engine for running animation which calculates the animated values and set them on the target objects. By ValueAnimator you can animate a view width, height, update its x and y coordinates or even can change its background.

What is Property animation in Android?

A property animation changes a property's (a field in an object) value over a specified length of time. To animate something, you specify the object property that you want to animate, such as an object's position on the screen, how long you want to animate it for, and what values you want to animate between.


1 Answers

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.

like image 156
Gil Moshayof Avatar answered Sep 22 '22 05:09

Gil Moshayof