getLocationOnScreen将()中的onCreate()刚刚返回0getLocationOnScreen、onCreate

2023-09-05 03:22:05 作者:考不上高中我就去抗日

在一个按钮点击响应的ImageView的(或其他视图)调用getLocationOnScreen()给出正确的值。

Calling getLocationOnScreen() on an ImageView (or other View) in a response to a button click gives correct values.

但调用getLocationOnScreen()的活动:在OnCreate()的时候,我只得到[0,0]。我该如何解决这个问题?

But when calling getLocationOnScreen() from Activity::onCreate(), I only get [0, 0]. How can I fix this?

时的onCreate()太早了所有的计算工作要做?有没有以后更好的地方勾开始动画?

Is onCreate() too early for all calculations to be done? Is there a later better place to hook to start animations?

推荐答案

还为时过早检查 getLocationOnScreen将()的onCreate()。更好的地方是在视图级(如果您使用的是自定义视图),是 onLayout()。只有在这里,视图的大小和位置进行了计算。如果,你是不是创建自定义视图,您可在活动水平 onWindowFocusChanged得到它()(具有焦点的活动)。

It's too early to check for getLocationOnScreen() in onCreate(). The better place is at view level (if you are using custom views), is at onLayout(). Only here, the view's size and position are calculated. In case, you aren't creating custom views, you could obtain it in activity level at onWindowFocusChanged() (with the activity having the focus).