Android Animation class and XML animation

Source: Internet
Author: User

Sometimes to add a little animation to the control, in Android, the animation is also a class, that is, the animation class. After the animation effect this class is finished, in conjunction with the control class, you can implement the control has some action effects such as effect. The definition of the animation effect to be in the XML file.

The screen is recording some of the Dayton. But the impact is not big.

The above example is only using the ImageView control, then the different controls with the gradual enlargement, there is a position to translate the action.

Let's talk about how to achieve:

Instantiate the animation class first:

Animation myanimation;//loads an animated XML file through the Loadanimation function to  instantiate the action class myanimation = Animationutils.loadanimation ( Selection.this, R.anim.my_anim);

  

Implement the XML file (the following 4 effects from the band):

Alpha Gradient Transparency Animation effect

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" >    <alpha        android:duration= ""        android:fromalpha= "0.0"        android:toalpha=    "1.0"/> <!--     Transparency Control animation effect Alpha gradient Transparency animation effect        Float Value: Fromalpha property is the Transparency Toalpha property at the beginning of the animation when the transparency description is at   The end of the animation            :                 0.0 indicates full transparency                1.0 represents a full opacity            above value takes a numeric long integer value of float data type between 0.0-1.0                :            duration  property for animation duration            Description:                     the time is in milliseconds    --></set>

Scale gradient dimension stretch animation effect

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <scale android:duration= "" android:fillafter= "false" android:fromxscale= "0.0" a ndroid:fromyscale= "0.0" android:interpolator= "@android: Anim/accelerate_decelerate_interpolator" Android:pivo     tx= "50%" android:pivoty= "50%" android:toxscale= "1.4" android:toyscale= "1.4"/></set><!--            Size Stretch animation effect Scale property: interpolator Specifies an animated insert during my trial, there are three types of animation inserts found when using resources in Android.res.anim: Accelerate_decelerate_interpolator acceleration-deceleration animation Insert accelerate_interpolator acceleration-Animation Insert Decelerate_i                Nterpolator Deceleration-Animation insert other specific animation effects floating-point values: The Fromxscale property is the scaling dimension on the x-coordinate at the start of the animation The Toxscale property is the scaling dimension on the x-coordinate at the end of the animation Fromyscale property is the scaling dimension on the y-coordinate at the start of the animation Toyscale property is the Y-seat at the end of the animation Description of scaling dimensions on the label:                The above four attribute values of 0.0 mean that shrinking to no 1.0 means that the normal no scaling value is less than 1. 0 means that a shrink value greater than 1.0 means that the magnification Pivotx property is the start position of the animation relative to the object's x-coordinate Pivoty property is animated relative to the object's Y                Start position of coordinates description: The above two attribute values from 0%-100% value 50% is the midpoint position on the x or Y coordinate of the object. Long Integer value: Duration property is the animation duration description: The time is in milliseconds Boolean value: Fillafter property When set to True, the animation is converted to the animated knot After the bundle is applied
-

Translate picture conversion position move animation effect

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" >    <translate        android:duration= "Android:fromxdelta=" "        android:fromydelta=" 30 "        android:toxdelta= " -80"        android:toydelta= "/>"    <!--     Translate position transfer animation effect        integer value:            The Fromxdelta property is the position of the x-coordinate at the start of the animation                Toxdelta   property is the position on the x-coordinate at the end of the animation            Fromydelta property is the position on the y-coordinate when the animation starts            Toydelta   property is the position of the y-coordinate at the end of the animation            Note:                     fromxtype toxtype fromytype toytype is not specified, the                     default is to use their own relative reference                     Long integer value:  The Duration property is an animation duration            Description: The   time is in milliseconds--></set>

