Some Thoughts on property animation in Android may solve some performance problems for you. android can

Source: Internet
Author: User

Some Thoughts on property animation in Android may solve some performance problems for you. android can

========================================================== ======================
Author: qiujuer
Blog: blog.csdn.net/qiujuer
Website: www.qiujuer.net
Open-source Library: Genius-Android
Reprinted please indicate the source: http://blog.csdn.net/qiujuer/article/details/42531485

-- Learning Open Source for open source; a beginner's mentality is shared with you!
========================================================== ======================

Collation

I have been using animation for a long time and gradually like her. Although it may not be a female, I hope it will be ~~

After using it for a long time, I gradually thought about performance issues. In my article ([Material Design] MaterialButton effect advanced animation, auto-moving, alignment effect) A large number of attribute animations are used in the article about button-based special effects. However, after a long time, I only need one attribute animation to solve all the problems.

Next let's talk about how to merge Multiple Attribute animations into one.

Start

In general, property animations are used when you define custom controls or control some mobile operations. Attribute animations are not as difficult as you think, you just need to check the usage ~

    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 section, we have created an animation about coordinates. Generally, coordinates contain XY, so we have defined two Property animations and then packaged them into an AnimatorSet for management. What is AnimatorSet? You can understand that this class is an attribute management class. You can add more than N animations to it, and then enable the unified scheduling to stop. An animation queue is maintained in the AnimatorSet.

Although it was finally packaged into the AnimatorSet for unified scheduling, you can see some source code in the AnimatorSet and you will know that it is also starting every attribute animation at the end.

This is what we need to think about today. Now you have completed a very complicated operation, including not only a coordinate or a bunch of coordinates, but also a lot of color changes, in this case, attribute animation is not a wise choice. Why? How many property animation classes do you need to create to complete what I said? A lot ~ A waste of memory CPU? Dare you say no ???

Now that we are talking about optimization, We can optimize it ~~

Optimization

We have two solutions for the above situation.

First

Use an Animation, and thenProtected void applyTransformation (float interpolatedTime, Transformation t)For details, see my article: there is a certain introduction to creating the ultimate Material Design animation style Button, which is much more efficient than the above multi-attribute animation.

Second

The first is implemented using the most basic Animation, and the second is property Animation.

Let's take a look at some 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 internal class to encapsulate all the attributes at a time. Then we can use only one attribute animation. Now let's 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(100, 100));        aPaint.start();    }

Is it easy? Is there only one property Animation: ObjectAnimator, yes ~ Yes!

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


Wipe ~~~ What's wrong with this ?? Where is the error ??

Thoughts

We should think about it. Our idea is good. We can encapsulate an instance of an attribute set class and then use only one attribute animation. But what is the problem?

Let's think about it. For Float, Int, all of these changes come with C = A + (B-A) * T, but for the Object type? How can we subtract this and calculate it?

I think this is where we should implement it. We need to tell the program how to calculate it:C = A + (B-A) * TSuch a formula tells it that when the time is equivalent to 10%, it should be:New ViewProperty (10, 10 ),This value!

TypeEvaluator

public interface TypeEvaluator<T> {    /**     * This function returns the result of linearly interpolating the start and end values, with     * <code>fraction</code> representing the proportion between the start and end values. The     * calculation is a simple parametric calculation: <code>result = x0 + t * (x1 - x0)</code>,     * where <code>x0</code> is <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     *         <code>fraction</code> parameter.     */    public T evaluate(float fraction, T startValue, T endValue);}
In the animation class, there is such an interface, The work done by this interface is the above: C = A + (B-A) * T

Here we can look:

    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;    }
In the instantiation of the attribute animation ObjectAnimator class, there is such a constructor that allows you to specify your own operation interface class.

Now we inherit this interface and implement it:

PaintEvaluator

    public static class PaintEvaluator implements TypeEvaluator {        private static final PaintEvaluator sInstance = new PaintEvaluator();        public static PaintEvaluator getInstance() {            return sInstance;        }        public Object evaluate(float fraction, Object startValue, Object endValue) {            ViewProperty start = (ViewProperty) startValue;            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 easy. Calculate x and y respectively, and then return the instance of a class.

End

Then modify the above startup code:

    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(100, 100));        paint.start();    }
OK, success ~~
Now it's easy and comfortable ~~

I wish you all the best code to write ~~

========================================================== ======================
Author: qiujuer
Blog: blog.csdn.net/qiujuer
Website: www.qiujuer.net
Open-source Library: Genius-Android
Reprinted please indicate the source: http://blog.csdn.net/qiujuer/article/details/42531485

-- Learning Open Source for open source; a beginner's mentality is shared with you!
========================================================== ======================

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.