Android ListView Animation Effect Implementation principle and source code

Source: Internet
Author: User

Android animation is divided into three kinds, the property animation for we most often use animation, and can meet the project development almost all needs, Google official package support 3.0+. We can quote three-party package nineoldandroids to be excused to the lower version number. In this example, an attribute animation is used to achieve the effect.

To animate a normal view, we just need to define the animated objectanimator or Animatorset. Then set the properties to start and available.

But. How, when, where, and in what view do I animate the ListView?

GitHub has a mature ListView animation package Https://github.com/nhaarman/ListViewAnimations.git, basically can meet the more dazzling effect, for example, Google + dynamic list loading, Various dynamic list display and delete.

The question is, if we don't meet our needs, how do we achieve the ListView animation effect we want? It is not complicated to study the project realization principle found on GitHub. Below we will take a look at the open source project to achieve its own ListView animation effect.

To animate the ListView item, the first thing to think about is the adapter GetView () method. You can get each item view in the GetView, then define the animation effect and animate the Contentview with the animation requirements.

If you need to do an animation to delete item. The ability to pass the view and the position to the animation or animated set when GetView, which is simpler to see in the code:



public static Animatorset Buildlistremoveanimator (final view view, final list list, final Myanimlistadapter Ada  Pter, final int index) {Animatorlistener Al = new Animatorlistener () {@Override public void Onanimationstart (Animator animation) {//TODO auto-generated method stub} @Overrid            e public void Onanimationrepeat (Animator animation) {//TODO auto-generated method stub } @Override public void Onanimationend (Animator animation) {//TODO Auto-generat                Ed method Stub List.remove (index);                Viewholder VH = (viewholder) view.gettag ();                Vh.needinflate = true;            Adapter.notifydatasetchanged (); } @Override public void Onanimationcancel (Animator animation) {//TODO auto-generate        D 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 O Nanimationupdate (Valueanimator animation) {//TODO auto-generated method stub if (animation                . Getanimatedfraction () >= 1) {view.setvisibility (view.gone); } else {view.getlayoutparams (). Height = height-(int) (Heig                    HT * 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 do a ListView dynamic reality for each item display effect. This teaches deletion complexity, the need to calculate the start time of each item animation, and to infer whether the animation is realistic. There are also factors such as how to calculate the actual time of animation according to the rules or order of the animation. And not good to optimize the performance of the list, and display effect.

The following example simply implements the basic animation effect. Lack of performance optimization.


public static Animatorset Buildshowanimatorlist (viewgroup parent, ListView list, view view, Long Manimationstartmillis, int mlastanimatedposition, int mfirstanimatedposition) {if (Manimationstartmillis = =-1) {Man        Imationstartmillis = 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); }


The above method can achieve the desired ListView item animation, and can go to define the desired effect.

This example source code address:

Https://github.com/CankingApp/ListAnimator

A lot of suggestions. A lot of communication ~

Android ListView Animation Effect Implementation principle and source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.