Rotate picture Transfer rotation animation effect

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <rotate android:duration= "android:fromdegrees=" 0 "android:interpolator=" @android: ANI M/accelerate_decelerate_interpolator "android:pivotx=" 50% "android:pivoty=" 50% "android:todegrees=" +3             "/> <!--rotate rotation animation effect properties: interpolator Specifies an animated insert during my experiment, using resources in Android.res.anim to discover               There are three types of animation inserts: Accelerate_decelerate_interpolator acceleration-deceleration animation Insert Accelerate_interpolator                                  Acceleration-Animation Insert Decelerate_interpolator deceleration-Animation insert other animation effects that belong to a specific                       Floating-point value: The Fromdegrees property is the angle of the object at the start of the animation Todegrees property is at the end of the animation when the object rotates at an angle greater than 360 degrees                                   Description: When the angle is negative--indicates counterclockwise rotation when the angle is positive--Indicates clockwise rotation         (Negative from--to positive: clockwise rotation)               (Negative from--to negative: counterclockwise rotation)                      (Positive from--to positive: clockwise rotation)                            (Positive from--to negative number: counterclockwise rotation) The Pivotx property is the start position of the animation relative to the object's x-coordinate Pivoty property is the beginning of the animation relative to the object's y-coordinate Note: The above two attribute values from the 0%-100% value 50% is the object's X or y-direction coordinates on the midpoint position Long value: Durat The Ion property is the animation duration description: The time is in milliseconds--></set>

Associating animation classes with controls

  

  Control Start Action imgpic.startanimation (myanimation);

Pretty simple, just implement the XML file first, define the type of action. Then, using the Loadanimation function, instantiate the Animaiton class with an XML file. Then you associate the Animaiton class with the control class, and the control begins to have an action effect.

Note, however, that the animation class can only be run in the main thread!!!!!!

And I found a problem, adding an action effect directly to the code, and not automatically executed. And if you define multiple controls that have action effects at the same time, they are executed at the same time, what can be done in order?

The problem with the UI and the main thread, it's natural to think of the handler class.

My solution is: After entering the activity, a new thread is opened, and the thread sends a message to the handler class to execute the action according to our needs, so it can be executed.

This is the code inside the OnCreate:

    protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);                Used to update UI handler = new Handler () {@Override public void Handlemessage (Message msg) in the main thread {                Super.handlemessage (msg); Switch (MSG.ARG1) {case 0: {myanimation = Animationutils.loadanimation (Selectio                        N.this, R.anim.my_anim);                        Imgpic.startanimation (myanimation);                             Imgpic.setvisibility (view.visible);                    }break;                        Case 1: {myanimation = Animationutils.loadanimation (Selection.this, R.anim.studio_anim);                        Lwgstudio.startanimation (myanimation);                    Lwgstudio.setvisibility (view.visible);                    }break; Case 2: {backanimation = Animationutils.loaDanimation (Selection.this, R.anim.back_anim);                        Back.startanimation (backanimation);                    Back.setvisibility (view.visible);                }break;        }            }        };        Transparent status bar GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        Imgpic = (ImageView) Findviewbyid (r.id.ask);        Imgpic.setvisibility (view.invisible);        back = (ImageView) Findviewbyid (r.id.firstback);        Back.setvisibility (view.invisible);        Lwgstudio = (ImageView) Findviewbyid (R.id.lwgstudio);                        Lwgstudio.setvisibility (view.invisible);                                New Thread (New Runnable () {@Override public void run () { Go in one second. Askfood try {Thread.slee                                P (1000);                                } catch (Interruptedexception e) {    E.printstacktrace ();                                } Message OK = new Message ();                                OK.ARG1 = 0;//to differentiate what UI handler.sendmessage (OK) to update;                                try {thread.sleep (2000);                                } catch (Interruptedexception e) {e.printstacktrace ();                                }//After the first action executes, the action Lwgstudio occurs every two seconds ok = new Message ();                                OK.ARG1 = 1;//to differentiate what UI handler.sendmessage (OK) to update;                                try {thread.sleep (1000);                                } catch (Interruptedexception e) {e.printstacktrace ();                                                         }       Finally, the full screen turns green action OK = new Message ();                            OK.ARG1 = 2;//to differentiate what UI handler.sendmessage (OK) to update;            }}). Start (); }

  

Android Animation class and XML animation

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.