Android: How to get a View position before and after an layout update?
I'm trying to create a animation to link two position of a View. The position of the View is defined by the overridden method onLayout. I'm getting the position before and after the position update with the getLocationOnScreen method.
Here is my code simplified:
int[] startPosition = new int[2];
view.getLocationOnScreen(startPosition);
// do some layout update that changed the view position on screen
requestLayout();
invalidate();
int[] endPosition = new int[2];
view.getLocationOnScreen(endPosition);
Animation anim = new TranslateAnimation(startPosition[0], endPosition[0], startPosition[1], endPosition[1]);
anim.setDuration(100);
view.startAnimation(anim);
The problem is that the onLayout method isn't called after the layout update and the call to requestLayout... I found that calling requestLayout will only schedule a layout pass of the view tree. So my question is: how to make this layout calculation synchronously ? Or wait for its end ? I could use postDelayed or add a event system to the onLayout method, but I'd like to test cleaner methods first... I could also call the onLayout method myself, but I don't know exactly how to cal开发者_运维问答culate its arguments.
Any idea is welcomed !
精彩评论