Android development-graphic images and animations-XML Implementation of Animation Effects

Source: Internet
Author: User

XML is used to define the XML file of the Tween Animation in the res/anim directory of the project. This file must contain a root element, you can set <alpha> <scale> <translate> <rotate> interpolation element or add all the above elements to the <set> element group. By default, therefore, all animation commands occur at the same time. To enable them to occur in sequence, you need to set a special attribute startOffset. Animation commands define what kind of conversions you want to perform. when they happen, how long should they be executed? The conversions can be continuous or at the same time. For example, if you want to move the text content from the left to the right, then rotate the text 180 degrees, or rotate the text content simultaneously during the movement, no conversion requires some special parameters (the size of the start and end sizes, the rotation angle of the start and end, and so on). You can also set some basic parameters (for example, if several conversions occur simultaneously, you can set the same Start Time for them. If the conversion is based on the sequence, the calculation Start Time plus the cycle. Tween Animation common node attribute [type] function remarks Duration [long] attribute is the Animation Duration in milliseconds fillAfter [boolean] When set to true, after the animation ends, fillBefore [boolean] is applied. If this parameter is set to true, this animation is converted to an animation that is specified by interpolator before the animation starts. There are some common plug-ins: accelerate_decelerate_interpolator acceleration-Deceleration animation plug-in accelerate_interpolator acceleration-animation plug-in decelerate_interpolator repeatCount [int] RepeatMode [int] specifies the number of RepeatMode animation RepeatMode: start again 2: plays backwardstartOffset [long] The interval between the animations. The next animation zAdjustment [int] defines the change of the Z Order of the animation from the last animation stop time to 0: keep Z Order unchanged 1: Keep at the top-1: keep at the bottom of Table 2 XML node function description alpha gradient transparency animation effect <alphaandroid: fromAlpha = "0.1" android: toAlpha = "1.0" android: duration = "3000"/> fromAlpha attribute is the animation start time. Transparency 0.0 indicates completely transparent. 1.0 indicates completely transparent. The value above takes float data between 0.0 and 1.0 as the animation duration., the toAlpha attribute of the ms unit is the transparency of the animation end. Table 3 scale gradient scaling animation effect <scaleandroid: interpolator = "@ android: anim/accelerate_decelerate_interpolator" android: fromXScale = "0.0" android: toXScale = "1.4" android: fromYScale = "0.0" android: toYScale = "1.4" android: Large Tx = "50%" android: Large ty = "50%" android: fillAfter = "false" android: startOffset = "700" android: duration = "700" android: repeatCount = "10"/> fromXScale [float] fromYScale [float] indicates the start time of an animation, in X and Y coordinates, the scaling size 0.0 indicates that the scale is reduced to no 1.0, indicating that the normal scale-up value is less than 1.0, indicating that the scale-up value is greater than 1.0, indicating that the toXScale [float] toYScale [float] indicates that the animation ends., the scaling dimension of X and Y coordinates, TX [float] float ty [float], is the attribute value of the starting position of the animation relative to the X and Y coordinates of the object. The value ranges from 0% to 100%, 50% indicates the point position of the X or Y coordinate of the object. Table 4. Position movement of the translate image <translateandroid: fromXDelta = "30" android: toXDelta = "-80" android: fromYDelta = "30" android: toYDelta = "300" android: duration = "2000"/> fromXDeltatoXDelta is the animation. fromYDeltatoYDelta is the position on X coordinate at the end of the animation. The position on Y coordinate at the end of the animation. rotateandroid: interpolator = "@ android: anim/accelerate_decelerate_interpolator" android: fromDegrees = "0" android: toDegrees = "+ 350" android: Export Tx = "50%" android: required ty = "50%" android: duration = "3000"/> fromDegrees indicates the angle of the object when the animation starts. It indicates that the angle is negative. It indicates that the rotation is clockwise and the angle is positive. It indicates that the rotation is clockwise (negative from -- to positive: clockwise rotation) (negative from -- to negative: clockwise rotation) (positive from -- to positive: clockwise rotation) (positive from -- to negative: clockwise rotation) the toDegrees attribute indicates that when the animation ends, the object rotation angle can be greater than 360 degrees. The starting position of the animation relative to the X and Y coordinates of the object is described as follows: the preceding two attribute values are defined in XML format from 0% to 100% as the midpoint of the X or Y coordinate of the object) [html] view plaincopy <span style = "font-size: 18px;"> <set android: Using interpolator = "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: export Tx = "50%" android: Copyright ty = "50%" android: fillAfter = "false" android: duration = "700"/> <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: export Tx = "50%" android: Ty = "50%" android: startOffset = "700" android: duration = "400" android: fillBefore = "false"/> <rotate android: fromDegrees = "0" android: toDegrees = "-45" android: toYScale = "0.0" android: Export Tx = "50%" android: Export ty = "50%" android: startOffset = "700" android: duration = "400"/> </set> </span>: the xml file used is as follows: 1. alpha [html] view plaincopy <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 [html] view plaincopy <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: durtx = "50%" android: Durty = "50%" android: duration = "2000"/> </set> 3. scale [html] view plaincopy <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: Large Tx = "50%" android: Large ty = "50%" android: fillAfter = "false" android: duration = "2000"/> </set> 4. translate [html] view plaincopy <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 source code is as follows: [java] view plaincopypublic class 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); www.2cto.com} 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 ;}} shared:

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.