Android animated--view Animation

Source: Internet
Author: User

I. Overview

View animations have four effects, such as panning, rotating, zooming, and transparency, while frame animations are also part of the view animation, but the implementation is different from the previous several.

Second, the type of view animation

The four animations correspond to subclass Translateanimation, Rotateanimation, Scaleanimation, and alphaanimation of four animation, and also use XML to define animations, respectively, corresponding to the label <translate> <rotate> <scale> <alpha> in real-world applications, XML-based, because the structure of this approach is clearer.

The following is a brief introduction to XML definition animations. Add an animated source file to the Anim folder under Res, where the outermost is a set representation is an animated collection that allows you to define multiple animation effects within it. The common properties of set are:

1, interpolator interpolation, affect the speed of the animation, that is, when playing the animation speed changes, such as the initial slow gradually accelerated or the initial rapid gradual deceleration and other effects.

A few interpolator are defined in the animations framework

1.AccelerateDecelerateInterpolator: At the beginning and end of the animation where the rate change is slow, in the middle of the time speed.

2.AccelerateInterpolator: At the beginning of the animation, the rate change is slow, and then start to accelerate
3.CycleInterpolator: Animation loop plays a specific number of times, rate changes along the sine curve

4.DecelerateInterpolator: At the beginning of the animation, the rate change is slow, and then start slowing down
5.LinearInterpolator: Animations change at a uniform rate

2. Shareinterpolator whether the interpolation is shared, that is, whether the animation within the animation collection shares a set-defined interpolator.

3. Zadjustment set the position of the animation runtime on the z axis (top/bottom/normal)

4, Fillafter Animation after the end of the animated effect

5. Whether to restore the initial state after the Fillbefore animation ends

The common properties of the four animations are

1, translate translation animation

1, Fromxdelta, Toxdelta set the initial value of the x-coordinate and the end value

2. Fromydelta and Toydelta

2. Scale Zoom Animation

1, Fromxscale Toxscale horizontal direction scaling the beginning and ending value

2. Fromyscale Toyscale

3, pivotx pivoty Zoom Axis Point coordinates

3. Rotate Rotation animation

1, fromdegrees todegrees rotation angle of the whole value

2, Pivotx, pivoty rotation axis point coordinates

The pivot point default value is the upper-left corner.

4. Alpha Transparency Animation

1, Fromalpha toalpha transparency of the whole value

The common properties of the four types of animations and sets are: Duration animation duration and Fillafter and Fillbefore. The animation itself can also specify its own interpolator.


Using animations is very simple, after you define an XML file, you can load the animation for the view with the following code

Animation Animation = animationutils.loadanimation (this, r.anim.animation_test);        Testimage.startanimation (animation);

Where startanimation represents the immediate execution of the animation, and the corresponding setanimation, which indicates that the view will be executed by the animation, you can set when to execute.

In addition to XML, it is also easy to animate through code.

New Alphaanimation (0,1);          Alphaanimation.setduration (+);          Testimage.startanimation (alphaanimation);

The above is the transparency animation, the other animation effect method is similar.


In addition to the above use can also be set to monitor the animation, the code is as follows

        Animation.setanimationlistener (new  Animation.animationlistener () {            @Override              publicvoid  Onanimationstart (Animation Animation) {            }            @Override            public  void  onanimationend (Animation Animation) {                }            @Override            public  void  onanimationrepeat (Animation Animation) {            }        });

Three listening methods correspond to three states respectively.

Iii. Custom View Animations

Custom view animations primarily inherit animation abstract classes, Initialize and Applytransformation methods, where initialize methods perform initialization work, The Applytransformation method makes the corresponding matrix changes. Custom view animations are rarely used in real-world development.

Four, frame animation

Frame animations implement a slideshow-like effect and play a preset set of pictures. The class used is animationdrawable, the corresponding label is Animation-list, the child label item is specified by drawable to play the picture, duration specifies the time the picture is displayed.


<?XML version= "1.0" encoding= "Utf-8"?><animation-listxmlns:android= "Http://"Android:oneshot= "false">    <Itemandroid:drawable= "@drawable/ic_add"android:duration= "+" />    <Itemandroid:drawable= "@drawable/ic_manage"android:duration= "+" />    <Itemandroid:drawable= "@drawable/ic_setting"android:duration= "+" />    <Itemandroid:drawable= "@drawable/ic_switch"android:duration= "+" /></animation-list>

The above drawable is used as the background of the view and played by the drawable start method, and the default is loop play. Frame animations have caused oom (memory overflow) and should avoid using too many large-sized pictures.

      TextView TextView = (TextView) Findviewbyid (;                 = (animationdrawable) textview.getbackground ();                Drawable.start ();

V. Special usage scenarios for view animations

1, Layoutanimation

Acts on ViewGroup, adding an animated effect to the appearance of each child element in the ViewGroup.

The main use steps are:

1. Define the corresponding XML file, as follows

<? XML version= "1.0" encoding= "Utf-8" ?> <  xmlns:android= ""    Android:delay  = "0.5"    android:animationorder= "normal"    android:animation  = "@anim/animation_item"/>

Delay is the animation lag time, AnimationOrder specifies the animation play order (Normal/random/reverse), animation specifies the specific animation.

2, define the specific animation effect

3. Specify layoutanimation for ViewGroup, by Layoutanimation property.

In addition to the above methods can also be implemented through code, mainly through the Layoutanimationcontroller.

   New Layoutanimationcontroller (animation);        Controller.setorder (layoutanimationcontroller.order_normal);        Listview.setlayoutanimation (Controller);

2. Activity switching effect

It is mainly implemented by overridependingtransition (int enteranim,int Exitanim) method.

Where Enteranim refers to an animated resource when activity is opened Id,exitanim the animation resource ID when activity is paused.

As long as the method is called in startactivity or the copy finish method to achieve a custom switching effect, note that the method must be placed behind the preceding method.

Similarly, for fragments (Fragment) You can also add toggle animations, and for compatibility, use the SUPPORT-V4 Compatibility Pack to add toggle animations through Fragmenttransaction setcustomanimations ().


Android animated--view Animation

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: 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.