Android - offsetTopAndBottom,setTranslateY之间的区别

And*_*dio 15 android android-view

Android View似乎有两种方法似乎可以做一些非常相似的功能.

1] setTranslationX&setTranslationY
2] offsetLeftAndRight&offsetTopAndBottom

谁能告诉我这些有何不同?什么是可以使用[1]而不是[2]的情节?反之亦然?

bwt*_*bwt 5

offsetLeftAndRight()offsetTopAndBottom()改变的布局传递的结果,而setTranslationX()setTranslationY添加其它附加上的布局结果的顶部偏移,默认值是0.

一个结果是leftAndRight/TopAndBottom偏移量在每次布局传递时被重置(例如由触发requestLayout()),而翻译则没有.

我的理解是,offsetLeftAndRight()offsetTopAndBottom()如果你写你自己的布局,而大多是有用的setTranslationX()setTranslationY是通用的方法来调整布局的结果.


Kri*_*nam -2

一般来说,这两个函数都是不同 api 级别的产物,现在在 settranslation 中,api 应该至少为 16,但是对于偏移量,您可以从安静的早期 api 中看到它们,两者之间的区别是 settranslationx 和 y ,给我们一个效果移动屏幕,不需要 aplha,但对于偏移来说,它是一个模糊函数,其中 alpha 是关键点,settranslate 示例

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="100%p" android:toXDelta="0%p"
    android:duration="@android:integer/config_longAnimTime"/>
Run Code Online (Sandbox Code Playgroud)

,会产生将屏幕从左向右移动偏移的错觉

<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"/>
Run Code Online (Sandbox Code Playgroud)

将创建一个模糊的 UI