Android Translate Animation

Victor picture Victor · Mar 29, 2012 · Viewed 8.8k times · Source

I have an ImageView that is 250dp above the bottom and with translate animation i want to move it to 50dp above the bottom.

I know how to use a translate animation, but I don't know what would be the ToYValue field.

The code wil be something like this:

TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0,Animation.ABSOLUTE,250,Animation.ABSOLUTE,50);
translate.setDuration(1000);
translate.reset();  
translate.setFillAfter(true);
iv.clearAnimation();
iv.startAnimation(translate);

Answer

user901309 picture user901309 · Mar 29, 2012

You want a fromYValue of 0, meaning start from where it is currently, and a toYValue of 50, meaning move 50 pixels down. Note that these values are in pixels, not dp. If it has to be in dp, that is a whole other question..

The key is in the word "change" from the TranslateAnimation documentation "Change in Y coordinate to apply at the start of the animation."

http://developer.android.com/reference/android/view/animation/TranslateAnimation.html