Determining translationX/Y values for ObjectAnimator; how to move a view to an exact screen position?

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:

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…

Constructing the 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.

Clearly, I’m missing very fundamental knowledge, just trying to figure out what exactly that is. Any guidance much appreciated. Thanks.

Leave a Comment