Android 确定 ObjectAnimator 的平移 X/Y 值;如何将视图移动到确切的屏幕位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24217357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determining translationX/Y values for ObjectAnimator; how to move a view to an exact screen position?
提问by wkhatch
I'm trying to move a view to the upper right corner of the screen, using ObjectAnimator.ofFloat(...) But, I'm not getting the results I expect. I'm getting the coordinates of the view beforehand, using ViewTreeListener, etc, and I already know the x value I need to offset from the end of the overall width. I can't get either dimension to move to where I want it. Relevant code:
我正在尝试使用 ObjectAnimator.ofFloat(...) 将视图移动到屏幕的右上角,但是,我没有得到我期望的结果。我预先获取视图的坐标,使用 ViewTreeListener 等,并且我已经知道我需要从总宽度的末尾偏移的 x 值。我无法让任一维度移动到我想要的位置。相关代码:
Getting the starting coordinate; where the view currently is:
获取起始坐标;当前视图在哪里:
int[] userCoords = new int[]{0,0};
userControlLayout.getLocationInWindow(userCoords);
//also tried getLocationInScreen(userCoords); same result
userUpLeft = userCoords[0];
userUpTop = userCoords[1];
Surprisingly, I get the same value as userUpLeft (which is in screen coordinates, and not relative to parent) when I call userControlLayout.getLeft()
I'd expect them to be different per my understanding of the docs. Anyway...
令人惊讶的是,当我打电话时,我得到与 userUpLeft 相同的值(在屏幕坐标中,而不是相对于父级)userControlLayout.getLeft()
我希望它们根据我对文档的理解而有所不同。反正...
Constructing the ObjectAnimators:
构造 ObjectAnimators:
//testXTranslate is a magic number of 390 that works; arrived at by trial. no idea why that
// value puts the view where I want it; can't find any correlation between the dimension
// and measurements I've got
ObjectAnimator translateX = ObjectAnimator.ofFloat(userControlLayout, "translationX",
testXTranslate);
//again, using another magic number of -410f puts me at the Y I want, but again, no idea //why; currently trying the two argument call, which I understand is from...to
//if userUpTop was derived using screen coordinates, then isn't it logical to assume that -//userUpTop would result in moving to Y 0, which is what I want? Doesn't happen
ObjectAnimator translateY = ObjectAnimator.ofFloat(userControlLayout, "translationY",
userUpTop, -(userUpTop));
My understanding is that the one arg call is equivalent to specifying the end coordinate you want to translate/move to, and the two arg version is starting at...ending at, or, from...to I've messed around with both and can't get there.
我的理解是,一个 arg 调用相当于指定要平移/移动到的结束坐标,而两个 arg 版本开始于...结束于,或者,从...到我搞砸了两者都无法到达那里。
Clearly, I'm missing very fundamental knowledge, just trying to figure out what exactly that is. Any guidance much appreciated. Thanks.
显然,我缺少非常基本的知识,只是想弄清楚那到底是什么。非常感谢任何指导。谢谢。
回答by matiash
First, userControlLayout.getLeft()
is relative to the parent view. If this parent is aligned to the left edge of the screen, those values will match. For getTop()
it's generally different simply because getLocationInWindow()
returns absolutecoordinates, which means that y = 0 is the very top left of the window -- i.e. behind the action bar.
首先,userControlLayout.getLeft()
是相对于父视图。如果此父级与屏幕的左边缘对齐,则这些值将匹配。因为getTop()
它通常不同,因为getLocationInWindow()
返回绝对坐标,这意味着 y = 0 是窗口的左上角——即在操作栏后面。
Generally you want to translate the control relative to its parent (since it won't even be drawn if it moves outside those bounds). So supposing you want to position the control at (targetX, targetY)
, you should use:
通常,您希望相对于其父级平移控件(因为如果它移动到这些边界之外,它甚至不会被绘制)。因此,假设您想将控件定位在(targetX, targetY)
,您应该使用:
int deltaX = targetX - button.getLeft();
int deltaY = targetY - button.getTop();
ObjectAnimator translateX = ObjectAnimator.ofFloat(button, "translationX", deltaX);
ObjectAnimator translateY = ObjectAnimator.ofFloat(button, "translationY", deltaY);
When you supply multiple values to an ObjectAnimator
, you're indicating intermediate values in the animation. So in your case userUpTop, -userUpTop
would cause the translation to go down first and then up. Remember that translation (as well as rotation and all the other transformations) is always relative to the original position.
当您向 提供多个值时ObjectAnimator
,您是在动画中指示中间值。所以在你的情况下userUpTop, -userUpTop
会导致翻译先下降然后上升。请记住,平移(以及旋转和所有其他变换)始终相对于原始位置。