无法理解何时在Android中使用getLeft,setLeft等?

时间:2011-09-20 11:12:15

标签: android android-layout coordinates css-position

问题在于,我在布局中设置了一些视图,然后尝试在动画后获取该视图的左右变量,但返回值始终是未设置动画时的初始值。

我正在使用这样的属性动画:

例如,这是将中间框架移动宽度的4/3,并通过播放其alpha来再次显示在屏幕上...

middleFrame.animate().translationXBy(4*(middleFrame.getWidth())/3).alpha(1.0f);

然而,在动画之后,我应该能够获得更改的中间帧的左右值,但是当我尝试按照之前说的那样,它会给我以前的值..

我已经检查了这个问题和Romain Guy的回答How to get the absolute coordinates of a view

然而,我无法理解何时可以使用getLeft / Right或getLocationOnScreen()等方法。

我已经尝试过getLocationOnScreen或getLocationInWindow,但它们会返回相同的结果。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我也从未理解为什么getLocationOnScreen和getLocationInWindow返回相同的值......

getLeft / Right / Top / Bottom返回偏移量,以像素为单位查看父级。 您无法将其用于动画,因为此信息是静态的,仅提供有关布局的信息。

简单示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent" 
            android:text="@string/hello" />
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent" 
            android:text="@string/hello" />
</LinearLayout>

如果您使用此布局并为第一个文本视图设置动画并向右侧进行转换,则会溢出第二个文本视图而不是将其推向右侧。所以基本上第一个文本视图仍处于旧位置(getLeft / Right / Top / Bottom相同值),但内部移动到另一个位置。

直截了当:动画只是一种视觉效果,与布局定位无关。