Android Property Animation, androidproperty

Source: Internet
Author: User

Android Property Animation, androidproperty
Property Animation introduction:

Born in 3.0, a series of operations are performed using the attributes of the View. For example, the setAbc attribute of a View can be set theoretically.

It not only changes the painting of the View, but also the attributes of the View. Tween Animation only changes the painting of the View.

ValueAnimator is the base class of an animation. It has a subclass of ObjectAnimator. Interpolator and TypeEvaluator are required to calculate the attribute value.

Interpolator time plug-in, Animation speed, see the previous Tween Animation

The factory method of TypeEvaluator Animator can create objects with properties of any value type. TypeEvaluator is used to calculate the attribute value.

Some View attributes are added after 3.0:

1) translationX and translationY: These two attributes control the position of the View,
Their values are set by the layout container and are an offset relative to the coordinate origin (top left corner 0.

2) rotation, rotationX, and rotationY: controls the rotation of a View around the axis points (TX and Ty. It is not consistent with RotateAnimation in Tween Animation.

RotateAnimation rotation, expressed as plane rotation

The rotationX and Y rotate in a three-dimensional way. By default, It is a horizontal line of x y based on the center point of the View.
3) scaleX and scaleY: controls the scaling of View based on transport Tx and transport ty.
4) rotate Tx and rotate TY: rotate the vertices and zoom datum points. By default, they are the center points of the View.
5) x and y: Describes the final position of the view in its parent container, which is the sum of the left Mark and offset (translationX, translationY) in the upper left corner.
6) aplha: transparency. 1 is completely opaque, and 0 is completely transparent.

These attributes are similar to the Animation attribute values of Tween Animation.


ObjectAnimator object Animation

This animation can only represent one action attribute at a time.

ObjectAnimator xml implements xml definition Animation

Res/animator/scale_object_animator.xml

<?xml version="1.0" encoding="utf-8"?><objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="2000"    android:propertyName="scaleX"    android:repeatCount="1"    android:repeatMode="reverse"    android:valueFrom="1.0"    android:valueTo="2.0" ></objectAnimator>
Code loading animation xml

