I have one ImageView in my application which can be situated anywhere on screen
On touch I want to move this view at the center of the Screen. I tried this functionality with Translate Animation and its RELATIVE_TO_PARENT functionality as follows
TranslateAnimation translateAnimation1 = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_PARENT,0.0f,
TranslateAnimation.RELATIVE_TO_PARENT,0.5f,
TranslateAnimation.RELATIVE_TO_PARENT,0.0f,
TranslateAnimation.RELATIVE_TO_PARENT,0.5f);
but ImageView moves 50% (of the screen) down from its current position.
Is there any way to move this view to the center of the screen regardless of its current position?
In order to move a View anywhere on the screen, I would recommend placing it in a full screen layout. By doing so, you won’t have to worry about clippings or relative coordinates.
You can try this sample code:
main.xml
Your activity
The method
moveViewToScreenCentergets the View’s absolute coordinates and calculates how much distance has to move from its current position to reach the center of the screen. ThestatusBarOffsetvariable measures the status bar height.I hope you can keep going with this example. Remember that after the animation your view’s position is still the initial one. If you tap the MOVE button again and again the same movement will repeat. If you want to change your view’s position do it after the animation is finished.