How to permanently move view with animation effect in Android

One of the task I was trying to achieve is moving a view from (0, 0) to (x, y) permanently with animation effect. Using TranslateAnimation is not good enough because it only draws the view in new location, but its actual position (invoked by getLayoutParams) is still in (0, 0). I tried attaching an AnimationListener and at the end of the animation and calling “view.setLayoutPararams” to (x, y), but it caused the view to quickly flash from (0, 0) to (x, y) at the end of the animation.

Finally I got this simple solution:

1
2
3
4
5
6
7
// first set the view's location to the end position
view.setLayoutParams(...);  // set to (x, y)
 
// then animate the view translating from (0, 0)
TranslationAnimation ta = new TranslateAnimation(-x, -y, 0, 0);
ta.setDuration(1000);
view.startAnimation(ta);

 
Add a comment

Comments (6)

  1. Zala Janaksinh, April 13, 2012
    hi i want apply the animation on view with view movement please have you idea then show me thank in advance Reply
  2. Rich, December 1, 2011
    Do you have the exact syntax of this line: view.setLayoutParams(...); // set to (x, y) Tried using LinearLayout but don't see how to set the x/y, only with AbsoluteLayout Reply
  3. admin, June 2, 2011
    To Marcos: any kind of view container will work (linearlayout, relativelayout, gridlayout...), though some layout (such as grid) will set z-order of its child views so that the animation might be blocked by the upper views. To Nik, no I didn't see the trail. Could be that your testing device is too slow (I'm using Droid). Reply
  4. Marcos, May 15, 2011
    Do you need to use a particular kind of Layout to contain that view in order to respond to x and y modifications? thanks Reply
  5. Nik, March 20, 2011
    Argh Now your solution leaves an ugly trail behind until finished :( Did you experince that problem too ? Reply
  6. Nik, March 20, 2011
    Fought this problem for hours! You just made my day! Reply

Add a comment

Top
(it will not be shared)