4

I've been trying to find out how to build an animator (xml) that will cause a slide up and slide down effect. How would that work? Thanks

n3wb
  • 1,037
  • 5
  • 12
  • 20
  • 2
    If you are animating views, and are ok with a programmatic response, look into [droidQuery](http://bit.ly/droidQuery)'s way: `$.with(view).slideUp()` and `$.with(view).slideDown()`. – Phil Aug 16 '13 at 18:01
  • How would it be used in a fragment transaction though? if I want to add a fragment and cause a slide up animation? – n3wb Aug 16 '13 at 18:18

1 Answers1

34

Make a folder anim in the res folder of the project. Now add slide_up.xml for slide_up animation. Then add slide_down.xml for slide down animation.

Code for slide_down.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate android:fromXDelta="0" android:toYDelta="-1000" android:duration="1500"/>
</set>

Code for slide_up.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate android:fromXDelta="0" android:toYDelta="1000" android:duration="1500"/>
</set>

Then load the animation in onCreate method consequently:

Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);

To start it attach it to object you want to be animated:

ImageView img = (ImageView) findViewById(R.id.img);
img.startAnimation(slideUp);

Hope I've helped you. :-)

Codeversed
  • 9,287
  • 3
  • 43
  • 42
Helmisek
  • 1,209
  • 11
  • 12
  • 2
    As there is only one `translate` involved in each xml file, you don't need the `set` element wrapped round it. Though if you are using appcompat_v7 you don't need the xml files at all, just use `R.anim.abc_slide_in_bottom` and `R.anim.abc_slide_out_bottom` – Steve Waring Oct 29 '14 at 19:50