Imageview_scale.setBackground (getResources (). getDrawable (R. drawable. a11); ObjectAnimator scaleAnimator = (ObjectAnimator) AnimatorInflater. loadAnimator (this, R. animator. scale_object_animator); scaleAnimator. setTarget (imageview_scale); // you can specify scaleAnimator as the animation target. setDuration (1000); scaleAnimator. setRepeatCount (50); scaleAnimator. start ();
Animation set

Composed of ObjectAnimator and ValueAnimator, the corresponding xml format is similar to <se> <objectAnimator/> <animator/> </set>

Xml definition Animation

Res/animator/set_rotate_scale.xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android" android: ordering = "together"> <! -- Android: ordering together indicates that the animation is run simultaneously, and sequentially indicates that the following animation is executed in sequence --> <set> <objectAnimator android: propertyName = "rotationX" android: repeatCount = "50" android: repeatMode = "reverse" android: valueFrom = "0" android: valueTo = "20"/> <objectAnimator android: propertyName = "rotationY" android: repeatCount = "50" android: repeatMode = "reverse" android: valueFrom = "0" android: valueTo = "45" android: valueType = "floatType"/> </set> <objectAnimator android: propertyName = "scaleX" android: repeatCount = "50" android: repeatMode = "reverse" android: valueFrom = "1.0" android: valueTo = "2.0"> </objectAnimator> <objectAnimator android: propertyName = "scaleY" android: repeatCount = "50" android: repeatMode = "reverse" android: valueFrom = "1.0" android: valueTo = "2.0"> </objectAnimator> </set>


Code loading animation xml 
Imageview_rotate.setBackground (getResources (). getDrawable (R. drawable. a11); AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater. loadAnimator (this, R. animator. set_rotate_scale); animatorSet. setTarget (imageview_rotate); animatorSet. setDuration (1000); animatorSet. setInterpolator (new BounceInterpolator (); // you can specify the anmatorset for the spring plug-in at the end. start ();
PropertyValuesHolder
// Use PropertyValuesHolder to construct an Animator and combine it into a set-like effect: PropertyValuesHolder pvhX = PropertyValuesHolder. ofFloat ("scaleX", 0f, 2.5f); PropertyValuesHolder pvhY = PropertyValuesHolder. ofFloat ("scaleY", 0f, 3f); ObjectAnimator animator = ObjectAnimator. ofPropertyValuesHolder (imageview, pvhX, pvhY); animator. setDuration (2000); animator. start ();
ViewPropertyAnimator

Obtain ViewPropertyAnimator through view. animate ()

Imageview. setBackground (getResources (). getDrawable (R. drawable. a11); ViewPropertyAnimator animate = imageview. animate (); // This object does not have the setRepeat method. // you can set the animation attributes to combine them into an animation similar to the set effect. alpha (0); animate. rotationX (50); animate. translationXBy (500); animate. scaleX (1.5f); animate. scaleY (1.5f); animate. setInterpolator (new BounceInterpolator (); animate. setDuration (2000); animate. start ();

ValueAnimator

The ValueAnimator code and xml settings do not contain setPropertyName. Because it is not an operation object, it only performs an action based on the value.
You need to add a listener to handle changes to the listener value.

Xml definition Animation

<?xml version="1.0" encoding="utf-8"?><animator xmlns:android="http://schemas.android.com/apk/res/android"     android:interpolator="@android:anim/accelerate_interpolator"    android:duration="10000"    android:startOffset="1000"    android:repeatCount="infinite"    android:repeatMode="restart"    android:valueFrom="1"    android:valueTo="100"    android:valueType="intType"></animator>
Code loading animation xml
ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater. loadAnimator (this, R. animator. animator); valueAnimator. setTarget (TV _num); valueAnimator. setEvaluator (new TypeEvaluator <Integer> () {@ Override public Integer evaluate (float fraction, Integer startValue, Integer endValue) {System. out. println ("percentage, fraction:" + fraction); System. out. println ("result value:" + (int) (startValue + fraction * (endValue-startValue)/10*10); return (int) (startValue + fraction * (endValue-startValue)/10*10) ;}}); valueAnimator. addUpdateListener (new AnimatorUpdateListener () {@ Overridepublic void onAnimationUpdate (ValueAnimator animation) {// return the evaluate value of the current frame of the first animation in onAnimationUpdate. out. println ("animation. getAnimatedValue () = "+ animation. getAnimatedValue (); TV _num.setText (animation. getAnimatedValue () + "") ;}}); // valueAnimator. setInterpolator (new LinearInterpolator (); valueAnimator. start ();

Animator listener

new AnimatorListener() {@Overridepublic void onAnimationStart(Animator animation) {}@Overridepublic void onAnimationRepeat(Animator animation) {}@Overridepublic void onAnimationEnd(Animator animation) {}@Overridepublic void onAnimationCancel(Animator animation) {}}

AnimatorListenerAdapter
New AnimatorListenerAdapter () {// empty implement the AnimatorListener @ Overridepublic void onAnimationCancel (Animator animation) {super. onAnimationCancel (animation);} @ Overridepublic void onAnimationEnd (Animator animation) {super. onAnimationEnd (animation);} @ Overridepublic void onAnimationRepeat (Animator animation) {super. onAnimationRepeat (animation);} @ Overridepublic void onAnimationStart (Animator animation) {super. onAnimationStart (animation);} @ Overridepublic void onAnimationPause (Animator animation) {super. onAnimationPause (animation);} @ Overridepublic void onAnimationResume (Animator animation) {super. onAnimationResume (animation );}}
AnimatorUpdateListener

New AnimatorUpdateListener () {@ Overridepublic void onAnimationUpdate (ValueAnimator animation) {// In onAnimationUpdate, this value returns the evaluate value of the current frame of the first animation System. out. println ("animation. getAnimatedValue () = "+ animation. getAnimatedValue ());}}

Some operation functions: animator. pause (); animator. resume (); animator. reverse (); animator. end (); animator. cancel ();

Animator. start (); animator. isStarted (); animator. isPaused (); animator. isRunning ();


Use attribute animation for background color

For details, see bouncingbils. java under ApiDemo.

Private static final int RED = 0xffFF8080; private static final int BLUE = 0xff80ff; private static final int CYAN = 0xff80ffff; private static final int GREEN = 0xff80ff80; {// animation color change ObjectAnimator colorAnim = ObjectAnimator. ofInt (this, "backgroundColor", CYAN, BLUE, RED); colorAnim. setTarget (ll_animation); colorAnim. setEvaluator (new ArgbEvaluator (); colorAnim. setRepeatCount (ValueAnimator. INFINITE); colorAnim. setRepeatMode (ValueAnimator. REVERSE); colorAnim. setDuration (3000); colorAnim. start ();}

LayoutTransition
/** Use LayoutTransition in ViewGroup to listen for layout changes, and create an animation * LayoutTransition. when the * CHANGE_APPEARING * CHANGE_DISAPPEARING sub-view disappears, the * CHANGING * ViewGroup layout is: android: animateLayoutChanges = "true" uses the default LayoutTransition to create an animation */LayoutTransition layoutTransition = new LayoutTransition (); layoutTransition. setDuration (5000); layoutTransition. setAnimator (LayoutTransition. APPEARING, scaleAnimator); ll_animation.setLayoutTransition (layoutTransition); final TextView TV = new TextView (this); TV. setWidth (100); TV. setHeight (100); TV. setText ("People's Republic of China"); ll_animation.addView (TV); // corresponding type = Runtime (new Runnable () {@ Overridepublic void run () {ll_animation.removeView (TV );}}, 2000 );

For more information, see LayoutAnimations. java in ApiDemos.


What should I do if I want to see an animation in an android program?

In the Android FrameWork, three Animation implementation methods are provided: Frame-by-Frame Animation, View Animation, and Property Animation ).

Based on the descriptions in the SDK, these three functions are powerful: frame-by-frame <view animation <attribute animation.
I. frame-by-frame animation:
This animation collects all the static images in the animation process, displays these images in sequence, and uses the principle of "visual stay" of the human eye, animation effects for users.

Ii. view animation:
It is also called a Tween animation. According to these two definitions, we can see some features of the animation method:
1) The animation method is only for View objects, such as ImageView and Button;
2) To implement this animation, you only need to provide the relevant attributes of two key frames. Android will give you an animation gradient process for two key frames within a specified period of time.

3. Property Animation:
Android introduces property animation in 3.0. Different from view animation's focus and view effect, view animation focuses more on Object attribute changes and implements animation by changing object attributes, regardless of whether the object is visible or not. For example, if you use a view animation to double a Button, the effect on the interface can be achieved, but the touch response area of the Button is the same as the original one, that is, view animation does not actually double the Button.
Www.linuxidc.com/Linux/2013-01/78069.htm

In Android development, Animation does not run for the first time. Does it start to work normally for the second time?

Was numberbtn hidden at first? If yes, add the previous numberbtn. setVisible (true); if it is not before you run numberbtn. clearAnimation (); try again and do not quite understand the logic you described

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.