Updated 7 October 2017
We can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.
A sequence of animation instructions defines the tween animation, defined by either XML or Android code. The animation instructions define the transformations that we want to occur, when they will occur, and how long they should take to apply.
Show and hide a view- it is an important part of the application. Many of the cases we want to show and hide a view with user visibility, it is attractive part of visibility, so we need to add an animation to view that render on hiding and showing.
we can take an example- we want to show and hide a view when RecyclerView is scrolling (look once on view part).
How will we do?
Add our RecyclerView with listeners such as OnScrollListener
1 2 3 4 5 6 7 8 9 10 |
RecyclerView.OnScrollListener listener = new RecyclerView.OnScrollListener(){ public void onScrollStateChanged(RecyclerView recyclerView, int newState) { if (newState == RecyclerView.TOUCH_SLOP_PAGING || newState != RecyclerView.SCROLL_STATE_IDLE) { hideViews(); } else { mBinding.listCategoryContainer.animate().alpha(1.0f).translationY(0).setInterpolator(new DecelerateInterpolator(1)).start(); showViews(); } } }; |
1 |
recyclerView.addOnScrollListener(listner); |
add animation showView animation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
private void showViews() { // TODO uncomment this Hide Footer in android when Scrolling mBinding.categoryToolBar.animate().alpha(1.0f).translationY(0).setInterpolator(new DecelerateInterpolator(1.4f)).setListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { mBinding.categoryToolBar.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animator animation) { mBinding.categoryToolBar.setVisibility(View.VISIBLE); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); } |
and HideViewAnimation-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
private void hideViews() { // TODO (+mToolbar) plus means 2 view forward ho jaye or not visible to user mBinding.categoryToolBar.animate().alpha(0f).translationY(+mBinding.categoryToolBar.getHeight()).setInterpolator(new AccelerateInterpolator(1.4f)).setListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { mBinding.categoryToolBar.setVisibility(View.GONE); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); } |
How it looks-
References: developer.android.com
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
2 comments