Android Development Animations Animation usage examples detailed _android

Source: Internet
Author: User
Tags set time time in milliseconds

This example describes the animations animation usage of Android development. Share to everyone for your reference, specific as follows:

One, animation type

The Android animation consists of four types:alpha, scale, translate, rotate

In an XML configuration file

Alpha
Gradient Transparency Animation effect
Scale
Gradient Size Telescopic animation effect
Translate
Moving animation effect in the position of picture transformation
Rotate
Picture Transfer rotation animation effect

In Java code

Alphaanimation
Gradient Transparency Animation effect
Scaleanimation
Gradient Size Telescopic animation effect
Translateanimation
Moving animation effect in the position of picture transformation
Rotateanimation
Picture Transfer rotation animation effect

Second, Android animation mode

There are two main animation modes of animation:tweened and Frame

One is tweened animation (gradient animation)

in XML
Javacode
Alpha
Alphaanimation
Scale
Scaleanimation

One is the frame by frame (Picture transformation animation)

in XML
Javacode
Translate
Translateanimation
Rotate
Rotateanimation

definition of animation in XML file

① open Eclipse, new Android Project
② New Anim folder in res directory
③ a new myanim.xml in the Anim directory (note file name lowercase)
④ Animation code to add XML

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
 <alpha/>
 <scale/>
 <translate/>
 <rotate/>
</ Set>

Iv. Analysis of Android XML animation

1. Alpha

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<alpha
android:fromalpha=" 0.1 "
android:toalpha=" 1.0 "
android:duration=" "3000"
/>
<!--transparency Control animation effect Alpha
    floating-point value:
      Fromalpha property is the transparency at the beginning of the animation
      Toalpha  property for the animation at the end of the opacity
      description:
        0.0 means full transparency
        1.0 means total opacity
      The numeric long value of the float data type between 0.0-1.0 is the above value
    : The
      Duration property is the duration description of the animation
      :
        time in milliseconds
-->
</set>

2. Scale

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "Http://schemas.android.com/apk/res/android" > <scale android:interpolator= "@android: Anim/accelerate_decelerate_interpolator" Android:fro mxscale= "0.0" android:toxscale= "1.4" android:fromyscale= "0.0" android:toyscale= "1.4" android:pivotx= "5 0% "android:pivoty=" 50% "android:fillafter= false" android:duration= "M"/> </set> <!--size telescopic animation Effect Scale Property: interpolator Specifies an animated insert in my experiment, I found three kinds of animated inserts when using resources in Android.res.anim: accelerate_decelerate_i Nterpolator acceleration-deceleration animation Insert accelerate_interpolator acceleration-Animation insert Decelerate_interpolator deceleration-animation insert others belong to a specific
      Animation effect Floating-point value: Fromxscale property is the Flex dimension on the X coordinate at the beginning of the animation 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 
          The Toyscale property is a description of the telescopic dimensions on the y-coordinate at the end of the animation: the above four property value 0.0 means that the contraction to no 1.0 indicates that the normal telescopic value is less than 1.0 for contraction Value greater than 1.0 indicates magnification pivotx   property is the start position of the animation's x-coordinate relative to the object Pivoty the start position of the animation relative to the object's y-coordinates: The above two property values are taken from the 0%-100% value 50% as the x or y direction coordinate of the object 

 The midpoint position on the Long value: The Duration property is the animation duration description: Time in milliseconds Boolean value: Fillafter property When set to True, the animation is applied after the end of the animation-->

3. Translate

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<translate
android:fromxdelta="
android:toxdelta= " -80"
android:fromydelta= " "
android:toydelta="
android:duration= "
/>
<!--translate position transfer animation effect
    integer value: The Fromxdelta property is the position on the X coordinate at the beginning 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 at the start of the animation The Toydelta property is the  position on the y-coordinate at the end of the animation
      Note:
           fromxtype toxtype fromytype Toytype is not specified when the
           default is relative
    to its own reference Long value:
      Duration property for animation duration
      Description:  time in milliseconds
-->
</set>

4. Rotate

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "Http://schemas.android.com/apk/res/android"
    > <rotate android:interpolator= "@android: Anim/accelerate_decelerate_interpolator" android:fromdegrees= "0" Android:todegrees= "+350" android:pivotx= "50%" android:pivoty= "50%" android:duration= "3000"/> <!--rot Ate rotation animation effect properties: interpolator Specifies an animated insert in my experiment, I found three kinds of animated inserts when I use the resources in Android.res.anim: Accelerate_de        Celerate_interpolator acceleration-deceleration animation Insert accelerate_interpolator acceleration-Animation Insert Decelerate_interpolator Deceleration-Animation inserts other specific animation effect floating point value: Fromdegrees property is the angle of the object at the beginning of the animation Todegrees property to the angle of the object rotation at the end of the animation can be greater than 360 degrees.
           Ming: When the angle is negative--indicates counterclockwise rotation when the angle is positive--clockwise rotation (negative from--to positive: clockwise rotation) (negative from--to negative number: counterclockwise rotation)
           (Positive from--to positive: clockwise rotation) (Positive from--to negative: counterclockwise rotation) The Pivotx property is the beginning position of the animation relative to the x-coordinate of the object Pivoty property is the starting position of the animation's y-coordinate relative to the object description: The above two property values are from 0%-100% 50% is the value of the midpoint position on the x or y direction coordinate of the object: The Duration property is the animation duration description: Time in milliseconds--> </set>

