Study Notes for Android animation

Source: Internet
Author: User

I used to write my blog in Baidu space. I always felt that I was not professional, had few visits, had few friends, and had little resources to use. So I checked it online yesterday and thought the iteye community was good, I applied for an iteye account and started to write blogs today. I hope you will pay more attention to communication and make progress together.

 

Recently, I have been developing the strongest audio app in China. I feel that my animation knowledge is not solid enough when I make the strongest audio app in China, therefore, this week, Teng learned about animation and graphics. Now, I will summarize the animation knowledge as follows:

 

The description of property animation is as follows. If you are too lazy to read it, you can directly click the connection to download the demo and view the demo address.

 

 

The Android framework provides two types of animations: property animation (available in Versions later than android3.0) and view animation. we recommend that you select property animation because it is more powerful and efficient. In addition to the above two types, you can also use drawable animation, which can help you import resources in drawable and display them one by one.

 

1. Property animation: This animation system helps you implement any object animations, including those that are not added to the interface.

2. View animation: this is an old method and can only be used for Views. It is easier to use and set it.

3. Drawable animation: Like a movie, a drawable can be played one by one.

 

What is propery animation:

 

The property animation system is a robust framework that allows you to animate almost anything. within a certain period of time, property animation can change the value of a property (a field in an object), such as position, animation duration, and animation start time, to control an animation.

 

Property animation attributes include:

 

Duration: Specifies the animation Duration. The default value is 300 ms.
Time interpolation: Specifies the animation inserter. For example, the LinearInterpolator animation changes at a uniform rate.
Repeat count and mode, number of animation repetitions and return status, restart and reverse.
Animation sets: You can place a series of animations in one set or one by one.
Frame refresh delay. You can set the animation refresh time, which is customized to 10 ms.

How property animation works:

 

// Http://developer.android.com/guide/topics/graphics/prop-animation.html#property-vs-view

 

How property animation differs from view animation

 

View animation can only operate on view objects and can only perform rotate, scale, translate, and alpha operations on views. view animation another function that cannot be implemented is it only modified where the View was drawn, and not the actual View itself, it means that the view is not actually displayed when it is animated.

 

The property animation system can animate Views on the screen by changing the actual properties in the View objects. in addition, Views also automatically call the invalidate () method to refresh the screen whenever its properties are changed. the new properties in the view class that facilitate property animations are:

 

TranslationX and translationY: relative to the upper left corner of the parent Control

Rotation, rotationX, and rotationY: coordinates of the center of the rotation

ScaleX and scaleY: coordinates of the zoom Center

Centers Tx and centers TY: Coordinates of centers
X and y: distance Coordinate
Alpha: transparency. The value ranges from 0 to 1.

How to accompish an animation:

 

1, Multiple ObjectAnimator objects

[Java]
ObjectAnimator animX = ObjectAnimator. ofFloat (myView, "x", 50f );
ObjectAnimator animY = ObjectAnimator. ofFloat (myView, "y", 100f );
AnimatorSet animSetXY = new AnimatorSet ();
AnimSetXY. playTogether (animX, animY );
AnimSetXY. start ();

ObjectAnimator animX = ObjectAnimator. ofFloat (myView, "x", 50f );
ObjectAnimator animY = ObjectAnimator. ofFloat (myView, "y", 100f );
AnimatorSet animSetXY = new AnimatorSet ();
AnimSetXY. playTogether (animX, animY );
AnimSetXY. start ();

2, One ObjectAnimator

[Java]
PropertyValuesHolder pvhX = PropertyValuesHolder. ofFloat ("x", 50f );
PropertyValuesHolder pvhY = PropertyValuesHolder. ofFloat ("y", 100f );
ObjectAnimator. ofPropertyValuesHolder (myView, pvhX, pvyY). start ();

PropertyValuesHolder pvhX = PropertyValuesHolder. ofFloat ("x", 50f );
PropertyValuesHolder pvhY = PropertyValuesHolder. ofFloat ("y", 100f );
ObjectAnimator. ofPropertyValuesHolder (myView, pvhX, pvyY). start ();

3, ViewPropertyAnimator

[Java]
? MyView. animate (). x (50f). y (100f );

MyView. animate (). x (50f). y (100f );

 

How to Declaring Animation in XML:

 

Android development allows you to use xml files instead of programming to use animations. With xml files, you can easily reuse or reedit your animations in multiple activities.

To distinguish between the new property animation and the old view animation, change the file name from res/anim to res/animator from android3.1 and later. Three animation attributes can be set in xml:

