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 (16)

  1. Steve, May 28, 2014
    Please note that the example here creating a new TranslateAnimation is wrong. It should have been: new TranslateAnimation(-x, 0, -y, 0); Reply
  2. deepak, April 30, 2014
    Hi, I am creating deck cards game type application.In this application I have created screen in which we use blank frame. I want to that cards is coming from bottom with animation and show on frame.I am using TranslationAnimation but cards is not showing on proper frame.How to fix this problem.Please me Reply
  3. Gilian, July 14, 2013
    perfect man, awesome, works nice with 2.3 and 4.1.2 Reply
  4. Daniel, October 21, 2012
    You are talking about view.setLayoutParams(...); // set to (x, y) But why are you talking about X and y. When i call .setLayoutParams it asks for Width and Height. So if i am not mistaking this is really different from your IDE? I have to do: mViewPager.setLayoutParams(new LayoutParams(width, height)); And i just want to move a view 150 downwards and set the new location 150 downwards. Why is this so hard in Android. Reply
  5. aw4y, August 21, 2012
    I found a simplier solution: add this to TranslationAnimation ta.setFillAfter(true); and it's done! cheers ;) Reply
    • Brian, October 4, 2012
      This won't change the actual position, just how its drawn Reply
  6. Doug, July 6, 2012
    So are you x and y vars integers? When I try to move an ImageView like this, it says setLayoutParams doesn't support int int Reply
  7. Morten Slott Hansen, May 30, 2012
    I had to do things slightly different. X,Y positions are not always an option - especially when working with relative layouts! To move my button and the view I had to do the following: final TranslateAnimation resultSlideUp = new TranslateAnimation(0, 0, 0, distX); resultSlideUp.setDuration(500); resultSlideUp.setFillEnabled(true); resultSlideUp.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationEnd(Animation animation) { RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(result.getWidth(), result.getHeight()); layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, -1); layoutParams.setMargins(0, result.getTop()+distX, 0, 0); result.setLayoutParams(layoutParams); } }); result.startAnimation(resultSlideUp); Reply
  8. 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
  9. 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
  10. 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
  11. 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
    • Amirah, May 23, 2012
      I have to express some thknas to the writer just for bailing me out of such a challenge. Just after searching throughout the the web and seeing suggestions which are not helpful, I thought my entire life was over. Living devoid of the approaches to the problems you've solved all through this post is a crucial case, as well as those that could have adversely damaged my entire career if I hadn't discovered your blog. The training and kindness in maneuvering every part was precious. I'm not sure what I would've done if I hadn't come across such a subject like this. I can also at this point look ahead to my future. Thanks so much for your high quality and effective help. I will not think twice to suggest your web sites to anyone who would need support on this area. Reply
  12. Nik, March 20, 2011
    Argh Now your solution leaves an ugly trail behind until finished :( Did you experince that problem too ? Reply
  13. Nik, March 20, 2011
    Fought this problem for hours! You just made my day! Reply

Add a comment

Top
(it will not be shared)