Androbean Studio
Androbean Studio

Reputation: 382

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?

Upvotes: 16

Views: 3743

Answers (2)

Philippe Banwarth
Philippe Banwarth

Reputation: 17755

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.

Upvotes: 9

Krishna Avadhanam
Krishna Avadhanam

Reputation: 98

in normal terms both functions are a product of different api levels , now in settranslation the api should be atlest 16, but for offset u can see them from quiet earlier api, and the difference between both is , settranslationx and y , gives us an effect of moving the screen ,does not require aplha in it, but for offset its a blurring function where the alpha is the key point, the settranslate example

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="100%p" android:toXDelta="0%p"
    android:duration="@android:integer/config_longAnimTime"/>

, will create an illution of moving the screen left to right where offset

<offset AlphaAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromalpha="0.0f"  android:toalpha=1.0f"
    android:duration="@android:integer/config_longAnimTime"/>

will create a blurring UI

Upvotes: -2

Related Questions