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:

// 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);

Add a comment

Comments (20)

  1. หนังโป, February 3, 2016
    It's really very compleҳ in tɦіs fսⅼl ߋf аctіѵіty lіfе tߋ ⅼistеn neѡs οn Ꭲeⅼеνiѕіοn, tҺսѕ I јսѕt uѕe іntегnet fօг tҺаt гeɑsⲟn, and ⲟbtain tҺе neѡeѕt infоrmatiоn. Reply
  2. Ishaan, November 8, 2014
    This example is a help but you have to figure out your requirement. What I implemented was give the translate animation with an animation listener. On animation end I changed the layout params. Works that way as well. Reply
  3. Edgar, October 3, 2014
    First of all, thank You for main idea. The main idea of an example (to set layout params and then apply animation) works like charm. But the whole example is a bit wrong. First of all the parameter order is wrong as someone said it should be TranslateAnimation(-x, 0, -y, 0). Second - actually -x,-y works wrong should be TranslateAnimation(-(x-oldX), 0,-(y-oldY),0); But still thank You for idea working on both 2.3 and 4.3.... Reply
    • Anshul Tyagi, October 30, 2014
      I also have a problem in this translation. Can you please help me too?? If yes give me your ID, I'll forward you my code. Thanks Reply
  4. 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
  5. 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
  6. Gilian, July 14, 2013
    perfect man, awesome, works nice with 2.3 and 4.1.2 Reply
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. Nik, March 20, 2011
    Argh Now your solution leaves an ugly trail behind until finished :( Did you experince that problem too ? Reply
  16. Nik, March 20, 2011
    Fought this problem for hours! You just made my day! Reply

Add a comment

(it will not be shared)