Analyze the key points of animation animation in Android _android

Source: Internet
Author: User
Tags current time documentation

In the API demo View->animation can find four animation demo, the first 3D translate more complex, and finally speak, first speak 2nd Interpolator. The activity corresponds to the Animation3.java in the view package, and the animation_3.xml of the layout.

The layout of the interface is not explained, just a spinner and a textview. This is not the content of this article.

Mainly explain the next few key statements.

Initialization of animation, from the name of the class can be seen is a change of view position of the animation, the parameters of the beginning of the horizontal axis, the end of the horizontal axis, the beginning of the ordinate.

Animation a = new Translateanimation (0.0f, 
        targetparent.getwidth ()-Target.getwidth ()- Targetparent.getpaddingleft ()- 
        targetparent.getpaddingright (), 0.0f, 0.0f); 

The following is the parameter settings for the animation, I added a comment

A.setduration (1000);//Set the time spent in animation A.setstartoffset (300)//Set the animation start of the delay//Set repeat mode, restart for the end of the restart, reverse for the original trajectory reverse back 
    A.setrepeatmode (Animation.restart); 
    Set the number of repetitions, INFINITE is infinite a.setrepeatcount (animation.infinite); Switch (position) {case 0://Accelerate entry to A.setinterpolator (Animati) According to the user's choice of spinner to set target entry mode Onutils.loadinterpolator (this. 
        R.anim.accelerate_interpolator)); 
      Break Case 1://decelerate into A.setinterpolator (this, Android. Animationutils.loadinterpolator. 
        R.anim.decelerate_interpolator)); 
      Break Case 2://Accelerated entry. The difference from the first is that when Repeatmode is reverse, it still returns the Origin a.setinterpolator for acceleration (Animationutils.loadinterpolator t His, Android. 
        R.anim.accelerate_decelerate_interpolator)); 
      Break Case 3://Step back a bit before speeding forward a.setinterpolator (this, Android. 
        R.anim.anticipate_interpolator)); 
    Break  Case 4:/Slow down, and then back a.setinterpolator before you cross the finish line Animationutils.loadinterpolator (this, ANDROID.R 
        . Anim.overshoot_interpolator)); 
      Break Case 5://case 3,4 's combination a.setinterpolator (this, Android. 
        R.anim.anticipate_overshoot_interpolator)); 
      Break Case 6://Stop to come back a few a.setinterpolator (Animationutils.loadinterpolator (this, Android). 
        R.anim.bounce_interpolator)); 
    Break 
  //Let target start executing this animation target.startanimation (a); 

 }

Here are some of the actions that Android has already preset, and we can customize the XML to achieve better animation, the next one.

In addition to Translationanimation, there are alphaanimation, rotateanimation, scaleanimation, using the combination of several matrix movements, can form a series of complex animation effects. Please check the SDK for specific usage.

The whole is relatively simple, as a function of the call, still do not understand the API comments and SDK documentation, there is no difficulty to understand.

Now start looking at the third push, from which View->animation->push can start the activity

Push this demo is mainly to show the view of the switching effect.

The corresponding Java file for the push is the Animation2.java in the view package, and the corresponding XML layout file is Layout/animation_2.xml.

Look at the layout file first, the main use of this page is a viewflipper. With Viewflipper, you can dynamically switch between multiple view, and you can customize the toggle animation, which is shown in this example how to define a toggle animation.

Here are just the key sentences.

Let Viewflipper begin to switch automatically.

 
 

Changes the entry and exit animation effects when clicking on the Spinner option.

public void onitemselected (adapterview parent, View v., int position, long ID) {switch (position) {case 0: 
      Mflipper.setinanimation (Animationutils.loadanimation (this, r.anim.push_up_in)); 
      Mflipper.setoutanimation (Animationutils.loadanimation (this, r.anim.push_up_out)); 
    Break 
      Case 1:mflipper.setinanimation (The animationutils.loadanimation (this, r.anim.push_left_in)); 
      Mflipper.setoutanimation (Animationutils.loadanimation (this, r.anim.push_left_out)); 
    Break Case 2:mflipper.setinanimation (animationutils.loadanimation) (this. 
      r.anim.fade_in)); Mflipper.setoutanimation (This, Android. Animationutils.loadanimation. 
      R.anim.fade_out)); 
    Break 
      Default:mFlipper.setInAnimation (Animationutils.loadanimation (this, r.anim.hyperspace_in)); 
      Mflipper.setoutanimation (Animationutils.loadanimation (this, r.anim.hyperspace_out));Break 

 } 
  }

