Animated swap position of two buttons

倖福魔咒の 提交于 2019-11-29 04:47:48

You wont get the getRight value in onCreate because the UI hasn't been drawn on the screen so none of the UI elements are measure yet. Try this

ViewTreeObserver vto = btn1.getViewTreeObserver();  
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
    @Override  
    public void onGlobalLayout() {  
        btn1.getViewTreeObserver().removeGlobalOnLayoutListener(this);  
        int left = btn1.getLeft(); 
    }  
});

Put the code in the onCreate method

Android's TranslateAnimation doesn't reposition your components, it just animates the transition you want to do - after that, it's up to you to change the layout params of your component (take a look at this post).

P.S. You can use directly the translate animation, without using a whole new animation set. Another good thing will be to create the animations when your layout is inflated and to override onSizeChanged(int, int, int, int) to re-create them using the new dimensions.

What's the layout you are using?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!