ValueAnimator-<animator>
ObjectAnimator-<objectAnimator>
AnimatorSet-<set>
The usage is as follows:

[Java]
<Setandroid: ordering = "sequentially">
<Set>
<ObjectAnimator
Android: propertyName = "x"
Android: duration= "500"
Android: valueTo = "400"
Android: valueType = "intType"/>
<ObjectAnimator
Android: propertyName = "y"
Android: duration= "500"
Android: valueTo = "300"
Android: valueType = "intType"/>
</Set>
<ObjectAnimator
Android: propertyName = "alpha"
Android: duration= "500"
Android: valueTo = "1f"/> </set>

<Setandroid: ordering = "sequentially">
<Set>
<ObjectAnimator
Android: propertyName = "x"
Android: duration= "500"
Android: valueTo = "400"
Android: valueType = "intType"/>
<ObjectAnimator
Android: propertyName = "y"
Android: duration= "500"
Android: valueTo = "300"
Android: valueType = "intType"/>
</Set>
<ObjectAnimator
Android: propertyName = "alpha"
Android: duration= "500"
Android: valueTo = "1f"/> </set>

Code:

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

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

 

Animation api overview:

 

In the android. animation package, you can find many property animation APIs. In the android. view. animation package, you can find many defined imterpolators. below is the components in propery animation system.

 

1, what is supported ded in animator class

 

ValueAnimator is inherited from animator. google interprets it as The main timing engine for property animation that also computes the values for the property to be animated, which is to calculate The position of the image every 10 ms.
ObjectAnimator inherits from ValueAnimator. google interprets it as A subclass of ValueAnimator that allows you to set a target object and object property to animate. More functions than ValueAnimator are not only capable of Location Calculation, you can also refresh the new position of the image.
AnimatorSet, Provides a mechanic to group animations together so that they run in relation to one another is to combine a lot of animations for refresh and display.
 

2, what and how to use Evaluators

Evaluators tell the property animation system how to calculate values for a given property. for example, IntEvaluator/FloatEvaluator/ArgbEvaluator/TypeEvaluator.

Using a typeEvaluator:

[Java]
Public class FloatEvaluator implements TypeEvaluator {
Public Object evaluate (float fraction, Object startValue, Object endValue ){
Float startFloat = (Number) startValue). floatValue ();
Return startFloat + fraction * (Number) endValue). floatValue ()-startFloat );
}
}

Public class FloatEvaluator implements TypeEvaluator {
Public Object evaluate (float fraction, Object startValue, Object endValue ){
Float startFloat = (Number) startValue). floatValue ();
Return startFloat + fraction * (Number) endValue). floatValue ()-startFloat );
}
}

 

3, what and how to use interpolator

A time interpolator defines how specific values in an animation are calculated as a function of time.

 

AccelerateDecelerateInterpolator accelerates and then slows down,
AccelerateInterpolator always accelerates
AnticipateInterpolator
OvershootInterpolator: indicates to throw a certain value forward before returning to the original setting,
BounceInterpolator: It indicates that the animation starts when it ends,
CycleInterpolator: specifies the number of times the animation is played cyclically, and the speed changes along the sine curve.
DecelerateInterpolator: indicates that the animation starts fast and then slow.
LinearInterpolator: constant rate change
AnticipateOvershootInterpolator: returns the last value after a certain value is pushed forward at the beginning.
TimeInterpolator, Aninterface
That allows you to implement your own interpolator.

Using interpolators:

AccelerateDecelerateInterpolator:

[Java]
Public float getInterpolation (float input ){
Return (float) (Math. cos (input + 1) * Math. PI)/2.0f) + 0.5f;
}

Public float getInterpolation (float input ){
Return (float) (Math. cos (input + 1) * Math. PI)/2.0f) + 0.5f;
}
 

LinearInterpolator:

[Java] view plaincopyprint? Public float getInterpolation (float input ){
Return input;
}

Public float getInterpolation (float input ){
Return input;
}

 

Animating with ValueAnimator/Animating
With ObjectAnimator: see the demo for the use of these two types of animator. The demo also includes the use of AnimatorSet and animatorListener.

 

Animating Layout changes to ViewGroup:

 

As mentioned earlier, perporty animation can be used not only in view, but also in wiewgroup.

 

APPEARING: displays the status of the view.
CHANGE_APPEARING: displays the status of the parent control of the view.
DISAPPEARING
CHANGE_DISAPPEARING: status of the parent control whose view is disappearing
Property animation is introduced here first. If you have any questions, click the link to download the demo and view the demo address.

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.