Rishabh Jain
Rishabh Jain

Reputation: 536

Difference between setTranslationY() and translationYBy()?

I am trying to achieve the following functionality :- When Someone taps on my image view it should show the image appearing with an animation i.e. entering from screen top. To get this I am following the following approach

I have two set of codes where animating the view out of screen does not work but just calling setTranslationY() on view works Refer to code segment attached

imageView.setTranslationY(-2000);
imageView.setImageResource(R.drawable.red);
imageView.animate().translationYBy(2000).setDuration(300);

This works but following doesn't.

imageView.animate().translationYBy(-2000);
imageView.setImageResource(R.drawable.red);
imageView.animate().translationYBy(2000).setDuration(300);

Can you please explain about the behaviour and maybe difference between setTranslateY() vs translationYby().

Upvotes: 1

Views: 3057

Answers (3)

yoAlex5
yoAlex5

Reputation: 34341

Android setTranslationY vs translationYBy

  • setTranslationY moved on point one single time
  • translationYBy moved on point every time

[Take a look at example]

Upvotes: 0

AndrewBloom
AndrewBloom

Reputation: 2438

From docs: https://developer.android.com/reference/android/view/ViewPropertyAnimator

translationY

Added in API level 12 public ViewPropertyAnimator translationY (float value) This method will cause the View's translationY property to be animated to the specified value. Animations already running on the property will be canceled.

Parameters value float: The value to be animated to.


translationYBy

Added in API level 12 public ViewPropertyAnimator translationYBy (float value) This method will cause the View's translationY property to be animated by the specified value. Animations already running on the property will be canceled.

Parameters value float: The amount to be animated by, as an offset from the current value.

Clearly the difference is on the meaning of the parameter: for translationY it represents an absolute position, for translationYBy it represents on offset from the current value.

Upvotes: 2

saad shahzad
saad shahzad

Reputation: 1

The difference between translationYBy and setTranslationY is that the first one will affect the animation of the image when you click on it while the the second one will affect the image as soon as the program has start running, meaning if you position the image lets say 360 by using setTranslationY, it will be at 360 in the beginning of the app

Upvotes: 0

Related Questions