我正在尝试使用ObjectAnimator.ofFloat(...)将视图移动到屏幕的右上角。但是,我没有得到我期望的结果。我使用ViewTreeListener获取视图的坐标等信息,并且我已经知道需要从整体宽度的末尾偏移的x值。我无法将任何一个维度移动到我想要的位置。相关代码:
获取起始坐标;即视图当前所在位置:
获取起始坐标;即视图当前所在位置:
int[] userCoords = new int[]{0,0};
userControlLayout.getLocationInWindow(userCoords);
//also tried getLocationInScreen(userCoords); same result
userUpLeft = userCoords[0];
userUpTop = userCoords[1];
令人惊讶的是,当我调用userControlLayout.getLeft()
时,我得到了与屏幕坐标相同的值(而不是相对于父级的值)。根据文档的理解,我本应该得到不同的值。无论如何...
构建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));
我的理解是,使用一个参数的调用等同于指定您要平移/移动到的结束坐标,而使用两个参数的版本则是从...到,或者从起始点...到终点。我已经尝试过两种方法,但都不行。
显然,我缺乏非常基本的知识,只是想找出确切的问题所在。任何指导都将不胜感激。谢谢。