I want to include a simple animation in my app where a view widget moves to another view widget.
The code I have written is:
TranslateAnimation animate = new TranslateAnimation(view1.getTranslationX(), view1.getTranslationY(), view2.getTranslationX(), view2.getTranlationY());
animate.setDuration(500);
view1.startAnimation(animate);
The problem is that view1 is a custom view inherited from ViewSwitcher whereas view2 is a Button. getTranslation() gives no such method exception for both.
How can I get the view’s position on screen and is this the right way to do the animation ?
Note: The ViewSwitcher is part of a ListView item so it also needs to cross over its ListView boundary to get to the Button(just in case that makes a difference).
Methods like
getTranslationX()andgetTranslationY()simply return an offset value that may be applied to the actual view position, and not the position itself. You will want to use methods likegetLeft()andgetTop()to get the x/y position value of a view relative to its parent (in this case, theListView).If you need more global coordinates, use
getLocationInWindow()orgetLocationOnScreen()to get the view’s position relative to the global display hierarchy. These methods do not return the position, but rather fill it into theint[]you provide as a parameter.All of these methods can be called on any
View.HTH