Animate effects in XML

public static Animation Loadanimation the context, int ID,
////////////The
second parameter ID is a reference to an animated XML file
//Example:
myanimation= animationutils.loadanimation (this, r.anim.my_action);
To load an animated XML file in XML using the static method Loadanimation () of the Animationutils class

Five, define animation in Java code

Define an animation instance object in code
private Animation Myanimation_alpha;
Private Animation Myanimation_scale;
Private Animation myanimation_translate;
Private Animation myanimation_rotate;
Initializes an instance object
Myanimation_alpha = new Alphaanimation (0.1f, 1.0f) according to the respective construction method;
Myanimation_scale = new Scaleanimation (0.0f, 1.4f, 0.0f, 1.4f,
       animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);
Myanimation_translate = new Translateanimation (30.0f, -80.0f, 30.0f, 300.0f);
Myanimation_rotate = new Rotateanimation (0.0f, +350.0f,
        animation.relative_to_self,0.5f,animation.relative_to _self, 0.5f);

Six, Android code animation analysis

1. Alphaanimation

Alphaanimation class object Definition

1. Private Alphaanimation Myanimation_alpha;

Alphaanimation Class object Construction

Alphaanimation (float fromalpha, float toalpha)
//First parameter Fromalpha transparency
//second parameter Toalpha for animation at the end
of the animation Myanimation_alpha = new Alphaanimation (0.1f, 1.0f);
Description:
//        0.0 for full transparency
//        1.0 means completely opaque

Set Animation duration

Myanimation_alpha.setduration (5000);
Set time duration to 5000 milliseconds

2. Scaleanimation

Scaleanimation class object Definition

Private Scaleanimation Myanimation_scale;

Scaleanimation Class object Construction

Scaleanimation (float FromX, float toX, float fromY, float toY,
      int pivotxtype, float pivotxvalue, int pivotytype, Floa T pivotyvalue)
///The first parameter FROMX the expansion dimension on the X coordinate at the beginning of the animation//the
second parameter tox the telescopic dimension on the X coordinate at the end of the animation
//The third parameter fromy the scaling dimension on the y-coordinate at the start of the animation
//Fourth parameter toy for the animation at the end of the y-coordinate scaling dimension/
* Description: The
  above four property value
  0.0 means that the contraction to no
  1.0 indicates that the normal telescopic
  value is less than 1.0 for contraction
  The value is greater than 1.0 to enlarge///
fifth parameter Pivotxtype for the animation in the X axis relative to the object position type
//sixth parameter pivotxvalue is the animation relative to the object's x-coordinate the start position
/ The seventh parameter Pivotxtype is the beginning position of the animation in the Y axis relative to the object position type
//eighth parameter pivotyvalue to the Y coordinate of the animation relative to the object
Myanimation_scale = new Scaleanimation (0.0f, 1.4f, 0.0f, 1.4f,
       animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);

Set Animation duration

Myanimation_scale.setduration (m);
Set time duration to 700 milliseconds

3. Translateanimation

Ranslateanimation class object Definition

Private Translateanimation myanimation_translate;

Translateanimation Class object Construction

Translateanimation (float Fromxdelta, float toxdelta,
            float fromydelta, float toydelta)
/ The first parameter fromxdelta to the move position on the X coordinate at the beginning of the animation//the
second parameter toxdelta to the move position on the X coordinate at the end of the animation/
/The third argument fromydelta the move position on the y-coordinate at the start of the animation
The fourth argument toydelta the move position on the y-coordinate at the end of the animation

Set Animation duration

Myanimation_translate = new Translateanimation (10f, 100f, 10f, 100f);
Myanimation_translate.setduration ();
Set time duration to 2000 milliseconds

4. Rotateanimation

The Rotateanimation class object defines
private rotateanimation myanimation_rotate;
Rotateanimation class objects are constructed
rotateanimation (float fromdegrees, float todegrees,
      int pivotxtype, float pivotxvalue, int Pivotytype, float pivotyvalue
///First argument fromdegrees the angle of rotation at the beginning of the animation
//The second parameter todegrees the angle to which the animation is rotated
// The third parameter pivotxtype is the animation at the X axis relative to the object position type
//fourth parameter pivotxvalue the start position//fifth parameter of the animation relative to the object's X-coordinate
Pivotxtype for animation on the y-axis relative to the object position type
//The sixth parameter pivotyvalue is the starting position of the animation relative to the y-coordinate of the object
myanimation_rotate = new Rotateanimation (0.0f, +350.0f,
        Animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);

Set Animation duration

Myanimation_rotate.setduration (3000);
Set time duration to 3000 milliseconds

How to use animation effects in Java code

Use a method inherited from the View parent class Startanimation () to add an animation effect to view or subclass view, and so on

public void Startanimation (Animation Animation)
view.startanimation (myanimation_alpha);
View.startanimation (Myanimation_scale);
View.startanimation (myanimation_translate);
View.startanimation (myanimation_rotate);

More interested readers of Android-related content can view this site: "Android Development Animation Tips", "Android Development introduction and Advanced Course" and "Android Control usage summary."

I hope this article will help you with the Android program.

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.