Today I will be writing about a very short post post on how to animate a view to slide in from Right side of the screen.

In order to achieve this we will be using the Object Animator class of Android. We will be using translate property on X axis so that it translates from Right to Left.

Another class that we will be using shall be Layout Transition. This is responsible for animating the layout changes. For ex: If you require to do some animations when a view is being added, removed or being changed, then you will have to use this class only.

Lets have a look at few lines of the code :


ValueAnimator anim = ObjectAnimator.ofFloat(v, "translationX", start,end);
anim.setInterpolator(interpolator);
anim.setDuration(duration);

 

In the above code we are translating it on X axis from a start position to end position. To translate from Right the start position will be something greater than equal to the width of screen and end position shall be 0.

Below I am posting the complete source code:

SlideFromRightAnimation.java

 

Now how to use the above class:

final ViewGroup vg = Parent View group
final View v = View which you want to animate
LayoutTransition tr = new LayoutTransition();
SlideFromRightAnimation sfr = new SlideFromRightAnimation(this, 0);
tr.setAnimator(LayoutTransition.APPEARING, sfr.getAnimator(v));
tr.setDuration(500);
tr.setStartDelay(LayoutTransition.APPEARING, 0);
vg.setLayoutTransition(tr);
vg.addView(v);

Now its done, when your view will be added it should come in from Right side.

Now lets see, how it will show up once integrated. Here I integrated it in one of my projects.

Leave a Reply