What is the difference between ViewPropertyAnimator's translateYBy() vs yBy() methods?
Solution 1
The documentation states that X
and Y
properties are absolute positions, while translationX
and translationY
are referred to the LEFT and TOP properties respectively.
So, the difference between animate().x()
and animate().translationX()
is that one animates to an absolute value, while the other animates to a still absolute value but determined by the view LEFT
and TOP
values.
By contrast, animate().xBy()
and animate().translationXBy()
increase (algebraically) the property value by the specified amount.
That is the origin of the "some mathematics" cited above.
Solution 2
A visualization of translationY(), y(), yBy(), translationYBy() methods are bellow
Solution 3
x
refers to the current visual position of a view in the x-axis. So, for example, when you animate x
by calling view.animate().x(10)
, the view will be animated such that it moves to x=10
. Let's just assume that view was in a position of (100, 150) when you started the animation. By the end of the animation, the view will be in (10, 150).
Now, contrast this with translationX
. If you animate this property by calling view.animate().translationX(10)
, you are moving a view by that many pixels in the x-axis. Let's assume the same example, where the view was in a position of (100, 150) when you started the animation. By the end of the animation, the view will be in (110, 150).
Hope that clarifies the difference between x()
and translationX()
. The difference is the same for y()
and translationY()
, but in the y-axis.
In my view, xBy()
achieves the same effect as translationX()
but by using the x
property itself plus some mathematics. yBy()
and translationY()
are the equivalents in the y-axis.
Hope that clarifies...
Some Noob Student
Just Some Noob Student. ( ̄(工) ̄) I ask questions that are dumb and can't distinguish a good answer. ╮(╯▽╰)╭ So please forgive me if I didn't choose your answer as the best answer. o(〒﹏〒)o But I'll make it up to you with upvotes. ‧★,:*:‧\ ( ̄▽ ̄) /‧:‧°★
Updated on June 03, 2022Comments
-
Some Noob Student almost 2 years
ViewPropertyAnimators (http://developer.android.com/reference/android/view/ViewPropertyAnimator.html) have two types of methods to move Views around:
translateX() vs x(), translateY() vs y(), translateXBy() vs xBy() and finally translateYBy() vs yBy().
Can someone tell me the difference between these methods?
-
Some Noob Student almost 10 yearsThanks, I always wondered why they needed a translateXBy method. It feels exactly the same as XBy.
-
tgkprog about 8 yearsThis is the wrong answer. See Nicolò Parolini's answer
-
Irony Stack over 6 yearsI did not get. can you please give more info. or link to some resources. so that i can understand the concept
-
DeltaCap019 about 5 yearsIt took some time to focus on these gifs "especially which button is being clicked" on which gif, but now I won't get confused in these ever. Thanks
-
yoAlex5 about 5 yearsThanks for pointing me out. I added highlighter and a click counter
-
YellowJ almost 5 yearsWhat's the difference between YBY and TRANSLATIONYBY?
-
yoAlex5 almost 5 years
ViewPropertyAnimator.getValue
forTRANSLATIONYBY
returnsnode.getTranslationY()
,ViewPropertyAnimator.getValue
forYBY
returnsmView.mTop + node.getTranslationY()
as a resultViewPropertyAnimator.setValue()
pass the same value