Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - difference between offsetTopAndBottom, setTranslateY

Android View seems to have 2 methods which appears to do some very similar functionality.

1] setTranslationX & setTranslationY
2] offsetLeftAndRight & offsetTopAndBottom

Can anybody tell me how these are different? What is the scenarion in which [1] can be used but not [2]? and vise-versa?

like image 930
Androbean Studio Avatar asked Oct 18 '15 09:10

Androbean Studio


1 Answers

offsetLeftAndRight() and offsetTopAndBottom() alter the result of the layout pass, while setTranslationX() and setTranslationY add an additionnal offset on top of the layout result, the default value being 0.

One consequence is that the leftAndRight / TopAndBottom offset is reset with every layout pass (e.g. triggered by requestLayout()) while the translation is not.

My understanding is that offsetLeftAndRight() and offsetTopAndBottom() are mostly useful if you write your own Layout, while setTranslationX() and setTranslationY are generic methods to tweak the result of a layout.

like image 167
bwt Avatar answered Oct 06 '22 01:10

bwt