Automatic animations on Layout Change
If you want to animate views in a layout without writing big codes. Here is a Simple way.
Find the root element of the layout and add this property to it.
android:animateLayoutChanges="true" |
Adding Manual Animations
Fade Out Animation
view.animate().alpha( 0 .0f); |
Fade In Animation
view.animate().alpha( 1 .0f); |
Move Down by height
view.animate().translationY(view.getHeight()); |
Translate animate to origin Position
view.animate().translationY(view.getHeight()); |
Adding Duration
view.animate().alpha( 0 .0f).setDuration( 2000 ); |
Chaining Animations
For example, we will combine some animations from above to form one animation.
Lets see how
view.animate() .translationY(view.getHeight()) .alpha( 0 .0f) .setDuration( 300 ); |
This will translate the view by its height and fade out in a duration on 3 seconds.
Animation Listeners
We can add start and completion listeners for animations. Here is a simple example
view.animate() .translationY(view.getHeight()) .alpha( 0 .0f) .setDuration( 300 ) .setListener( new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super .onAnimationEnd(animation); view.setVisibility(View.GONE); } }); |
More Animations
Left to Right
// To animate view slide out from left to right
public void slideToRight(View view){
TranslateAnimation animate = new TranslateAnimation(0,view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}
Right to Left
// To animate view slide out from right to left
public void slideToLeft(View view){
TranslateAnimation animate = new TranslateAnimation(0,-view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}
Top to Bottom
// To animate view slide out from top to bottom
public void slideToBottom(View view){
TranslateAnimation animate = new TranslateAnimation(0,0,0,view.getHeight());
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}
Bottom to Top
// To animate view slide out from bottom to top
public void slideToTop(View view){
TranslateAnimation animate = new TranslateAnimation(0,0,0,-view.getHeight());
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}