Here the animation are all custom animation effects, you can find the corresponding XML file within the Res/anim, the following push_up_in.xml to illustrate the definition of the approximate usage.

Because the animation is made up of several animations, the perimeter is surrounded by a set tag, forming a animationset.

The change of the main defined position in the translate label, fromydelta= "100%p", refers to the place where the distance from the height of the view just below the viewflipper begins to appear, and the 100%p is a relative value, greater than 0 below, and less than 0. Toydelta= "0" means that the original location of the layout file is stopped just before it is reached. android:duration= "300" means that the entire action time is 300 milliseconds, and the system automatically adjusts the speed according to this time.

The Alpha label defines transparency, 0 is full transparency, 1.0 is opaque, and the process is 300 milliseconds, allowing view to appear as a gradual process

<set xmlns:android= "Http://schemas.android.com/apk/res/android" > 
  <translate android:fromydelta= "100 %p "android:toydelta=" 0 "android:duration="/> <alpha android:fromalpha= "0.0" android:toalpha= " 
  1.0" Android:duration= "/>" 
</set> 

Push_up_out.xml, and Push_left's two tags are almost the same, should be well understood.

Hyperspace_in.xml is simpler and has only one alpha, so the outer layer has no set label. Startoffset is set to delay.

Hyperspace_out.xml is relatively complex, set contains set, but still consists of a few small gestures together, can be a little bit to understand.

The outermost layer is a set, which is nested with a scale and a set.

The first scale tag can be understood as an axis point in the current view, and in a 700-millisecond time, the horizontal length of the view is changed to 1.4 times times and the height is 0.6 times times faster. As for Fillafter this label I have not understood this function, according to the SDK explanation, is in the continuous animation lets the view keep in the animation last frame, but according to my experiment does not seem to have any effect, this asks the expert advice. (Some of the information on the Internet is said to be set in the code, this is not an Android bug?) A animationset can be used as a subset of another animationset, which is easy to understand. The literal meaning of the rotate label should be easy to understand and not to repeat.

<set xmlns:android= "Http://schemas.android.com/apk/res/android" android:shareinterpolator= "false" > < Scale android:interpolator= "@android: Anim/accelerate_decelerate_interpolator" android:fromxscale= "1.0" an droid:toxscale= "1.4" android:fromyscale= "1.0" android:toyscale= "0.6" android:pivotx= "50%" android:p Ivoty= "50%" android:fillafter= "false" android:duration= "/> <set" android:interpolator= "@andro Id:anim/accelerate_interpolator "android:startoffset=" > <scale android:fromxscale= "1. 4 "android:toxscale=" 0.0 "android:fromyscale=" 0.6 "android:toyscale=" 0.0 "android:pivotx=" 50% "android:pivoty=" 50% "android:duration="/> <rotate "  
      0 "android:todegrees=" -45 "android:toyscale=" 0.0 "android:pivotx=" 50% "android:pivoty=" 50% " Android:duration= "400"/&Gt 

 </set> </set>

Many animations are actually made up of alpha, scale, rotate, translate, and the basic actions. These all belong to tween Animation. There is also a frame Animation, similar to the effect of film, a frame play animation, later.

All properties of the settings in the XML can be found in Java in the corresponding API functions, in the Android SDK documentation can be identified.

In fact, I think I wrote a bit verbose, the Android API name is very canonical, from the name has been able to determine the function of the function.

The main definition of 3D transition is within the animation package, which contains only two Java files.

3D flip is not very complicated, the most important is a function rotation.setanimationlistener (new Displaynextview (position)), in the 99th line of Transition3d. The primary role of this function is to set the events to be triggered before, after, and after the animation begins, through a listener.

The 3D flip effect is composed of two rotato actions, and the second action is initiated by listener after the first action is completed. These two animation are connected and look like the effect of 3D rotation.

At the same time, in Rotate3danimation.java, a animation is redefined, and the Initialize and Applytransformation methods are overridden. The Initialize is the initialization action, the applytransformation defines the animation effect, this is the most important part, is passed in the current time is the total time percent and the action, here utilizes the transformation matrix. Find my linear algebra forgot t_t, and I'll see it again later. The main camara is not very understanding, the note does not write what the use of this class, from the code to guess that this is to save the current interface.

This is simple, but difficult to do.

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.