开发者

Couldn't understand when to use getLeft, setLeft etc. in Android?

The problem is that, I a开发者_运维百科m animating some views in my layout then trying to get the left and right variables of that views after the animation but the returning value is always the initial value when they were not animated.

I am using property animations like this:

For example this is moving the middle frame by 4/3 of its width and making it appear again on the screen by playing with its alpha...

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

However after the animation I should be able to get the changed left and right value of the middleFrame but when I try to do that as i said before it gives me the previous values..

I have checked this question and Romain Guy's answer How to get the absolute coordinates of a view

However, I couldn't quite understand when can I use the getLeft/Right or getLocationOnScreen() etc methods.

I have already tried getLocationOnScreen or getLocationInWindow but they return the same results.

Thanks in advance.


I also never understood why getLocationOnScreen and getLocationInWindow returns the same values...

getLeft/Right/Top/Bottom returns the offset to view parent in pixel. You can't use that for animations because this information is static and only provides information about the layout.

Simple example:

<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>

If you use this layout and animate the first text view with a translation to the right side, it will overflow the second text view instead pushing it to the right side. So basically the first text view is still at its old position (getLeft/Right/Top/Bottom same value) but was internally moved to another position.

To come straight to the point: Animation is just a visual effect and has nothing to do with layout positioning.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