Android ListView animation effect implementation principle and source code
Android animations are divided into three types. Attribute animations are the most commonly used animations and can meet almost all development needs in the project. The official google package supports more than 3.0, we can reference the third-party package nineoldandroids To get out of service to a lower version. In this example, the property animation is used to achieve the effect.
To make an animation for a normal View, we only need to define the desired animation ObjectAnimator or AnimatorSet, and then set the property to start and enable. However, what should we do for ListView animation, when, where, and which View?
Github has mature listview animation package https://github.com/nhaarman/ListViewAnimations.git, basically can meet the more dazzling effect, such as google + dynamic list loading, a variety of dynamic list display and delete. The problem is, if we cannot meet our needs, how can we achieve the desired ListView animation effect? It is not complicated to study the implementation principle of the project on github. Next we will refer to the open-source project to implement our own ListView animation effect.
The getView () method of the Adapter is the first thing you want to animation the items in the ListView. You can get each Item view in getView, define the animation effect, and then animation contentView Based on the animation requirements.
If you need to create an animation to delete an item, you can pass the View and position to the animation or animation set during getView. For more information, see the code:
public static AnimatorSet buildListRemoveAnimator(final View view, final List list, final MyAnimListAdapter adapter, final int index) { AnimatorListener al = new AnimatorListener() { @Override public void onAnimationStart(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animator animation) { // TODO Auto-generated method stub list.remove(index); ViewHolder vh = (ViewHolder) view.getTag(); vh.needInflate = true; adapter.notifyDataSetChanged(); } @Override public void onAnimationCancel(Animator animation) { // TODO Auto-generated method stub } }; AnimatorSet animatorSet = new AnimatorSet(); Animator anim = ObjectAnimator.ofFloat(view, "rotationX", 0, 90); Animator animb = ObjectAnimator.ofFloat(view, "alpha", 1, 0); ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1); final int height = view.getMeasuredHeight(); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { // TODO Auto-generated method stub if (animation.getAnimatedFraction() >= 1) { view.setVisibility(View.GONE); } else { view.getLayoutParams().height = height - (int) (height * animation.getAnimatedFraction()); view.requestLayout(); } } }); anim.setDuration(ANIMATION_DURATION); animb.setDuration(ANIMATION_DURATION); valueAnimator.setDuration(ANIMATION_DURATION + ANIMATION_DURATION + 100); animatorSet.playTogether(anim, animb, valueAnimator); animatorSet.addListener(al); return animatorSet; }
How to Create a listview to dynamically display the effect of each Item. This tutorial describes how to delete an animation. You need to calculate the start time of each item animation, determine whether the animation is realistic, and determine the rules or sequence of the animation to be displayed, and calculate the actual time of the animation. It is difficult to optimize the list performance and display effect. The following example only achieves the basic animation effect and lacks performance optimization.
public static AnimatorSet buildShowAnimatorList(ViewGroup parent, ListView list, View view, long mAnimationStartMillis, int mLastAnimatedPosition, int mFirstAnimatedPosition) { if (mAnimationStartMillis == -1) { mAnimationStartMillis = System.currentTimeMillis(); } ViewHelper.setAlpha(view, 0); Animator alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", 0, 1); Animator rx = ObjectAnimator.ofFloat(view, "rotationX", -90, 0); AnimatorSet set = new AnimatorSet(); set.playTogether(alphaAnimator, rx); set.setStartDelay(calculateAnimationDelay(list, mLastAnimatedPosition, mFirstAnimatedPosition,mAnimationStartMillis)); set.setDuration(DEFAULTANIMATIONDELAYMILLIS); set.start(); return set; } private static long calculateAnimationDelay(ListView list, int last, int first,long starmill) { long delay; int lastVisiblePosition = list.getLastVisiblePosition(); int firstVisiblePosition = list.getFirstVisiblePosition(); int numberOfItemsOnScreen = lastVisiblePosition - firstVisiblePosition; int numberOfAnimatedItems = last - first; if (numberOfItemsOnScreen + 1 < numberOfAnimatedItems) { delay = DEFAULTANIMATIONDELAYMILLIS; } else { long delaySinceStart = (last - first + 1) * DEFAULTANIMATIONDELAYMILLIS; delay = starmill + DEFAULTANIMATIONDELAYMILLIS + delaySinceStart - System.currentTimeMillis(); } return Math.max(0, delay); }
Refer to the above method to achieve the desired ListView Item animation, and you can customize the desired effect.