Uses of animations make your application more realistic, more attractive and easy to use. So in this blog, we are going to show you how to add basic animation in your Views to make it attractive by java code. Animations can add be notify users about what’s going on in your app and improve their thinking model of your app’s interface
Implementation
This animation is of view translation with fade in and fade out effect on the view,
In xml,
12345 <LinearLayoutandroid:id="@+id/content_frame"android:layout_width="match_parent"android:layout_height="55dp"android:background="@color/background_color" />
In Java,
12 mBinding.toolbar.animate().translationY(-(mBinding.toolbar.getHeight()).setDuration(1000).alpha(0.0f);
By above function, you can translate your view with fade out effect.
- translationY: translate your view in Y direction.
setDuration(1000)
sets the duration of the animation to 1 secondsalpha(0)
the view’s alpha property will be animated to this value (completely fade out)
You can also add the listener to your animation that what you want to do before or after performing the animation.
123456789101112131415161718192021 mBinding.toolbar.animate().alpha(0.0f).translationY(0).setListener(new Animator.AnimatorListener() {@Overridepublic void onAnimationStart(Animator animation) {}@Overridepublic void onAnimationEnd(Animator animation) {mBinding.toolbar.setVisibility(View.GONE);}@Overridepublic void onAnimationCancel(Animator animation) {}@Overridepublic void onAnimationRepeat(Animator animation) {}});
Thanks for the reading this blog, if you have any doubt and query comment below.
Happy Coding and Stay Super.