Android view animation-view animation

Source: Internet
Author: User

This article translated from: http://developer.android.com/guide/topics/graphics/view-animation.html

You can use the view animation system to execute the animation on the view object. A complementary animation is an animation that is calculated using starting point, ending point, size, rotation, and other animation features.

The compensation animation can execute a simple conversion series (Position, size, rotation, and transparency) on the content of the view object ). Therefore, if you have a textview object, you can move, rotate, zoom in, or zoom out the text. If the textview object has a background image, the background image will be changed along with the text. The animation package provides all the classes used in the compensation animation.

The animation instruction sequence defines the supplementary animation. These commands can be defined either in XML or in Android code. Like layout definition, we recommend that you use XML to define an animation because it is more readable, reusable, and pluggable than an animation to be encoded. In the following example, we use XML. (To learn more about defining animations in application code, read the animationset class and other animation subclasses .)

Animation commands define the animation transformations you want, as well as the timing of the animation and the playback duration of the animation. Animation transformations can be sequential or concurrent. For example, there is a textview object content that is moved from left to right, then rotated 180 degrees, or simultaneously rotated when the text is moved. Each transformation requires a set of parameters to specify the desired transformation (for the starting and ending dimensions of the dimensional transformation, for the starting and ending dimensions of the rotation, and for the ending angle ), and a group of common parameters (for example, start time and duration ). If several transformations occur at the same time, you need to set the same start time for them; if you want them to play in sequence, the Start Time and the length of the previous animation transformation are used to calculate the start time of the next animation.

The animation XML file must be defined in the Res/anim/directory of your android project. This file must have a separate root element: This element can be a separate <alpha>, <scal>, <translate>, <rotate> interpolation element, it can also be a <set> element that combines these elements (including <set> elements. By default, all animation commands are concurrent. To make them happen in sequence, you must specify the startoffset attribute as shown in the following example.

The following XML is from a view object in apidemo that is used for stretching and rotating at the same time:

<Set Android: Using interpolator = "false">

<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: Required Tx = "50%"

Android: 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: Required 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: Required Tx = "50%"

Android: Ty = "50%"

Android: startoffset = "700"

Android: Duration = "400"/>

</Set>

</Set>

The coordinates of the screen in the upper left corner (not used in the preceding example) are (0, 0) and gradually increase to the lower right corner.

Some values, such as ipvtx, can be specified relative to the object itself or its parent container. The expected value must be in the correct format (50 indicates 50% relative to the upper left corner of its parent container, and 50% indicates 50% relative to its upper left corner ).

By assigning an interpolator object, you can decide how to transform an animation over time. Android includes several interpolator subclasses that can specify the curves of various speeds. For example, accelerateinterpolator tells the system to start slowly and then gradually accelerate the transformation. Each transformation has a property value applied to XML.

Hyperspace_jump.xml file saved in the Res/anim/directory of the project. The following code references this file and applies it to an imageview object from the layout.

Imageview spaceshipimage = (imageview) findviewbyid (R. Id. spaceshipimage );

Animation hyperspacejumpanimation = animationutils. loadanimation (this, R. anim. hyperspace_jump );

Spaceshipimage. startanimation (hyperspacejumpanimation );

As an alternative to the startanimation () method, you can use animation. setstarttime () method to define the animation start time, and then use view. the setanimation () method assigns the animation object to the view object.

For more information about XML syntax, available labels, and attributes, see animation resources (http://developer.android.com/guide/topics/resources/animation-resource.html)

 

Note: No matter how your animation moves or adjusts the size, the boundaries of view objects with the animation will not be automatically adjusted to adapt to changes, even if the animation exceeds the view object's boundary, it will not be cropped, but if the animation exceeds its parent container's boundary, it will be cropped.

 

 

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.