Android View position not updating after ObjectAnimator tranlation
android, android-animation, java
Solution
The solution was to use:
ObjectAnimator posterSlider = ObjectAnimator.ofFloat(imageView, "translationX", 0);
...for the return animation. It seems that a view "remembers" its original position after a translation. So, passing 0 as the destination to the ObjectAnimator is the same as saying "send the view back to it's original position".
Problem
I've got 3 equally-spaced ImageViews (A,B and C) laid out horizontally across the screen. When an ImageView is clicked, it slides across the screen to the left (to the position of A) and the other two disappear. If A is the one that's clicked, then the other two just disappear. Once the translation is complete, the translated ImageView is assigned new click behavior. When it's clicked now, it returns to its original position (and the unclicked ImageViews re-appear). I'm using ObjectAnimator to do the translation and I'm using a single pre-computed value in order to determine how far a clicked ImageView should travel. This value is the pixel distance between the left-most edge of each ImageView and it's neighbor. When the first click is detected, the ImageView is animated like this: ``` ObjectAnimator posterSlider = ObjectAnimator.ofFloat( imageView, "translationX", -imageViewIdx * slideDistance); ``` So, in the case of A, the ImageViews index position in the parent will be 0, which when multipled by the slide distance will still be 0, so it never moves. If the index position is 1, the slide distance will be -slideDisance etc. And it works fine. The problem is that when the ImageView is returning to it's original position after being clicked a second time, the translated position is not correct. The translation code is simply the same as above, except instead of `-imageViewIdx * slideDistance` it becomes `imageViewIdx * slideDistance`. In the case of B returning to its original position, it actually returns to the position of C. In the case of C returning to its original position, it goes off the screen to the right. A works fine, but that's just a fluke, because it never has to move at all. So it's apparent that when determining the ImageViews return position, the ObjectAnimator is adding the `slideDistance` onto the ImageViews original starting position, and not the new position after the first click, like I would expect. Is there something that I need to do with ObjectAnimator or a layout after an animation to force it to update a views position? Remember, after the first translation, the ImageViews new click behavior works fine, so there's no problem detecting the new position with that. Also, when I log the ImageViews X coordinate after the first translation using `imageView.getX()` it reports the correct position (the left side of the screen) like I would expect. I'm targeting min API 16. Can anyone help me with this?