Graphic image and animation for Android Development (iii) XML implementation of animation effect _android

Source: Internet
Author: User
Tags time interval
Use XML to define tween Animation
The animation of the XML file in the project Res/anim directory, this file must contain a root element, you can make <alpha><scale> <translate> <rotate> Interpolation elements or the above elements are placed in the <set> element group, by default, so the animation instructions are simultaneous, in order to let them occur according to the sequence, you need to set a special attribute Startoffset. The animation instructions define what transformations you want to occur, and how long they should take to execute, and the conversion can be continuous or simultaneous. For example, you make text content move from left to right, then rotate 180 degrees, or rotate at the same time in the course of the move, and no conversion needs to set some special parameters (the size of the start and end size changes, the starting and ending rotation angle, and so on, you can also set some basic parameters (for example, start time and cycle), If you allow several transformations to occur at the same time, you can set them to the same start time, and if the sequence is followed by the calculation start time plus its cycle.
Tween Animation Common Node properties

property [Type] Function Note
Duration[long] property to animation duration Time is in milliseconds
Fillafter [Boolean] When set to True, the animation is applied at the end of the animation
Fillbefore[boolean] When set to True, the animation is applied before the animation starts

Interpolator

Specify an animation for the insertion There are some common inserts
Accelerate_decelerate_interpolator
Acceleration-deceleration animation insert
Accelerate_interpolator
Acceleration-Animation Insert
Decelerate_interpolator
Deceleration-Animation Insert device
Others belong to a specific animation effect
Repeatcount[int] Number of repetitions of the animation
Repeatmode[int] To define a duplicate behavior 1: Restart 2:plays Backward
Startoffset[long] The time interval between animations, starting at how much time the last animation stopped and performing the next animation
Zadjustment[int] Defines the change in the z order of an animation 0: Keep Z order unchanged
1: Keep at the top
-1: Remain at the lowest level

Table II

XML node Function description
Alpha Gradient Transparency Animation effect
<alpha
Android:fromalpha= "0.1″
Android:toalpha= "1.0″
Android:duration= "3000″/>
Fromalpha

property is the transparency at the start of the animation

0.0 means full transparency
1.0 means completely opaque.
The value above takes a number of float data types between 0.0-1.0

Duration for animation duration, Ms unit

Toalpha

property is transparency at end of animation

Table III

Scale Gradient Size Telescopic animation effect
<scale
Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"
Android:fromxscale= "0.0″
Android:toxscale= "1.4″
Android:fromyscale= "0.0″
Android:toyscale= "1.4″
android:pivotx= "50%"
Android:pivoty= "50%"
Android:fillafter= "false"
Android:startoffset= "700"
Android:duration= "700″
Android:repeatcount= "10″/>
Fromxscale[float] Fromyscale[float] The scaling dimensions on the X, y coordinates at the beginning of the animation 0.0 means contraction to no
1.0 means normal no telescopic
A value of less than 1.0 indicates a contraction
Value greater than 1.0 indicates magnification
Toxscale [float]
Toyscale[float]
Stretch dimensions on X, y coordinates at the end of the animation
Pivotx[float]
Pivoty[float]
is the starting position of the animation's x and Y coordinates relative to the object Attribute value Description: value from 0%-100%, 50% is the midpoint position on the x or y direction coordinate of the object

Table IV
Translate Moving animation effect in the position of picture transformation
<translate
Android:fromxdelta= "30″
Android:toxdelta= " -80″
Android:fromydelta= "30″
Android:toydelta= "300″
Android:duration= "2000″/>
Fromxdelta
Toxdelta
Is the animation, the position on the X coordinate at the end of the start
Fromydelta
Toydelta
Is the animation, the position on the y-coordinate at the end of the start

Table V
Rotate Picture Transfer rotation animation effect
<rotate
Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"
Android:fromdegrees= "0″
Android:todegrees= "+350″
android:pivotx= "50%"
Android:pivoty= "50%"
Android:duration= "3000″/>
Fromdegrees The angle of the object for the start of the animation Description
When the angle is negative--Indicates a counter-clockwise rotation
When the angle is positive--it means clockwise rotation
(Negative from--to positive number: clockwise rotation)
(Negative from--to negative: rotate counterclockwise)
(Positive from--to positive: clockwise rotation)
(Positive from--to negative: counterclockwise rotation)
Todegrees property is at the end of the animation the object rotates at an angle that can be greater than 360 degrees
Pivotx
Pivoty
The start bit for the X and Y coordinates of the animation relative to the object Description: The above two attribute values are taken from the 0%-100%
50% is the midpoint position on the x or y direction coordinate of the object


A complete XML definition is given below (provided by the SDK)
Copy Code code as follows:

<span style= "font-size:18px" ><set android:shareinterpolator= "false" xmlns:android= "http://" Schemas.android.com/apk/res/android ">
<scale
Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"
Android:fromxscale= "1.0"
android:toxscale= "1.4"
Android:fromyscale= "1.0"
android:toyscale= "0.6"
android:pivotx= "50%"
Android:pivoty= "50%"
Android:fillafter= "false"
Android:duration= "/>"
<set android:interpolator= "@android: Anim/decelerate_interpolator" >
<scale
android:fromxscale= "1.4"
android:toxscale= "0.0"
android:fromyscale= "0.6"
android:toyscale= "0.0"
android:pivotx= "50%"
Android:pivoty= "50%"
Android:startoffset= "700"
Android:duration= "400"
Android:fillbefore= "false"/>
<rotate
android:fromdegrees= "0"
android:todegrees= "-45"
android:toyscale= "0.0"
android:pivotx= "50%"
Android:pivoty= "50%"
Android:startoffset= "700"
Android:duration= "/>"
</set>
</set></SPAN>

Here is a screenshot of an instance of the implementation:
Pic
The XML file used is as follows:
1.alpha
Copy Code code as follows:

<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<alpha
Android:fromalpha= "1.0"
Android:toalpha= "0.0"
android:startoffset= "500"
Android:duration= "2000"
/>
</set>

2.rotate
Copy Code code as follows:

<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<rotate
Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"
android:fromdegrees= "0"
Android:todegrees= "360"
android:pivotx= "50%"
Android:pivoty= "50%"
Android:duration= "2000"
/>
</set>

3.scale
Copy Code code as follows:

<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<scale
Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"
android:fromxscale= "0.0"
Android:toxscale= "1.0"
android:fromyscale= "0.0"
Android:toyscale= "1.0"
android:pivotx= "50%"
Android:pivoty= "50%"
Android:fillafter= "false"
Android:duration= "2000"
/>
</set>

4.translate
Copy Code code as follows:

<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<translate
Android:fromxdelta= "10"
Android:toxdelta= "100"
Android:fromydelta= "10"
Android:toydelta= "100"
Android:duration= "2000"
/>
</set>

The specific implementation source code is as follows:
Copy Code code as follows:

public class Animation_xml_activity extends activity {
Private Button button1;
Private Button button2;
Private Button Button3;
Private Button Button4;
Private ImageView ImageView;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.ACTIVITY_ANIMATION__XML_);
button1= (Button) Findviewbyid (R.id.button_alpha);
Button2= (Button) Findviewbyid (r.id.button_rotate);
button3= (Button) Findviewbyid (R.id.button_scale);
button4= (Button) Findviewbyid (r.id.button_translate);
imageview= (ImageView) Findviewbyid (R.id.imageview);
Button1.setonclicklistener (New MyButton ());
Button2.setonclicklistener (New MyButton ());
Button3.setonclicklistener (New MyButton ());
Button4.setonclicklistener (New MyButton ());
}
Class MyButton implements onclicklistener{
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Switch (V.getid ()) {
Case R.id.button_alpha:
Alpha ();
Break
Case R.id.button_rotate:
Rotate ();
Break
Case R.id.button_scale:
Scale ();
Break
Case R.id.button_translate:
Translate ();
Break
Default
Break
}
}

}
public void Alpha () {
Animation animation=animationutils.loadanimation (Animation_xml_activity.this, R.anim.alpha);
Imageview.startanimation (animation);
}
public void Rotate () {
Animation animation=animationutils.loadanimation (Animation_xml_activity.this, r.anim.rotate);
Imageview.startanimation (animation);
}
public void Scale () {
Animation animation=animationutils.loadanimation (Animation_xml_activity.this, R.anim.scale);
Imageview.startanimation (animation);
}
public void Translate () {
Animation animation=animationutils.loadanimation (Animation_xml_activity.this, r.anim.translate);
Imageview.startanimation (animation);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.activity_animation__xml_, menu);
return true;
}
}
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.