Some thoughts about attribute animation in Android may be able to solve some performance problems for you

Source: Internet
Author: User

========================================================
Qiujuer
Blog:Blog.csdn.net/qiujuer
Website:Www.qiujuer.net
Open Source Library:Genius-android
Reprint Please specify Source: http://blog.csdn.net/qiujuer/article/details/42531485

--open source of learning, for open source, beginner's mentality, with June mutual encouragement!
========================================================

Order

With the animation for a long time gradually like her, although it may not be female, but I hope it is ~ ~

For a long time, I gradually think about the performance of the problem; in one of my ([Material Design] Materialbutton effect advanced animation Auto-move to align Effect) Introduction button Click Effect of the article used a lot of property animation, but later I thought for a long time, It is a single attribute animation that can solve all problems.

Let's talk about how to animate multiple properties into one.

Begin

In general, we use this property animation when customizing controls or controlling some movement operations, which are property animations that are not as hard to imagine, as long as you look at usage extrapolate.

    private float paintx = 0;    private float painty = 0; Private Property<myview, float> paintyproperty = new Property<myview, float> (Float.class, "PaintY") {@        Override public Float Get (MyView object) {return object.painty;        } @Override public void Set (MyView object, Float value) {object.painty = value;    }    }; Private Property<myview, float> paintxproperty = new Property<myview, float> (Float.class, "PaintX") {@        Override public Float Get (MyView object) {return object.paintx;        } @Override public void Set (MyView object, Float value) {object.paintx = value;    }    }; private void Start () {//paintx Objectanimator Apaintx = Objectanimator.offloat (this, paintxproperty, 0, 100        );        Painty objectanimator Apainty = Objectanimator.offloat (this, paintyproperty, 0, 100); Animatorset Animatorset set = new Animatorset ();        Set.playtogether (Apaintx, Apainty);    Set.start (); }

In the above we have created a property animation about coordinates, in general the coordinates have XY, so we have defined two property animations, and then packaged into a animatorset to manage . As for what is Animatorset? You can understand that this class is a property of the management class, you can add N more animations into which, and then the unified scheduling on stop; in Animatorset, an animation queue is maintained.

In the above although the final package to Animatorset in the unified scheduling, but you look at the animatorset part of the source code you know it is finally started each of the properties of animation to start.

This is what we need to think about today, you are now completing a very complex operation, which not only contains a coordinate or a bunch of coordinates, but also contains a lot of color changes, when you use property animation is not a wise choice; You think you're going to finish what I said. How many property animation classes do I need to create? A lot of it ~ waste memory CPU? Do you dare say no???

Since today is to say optimization, then optimize optimization ~ ~ ~

Optimization

We have two solutions for the situation above

First Kind

use a Animation, and then in protected void applytransformation (float interpolatedtime, transformation t) In the context of the callback to deal with the specific situation, specifically can look at my article: Create the ultimate material Design Animation style button has a certain introduction, its efficiency is far higher than the above multiple attribute animation situation.

The second Kind

The first is implemented using the most basic Animation, and the second is the use of attribute animations.

First look at some of the code:

   public static class Viewproperty {        private float paintx = 0;        private float painty = 0;        Public Viewproperty () {        } public        Viewproperty (float paintx, float painty) {            this.paintx = Paintx;            This.painty = Painty;        }    }

Yes, we use an inner class to encapsulate all the attributes once, and then we can use just one property animation; now look at the code:

   Private Viewproperty Mproperty = new Viewproperty (0, 0);    Private Property<myview, viewproperty> paintproperty = new Property<myview, viewproperty> ( Viewproperty.class, "Paintx") {        @Override public        viewproperty Get (MyView object) {            return Object.mproperty;        }        @Override public        void Set (MyView object, Viewproperty value) {            object.mproperty = value;        }    };    private void Start () {        //paint        objectanimator apaint = Objectanimator.ofobject (This, paintproperty, new Viewproperty (0, 0), new Viewproperty (+);        Apaint.start ();    }

Isn't it simple? is not only one property animation: Objectanimator, yes ~ Yes!

It's too early to be happy ~ ~ Look at the actual situation:


Wipe ~ ~ ~ What's wrong with this?? Where did it go wrong??

Thinking

We should think about it, our idea is good, encapsulate an instance of a property collection class, and then you can use just one property animation; But where is the problem?

Let's think that for Float,int These, the changes are made in c=a+ (b-a) *t, but for the object type? How does it subtract, how does it operate?

I think that's where we're supposed to be, and we need to tell the program what it's going to do: c=a+ (b-a) *t a formula that tells it to do 10% of the time when it's equivalent, it should be:new Viewproperty (Ten), this value!

Typeevaluator

Public interface Typeevaluator<t> {/** * This function returns the result of linearly interpolating the star T and end values, with * <code>fraction</code> representing the proportion between the start and end value S. The * calculation is a simple parametric calculation: <code>result = x0 + t * (x1-x0) </code>, * W Here <code>x0</code> are <code>startvalue</code>, <code>x1</code> is <code>     Endvalue</code>, * and <code>t</code> is <code>fraction</code>.     * * @param fraction the fraction from the starting to the ending values * @param startvalue the start value.     * @param endvalue the end value. * @return A linear interpolation between the start and end values, given the * &LT;CODE&GT;FRACTION&LT;/CODE&G T     Parameter. */Public T evaluate (float fraction, t startvalue, T endvalue);}
In the animation class, there is an interface thatThe work done by this interface is the above: c=a+ (b-a) *t

Which is what we can look at:

    public static <t, v> objectanimator ofobject (T-Target, Property<t, v> property,            typeevaluator<v> Evaluator, V ... values) {        Objectanimator anim = new Objectanimator (target, property);        Anim.setobjectvalues (values);        Anim.setevaluator (evaluator);        return anim;    }
There is such a constructor in the instantiation of the property animation Objectanimator class that allows the user to specify his or her own operator interface class in that constructor.

Now we inherit the interface and implement it:

Paintevaluator

    public static class Paintevaluator implements Typeevaluator {        private static final Paintevaluator sinstance = new Pain Tevaluator ();        public static Paintevaluator getinstance () {            return sinstance;        }        Public Object evaluate (float fraction, object Startvalue, Object endvalue) {            viewproperty start = (viewproperty) Start Value;            Viewproperty end = (viewproperty) endvalue;            float x = start.paintx + fraction * (end.paintx-start.paintx);            Float y = start.painty + fraction * (end.painty-start.painty);            return new Viewproperty (x, y);        }    }
It's simple. is to calculate x, y separately, and then return an instance of a class to OK.

End

Then modify the above startup code to:

    Private Viewproperty Mproperty = new Viewproperty (0, 0);    private static Paintevaluator Paintevaluator = new Paintevaluator ();    private static Property<myview, viewproperty> paintproperty = new Property<myview, viewproperty> ( Viewproperty.class, "Paint") {        @Override public        viewproperty Get (MyView object) {            return Object.mproperty ;        }        @Override public        void Set (MyView object, Viewproperty value) {            object.mproperty = value;        }    };    private void Start () {        //paint        objectanimator Paint = Objectanimator.ofobject (this, Paintproperty, Paintevaluator, New Viewproperty (0, 0), new Viewproperty (+));        Paint.start ();    }
OK, you are done ~ ~
Now is simple enough, enough pleasant ~ ~

Wish, everyone can write the code of the desirable ~ ~

========================================================
Qiujuer
Blog:Blog.csdn.net/qiujuer
Website:Www.qiujuer.net
Open Source Library:Genius-android
Reprint Please specify Source: http://blog.csdn.net/qiujuer/article/details/42531485

--open source of learning, for open source, beginner's mentality, with June mutual encouragement!
========================================================

Some thoughts about attribute animation in Android may be able to solve some performance problems for you

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.