Android ListView Animation Effect principle and source code

Source: Internet
Author: User

Android animation in three kinds, where property animation for our most commonly used animation, and to meet the development of almost all the needs of the project, Google's official package support 3.0+, we can refer to the three-party package Nineoldandroids to the low version. In this example, the effect is achieved using attribute animations.

To animate a normal view, we just need to define the animated Objectanimator or Animatorset, then set the properties to start and OK. But how, when, where, and which view do you animate the ListView?

GitHub has a mature ListView animation package Https://github.com/nhaarman/ListViewAnimations.git, basically can meet the more dazzling effect, such as 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? The discovery of project implementation principles on GitHub is not complicated, so let's look at the open source project to implement our own ListView animations.

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

If you need to do an animation to delete the item, you can pass the view and the position to the animation or animated set when GetView, this is relatively simple to see 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 to delete complex, need to calculate the start time of each item animation, and determine whether animation reality, there is the animation to follow what kind of rules or order, the calculation of the actual time of animation and other factors. And not good to optimize the performance of the list, and display effect. The following example only achieves the basic animation effect, the performance optimization lacks.


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); }


Refer to the above method to achieve your desired listview item animation, and can go to customize the desired effect.

This example source address:

Https://github.com/CankingApp/ListAnimator

A lot of suggestions, more exchanges ~

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.