Two ways to use Android animations.

Source: Internet
Author: User

Android Animation classification will not say, mainly about the two ways to use the animation: 1. Generate and use animations through code (not conducive to reuse) 2generate and use animations from an XML file (more convenient and highly reusable). 1. Generating animations from code? 12345//InitializeAnimation scaleanimation =NewScaleanimation (0.1f, 1.0f,0.1f,1.0f);//Set Animation timeScaleanimation.setduration (500);  This. Startanimation (scaleanimation);//InitializeAnimation translateanimation =NewTranslateanimation (0.1f, 100.0f,0.1f,100.0f);//Set Animation time translateanimation.setduration (+); this.startanimation (translateanimation);steps:1. Declare an animation 2. Set animation time (you can also set other properties such as number of animations) 3. Use animations. If a control uses two or more animations at the same time, it needs to be implemented by Animationset (animation set). For example, a imageview uses the above two animations simultaneously, the code is as follows://Initializing translate animationsTranslateanimation =NewTranslateanimation (0.1f, 100.0f,0.1f,100.0f); //Initialize Alpha animationAlphaanimation =NewAlphaanimation (0.1f, 1.0f); //Animation SetAnimationset set =NewAnimationset (true);                Set.addanimation (translateanimation);                                 Set.addanimation (alphaanimation); //Set animation time (action to each animation)Set.setduration (1000);  This. Startanimation (set);2. Generate an animated Ani.xml file from an XML layout file:<set xmlns:android= "Http://schemas.android.com/apk/res/android" > <translate android:interpolator= "@ Android:anim/accelerate_interpolator "android:fromxdelta=" 0 "android:toxdelta=" 0 "android:fromydelta=" 0 "Android: Toydelta= " -380" android:duration= "> <scale android:interpolator=" @android: Anim/accelerate_interpolator "Android:fromxscale=" 1.0 "android:toxscale=" 0.2 "android:fromyscale=" 1.0 "android:toyscale=" 0.2 "android:pivotx=" 50% "android:pivoty=" 50% "android:duration=" ></scale></translate></alpha></set>the above XML file has been animated, so just use it in the activity, and the code looks like this:? 1Animation Animation= Animationutils.loadanimation ( This. GetContext (), R.anim.ani); With the above line of code, you can directly use the animation in the XML file. 

Two ways to use Android animations.

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.