Defining X / Y translation values ​​for ObjectAnimator; How to move the view to the exact position of the screen?

I am trying to move the view to the upper right corner of the screen using ObjectAnimator.ofFloat (...) But I am not getting the expected results. I get the coordinates of the view in advance using ViewTreeListener etc., and I already know the x value that I need to compensate from the end of the total width. I can not get a measurement to go where I want. Relevant Code:

Getting the starting coordinate; where currently displayed:

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 the coordinates of the screen, and not relative to the parent), when I call userControlLayout.getLeft(), I expect that they will differ from each other in my understanding of the documents. Anyway...

Building 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 one arg call is equivalent to specifying the final coordinate that you want to translate / move, and the two arg versions start with ... end with or from ... they messed up with both of me and can't get there.

It is clear that I am lacking very fundamental knowledge, just trying to understand what it is. Any guidance is much appreciated. Thank you

+4
source share
1 answer

-, userControlLayout.getLeft() . , . getTop() , getLocationInWindow() , , y = 0 , .

, ( , ). , (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);

ObjectAnimator, . userUpTop, -userUpTop , , . , ( ) .

+14

Source: https://habr.com/ru/post/1544561/


All Articles