1

i'm new on this forum but i've read it for a long time.

I'm building an android application, a card game, and i'm trying to create the animations to make it playable. My question is this: Is it possible to build a TranslateAnimation from the code, using the position of the button i want to animate, to the position of another existent button or View?

I've tried things like .getLocationInWindow() but the values are not the ones i'm looking for.

Thanks in advance for every response.

sangra4l
  • 73
  • 4
  • Have you looked into [Drag & Drop](http://developer.android.com/guide/topics/ui/drag-drop.html)? – Emmanuel Jan 01 '15 at 17:38
  • are you saying to try using getX() and getY() in the drag & drop documentation?? – sangra4l Jan 01 '15 at 17:45
  • 1
    Sorry I misread your question. – Emmanuel Jan 01 '15 at 17:48
  • ochei, thanks anyway – sangra4l Jan 01 '15 at 17:48
  • You can get the `Views` on the screen and animate the ones that are of type "Card". You get their position and animate to each player (some other point (x0,y0), (x1,y1), etc). – Emmanuel Jan 01 '15 at 17:54
  • Ochei, but my problem is getting that position... I'm trying to make so that when a card is played, the next card that has to be dealt would start its animation from the deck (which is a Button) to the card position in the player hand (which is another Button). – sangra4l Jan 01 '15 at 18:04
  • So, you go through the layout hierarchy and you look for the `Button`s that are the "player's hands" and you get the coordinates of such `Button`'s. You may want to `extends` `Button` so you can easily identify `Button`'s that represent players. – Emmanuel Jan 01 '15 at 18:22

1 Answers1

2

You need to remember few things. If you want to use animating of the translation you need to provide the distance difference along both axis (x and y). So your code could look more less like this:

View viewToBeMoved = findViewById(R.id.view_to_be_moved);
View destinationView = findViewById(R.id.destination_view);
int xDiff = destinationView.getLeft() - viewToBeMoved.getLeft();
int yDiff = destinationView.getTop() - viewToBeMoved.getTop();
viewToBeMoved.animate().translationXBy(xDiff).translationYBy(yDiff);

Also you need to remember that this code will work ONLY when both viewToBeMoved and destinationView have the same parent (so getTop() and getLeft() methods return proper values).

Edit:

For views that dont belong to the same parent you can try something like this:

View viewToBeMoved = findViewById(R.id.view_to_be_moved);
int[] viewToBeMovedPos = new int[2];
viewToBeMoved.getLocationOnScreen(viewToBeMovedPos);

View destinationView = findViewById(R.id.destination_view);
int[] destinationViewPos = new int[2];
destinationView.getLocationOnScreen(destinationViewPos);

int xDiff = destinationViewPos[0] - viewToBeMovedPos[0];
int yDiff = destinationViewPos[1] - viewToBeMovedPos[1];
viewToBeMoved.animate().translationXBy(xDiff).translationYBy(yDiff);

instead of getLocationOnScreen you can use getLocationInWindow but in both cases be sure that you "invoke it AFTER layout has happened"

Community
  • 1
  • 1
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • thanks for the answer... So it isn't possible to make the animation work between different Views?? Sadly i've put the "deck button" in a "deck relative layout" and the "player's cards" in a "player's hand realtive layout". Isn't it possible to refer to their common ancestor or work with Absolute positioning (i've tried to do it myself but sadly without success) – sangra4l Jan 02 '15 at 09:58
  • it worked... i think i love you now, it's a shame we couldn't spend the holidays together... Jokes aside thank you man. – sangra4l Jan 02 '15 at 16:15