Property Animation in Android -- Android development art exploration notes

Source: Internet
Author: User

Property Animation in Android -- Android development art exploration notes

Animator was first introduced in Android 3.0. Compared with the previous Animation framework, Animator is more flexible and has more functions. It is officially recommended to use Animator instead of Animation. Before 3.0, you can use nineoldandroids to achieve the same effect.

Before using Animator, you need to understand several concepts:

Duration: animation playback Time interpolation: the attribute value changes over Time, such as linear growth or fast first and then slow Repeat count: Number of animation replay times Animator sets: animation set, multiple animations can be played simultaneously or in sequence. Frame refresh delay: Specifies the refresh time of each Animation Frame. By default, the Property Animation is refreshed once every 10 ms.

Property Animation is very powerful. It allows you to play animations on almost everything. The structure of Property Animation is as follows:

ValueAnimator is used to track the animation running time and attribute values. TimeInterpolator specifies the animation interpolation, such as AccelerateDecelerateInterpolator. How to calculate the attribute value specified by TypeEvaluator, for example, IntEvaluator.

Example

Click a Button to increase the width and height of the Button to 500dp and 400dp respectively.

The difference between Property Animation and View Animation: View Animation can only be used to add an Animation to a View. The Animation value can only be changed, for example, scale or rotation. Attribute View Animation, such as background color, cannot be changed, but only the position of View painting, it is not a real View. For example, if a button is moved from left to right, The onClick method is triggered or the initial position. View Animation implementation is simpler than Property Animation Evaluator

Taking IntEvaluator as an example, the source code is as follows:

   public Integer evaluate(float fraction, Integer startValue, Integer endValue) {        int startInt = startValue;        return (int)(startInt + fraction * (endValue - startInt));    }

It is easy to calculate the value of an attribute based on the initial value, the end value, and the ratio of the current time to the total duration.

Animator

Animator provides the basic structure for creating an animation. Instead of directly using it, we usually use its subclass.

ValueAnimator

Use ofInt (), ofFloat (), or ofObject () to obtain the ValueAnimator instance.

ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);animation.setDuration(1000);animation.start();

You can also customize the type.

ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(), startPropertyValue, endPropertyValue);animation.setDuration(1000);animation.start();
Source code of the example Animation
  button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                final IntEvaluator mEvaluator = new IntEvaluator();                ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100);                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {                    @Override                    public void onAnimationUpdate(ValueAnimator valueAnimator) {                        float fraction = valueAnimator.getAnimatedFraction();                        button.getLayoutParams().width = mEvaluator.evaluate(fraction, button.getWidth(), 500);                        button.getLayoutParams().height = mEvaluator.evaluate(fraction, button.getHeight(), 400);                        button.requestLayout();                    }                });                valueAnimator.setDuration(1000).start();            }        });

Note: Do not forget button. requestLayout () and valueAnimator. setDuration (1000). start ().

ObjectAnimator

ObjectAnimator is a subclass of ValueAnimator. You can directly calculate the target attribute.
Change the alpha attribute of the object foo from 0 to 1. The Code is as follows:

ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f);anim.setDuration(1000);anim.start();

To make ObjectAnimator run properly, the following steps are required:

The property to be modified must have the set method, such as setFoo... Only one parameter is specified. By default, this is the last parameter. The parameter must have a get method. For example, some attributes of getFoo () must be refreshed manually. Therefore, you must call invalidate () in onAnimationUpdate ().

If there is no attribute to be modified, the set method must be available. There are three solutions:

If you have the permission, add the set Method
But in many cases, we do not... Wrapper class (Packaging class) using this class)
ObjectAnimator. ofFloat (wrapper, "alpha", 0f, 1f), change the alpha attribute value of the original View in setAlph of the packaging class using ValueAnimatorAnimatorSet

A Set contains multiple animations, which are easy to use and can be directly written into the code.

AnimatorSet bouncer = new AnimatorSet();bouncer.play(bounceAnim).before(squashAnim1);bouncer.play(squashAnim1).with(squashAnim2);bouncer.play(squashAnim1).with(stretchAnim1);bouncer.play(squashAnim1).with(stretchAnim2);bouncer.play(bounceBackAnim).after(stretchAnim2);ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);fadeAnim.setDuration(250);AnimatorSet animatorSet = new AnimatorSet();animatorSet.play(bouncer).before(fadeAnim);animatorSet.start();
Declare an animation in XML

Create res/animator/directory
Labels for each type of Animator

ValueAnimator-ObjectAnimator-AnimatorSet-
<code class="language-html hljs "><set android:ordering="sequentially">    <set>        <cke:objectanimator android:propertyname="x" android:duration="500" android:valueto="400" android:valuetype="intType">        <cke:objectanimator android:propertyname="y" android:duration="500" android:valueto="300" android:valuetype="intType">    </cke:objectanimator></cke:objectanimator></set>    <cke:objectanimator android:propertyname="alpha" android:duration="500" android:valueto="1f"></cke:objectanimator></set></code>

Call in Activity

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,    R.anim.property_animator);set.setTarget(myObject);set.start();

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.