Animation learning in Android

Source: Internet
Author: User

Animation in Android:

    1. Tween animation: A gradient animation is generated by constantly performing image transformations (translation, scaling, and rotation) on objects in the scenario;
    2. Frame Animation: it is an animation for converting images that are prepared in sequence.

Sort out the usage of the two types of animations.CodeIn the attachment. For example:

 

 

 

The following describes how to use these two types of animations:

 

ITween animation

Tween animation has four forms:

LAlphaGradient transparency animation effect

LScaleGradient scaling animation effect

LTranslateAnimation Effect

LRotateImage Rotation animation effect

 

These four animation implementation methods are implemented through the combination of the animation class and the animationutils.

It can be implemented through XML: the XML file of the animation is in the Res/anim directory of the project.

For example, rotate. xml

 

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Set  Xmlns: Android  = "Http://schemas.android.com/apk/res/android"  Android: fillafter  = "False"  Android: zadjustment  = "Bottom"      >      < Rotate  Android: fromdegrees  = "0"  Android: todegrees  = "360"  Android: strongtx  = "50%"  Android: policty  = "50%"  Android: Duration  = "4000"          />  </  Set  > 

 

Animation implementation:

 
Animation anim =Animationutils. loadanimation (mcontext, R. anim. Rotate );//Listen to the animation status (START, end)Anim. setanimationlistener (NewEffectanimationlistener (); textwidget=(Textview) findviewbyid (R. Id. text_widget); textwidget. settext ("Screen transfer rotation animation effect"); Textwidget. startanimation (anim );

 

The specific usage method does not need to be introduced. For the usage methods and effects of each form, see the example in the attachment.

 

IIFrame Animation

Frame Animation is an image prepared in advance for sequential playback, similar to a movie. Unlike the animation package,

The android SDK provides another class of animationdrawable to define the frame animation.

 

 
Using XML files: Res/drawable-hdpi/frame. xml:

 

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Animation-list  Xmlns: Android = "Http://schemas.android.com/apk/res/android"  Android: oneshot  = "True"    >         <  Item  Android: drawable  = "@ Drawable/P1"  Android: Duration  = "1000"  > </  Item  >         <  Item  Android: drawable = "@ Drawable/P2"  Android: Duration  = "1000"  > </  Item  >         <  Item  Android: drawable  = "@ Drawable/P3"  Android: Duration  = "1000"  > </  Item  >         < Item  Android: drawable  = "@ Drawable/P4"  Android: Duration  = "1000"  > </  Item  >         <  Item  Android: drawable  = "@ Drawable/P5"  Android: Duration  = "1000"  > </  Item >         <  Item  Android: drawable  = "@ Drawable/P6"  Android: Duration  = "1000"  > </  Item  >  </  Animation-list  > 

 

Animation implementation:

AnimationdrawableAnim = (animationdrawable) getresources (). getdrawable (R. drawable. Frame );CustomanimdrawableCusanim = new customanimdrawable (anim); cusanim.Setanimationlistener(New frameanimationlistener (); textwidget = (textview) findviewbyid (R. Id. text_widget); textwidget. settext ("image frame-by-frame animation effect"); textwidget.Setbackgrounddrawable(Anim); cusanim. Start ();

 

The difference here is that when animation is implemented using animationdrawable, there is no interface provided to listen to the animation status (START, end ),

Here I have implemented a simple method to judge the animation status.Customanimdrawable is self-written and inherited from animationdrawable

A class,This parameter is used to determine the number of frames to be played, avoiding the impact of inconsistency between the theoretical time and the actual time when it is determined based on time.

Java reflection mechanism is used.

 Customanimdrawable implementation:

 Public   Class Customanimdrawable Extends  Animationdrawable {  Private  Final String tag = "XMP" ;  Private  Animationdrawable morianim;  Private  Animationdrawable mself;  Private  Handler mhandler;  Private   Boolean  Mstarted;  Private  Animendlistenerrunnable mendrunnable;  Private Animationdrawablelistener mlistener;  Public  Customanimdrawable (animationdrawable anim) {morianim = Anim; initialize ();}  Private   Void  Initialize () {mself = This  ; Mstarted = False  ; Mhandler = New  Handler (); mendrunnable = New  Animendlistenerrunnable ();  For ( Int I = 0; I <morianim. getnumberofframes (); I ++ ) {Mself. addframe (morianim. getframe (I), morianim. getduration (I) ;}@ override  Public   Void  Start () {morianim. Start (); mstarted = True  ; Mhandler. Post (mendrunnable );  If (Mlistener! =Null  ) {Mlistener. onanimationstart (mself);} log. V (tag, "------ Customanimdrawable ------> start" );}  /**  * Cyclically checks the animation status.  */      Class Animendlistenerrunnable Implements  Runnable {@ override  Public   Void  Run (){  // Animation started              If (! Mstarted ){  Return  ;}  //  Continue listening              If (! Isend ()){
// The latency here is related to the animation time of each frame. It can be basically the same, and the last frame will be completely played.
// The animation time above is 1000 ms for each frame, so it is set to 1000 msMhandler. postdelayed (mendrunnable, 1000 ); Return ;} Log. V (tag, "-----------> Over" ); // Animation ended If (Mlistener! = Null ) {Mstarted = False ; Mlistener. onanimationend (mself );}}} /** * Determines whether the animation ends with a reflection mechanism * @ Return */ Private Boolean Isend () {class <Animationdrawable> animclass = animationdrawable. Class ; Try { // Use the Java reflection method to determine whether to end // Obtain private methods such // Method method = animclass. getdeclaredmethod ("nextframe", Boolean. Class ); // Access its private variables Field field = animclass. getdeclaredfield ("mcurframe" ); Field. setaccessible ( True ); Int Currframenum = Field. getint (morianim ); Int Totalframenum = Morianim. getnumberofframes (); If (Currframenum = totalframenum-1) | (Currframenum =-1)){ Return True ;}} Catch (Exception e) {log. V (tag, "--------> Exception" );} Return False ;} Public Void Setanimationlistener (animationdrawablelistener listener) {mlistener =Listener ;} Public Interface Animationdrawablelistener { /** * Notifies the start of the animation * @ Param Animation */ Public Void Onanimationstart (animationdrawable animation ); /** * Notifies the end of the animation * @ Param Animation */ Public Void Onanimationend (animationdrawable animation );}}

 

You can download this example, including the use of basic animations.

Code: Http://files.cnblogs.com/bastard/animationTest.rar

 

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.