Android ApiDemos example (96): Views-& gt; Animation-& gt; Int

Source: Internet
Author: User

An animation is usually defined by defining a key frame (first or last frame) and then the system automatically generates an intermediate frame. The process of generating an intermediate frame is called "interpolation interpolate ". Android Animation supports multiple interpolation algorithms: Interpolators (which can be translated into interpolation devices ).

All Interpolators implement the Interpolator interface (actually the TimeInterpolator Interface), which defines a method:

Public abstract float getInterpolation (float input)

The input is the time of the normalized animation. The value ranges from 0 to 1. 0 indicates the start time, and 1 indicates the end time.
The return value is also a normalized value, which can represent the translation time, rotation angle, Alpha value change. The standard value range is between 0 and 1, but the allowed value is smaller than 0 or greater than 1, it indicates interpolation to both sides of the standard area.
In general, Interpolator defines the rate of animation changes, and provides different function-defined change rules relative to time. It can define a variety of non-linear transformation functions, such as acceleration and deceleration.

The Android system comes with a variety of Interpolator which can be changed through a variety of animations:

AccelerateDecelerateInterpolator accelerates and then slows down
AccelerateInterpolator Acceleration
AnticipateInterpolator takes a small step and then moves forward quickly
AnticipateOvershootInterpolator first takes a small step back and then moves forward quickly.
BounceInterpolator
CycleInterpolator Periodic Motion
DecelerateInterpolator deceleration
LinearInterpolator constant speed
OvershootInterpolator quickly advances to the right world, and then highlights a small step.
These Interpolator can be applied to any Animation, such as TranslateAnimation, RotateAnimation, ScaleAnimation, and AlphaAnimation. The interpolation objects vary with the types of Animation. For example, for TranslateAnimation, the interpolation object is displacement, and the corresponding animation is the translation speed. For RotateAnimation, the interpolation object is the rotation angle, and the corresponding animation is the rotation rate.

This example describes how to use multiple Interpolator to add an animation to a TextView.

<TextView
Android: id = "@ + id/target"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textSize = "26sp"
Android: text = "@ string/animation_3_text"/>

The example class corresponding to this example is Animation3, which uses the following Interpolator:

[Java]
Private static final String [] INTERPOLATORS = {
"Accelerate", "Decelerate", "Accelerate/Decelerate ",
"Anticipate", "Overshoot", "Anticipate/Overshoot ",
"Bounce"
};

Private static final String [] INTERPOLATORS = {
"Accelerate", "Decelerate", "Accelerate/Decelerate ",
"Anticipate", "Overshoot", "Anticipate/Overshoot ",
"Bounce"
}; The Animation type used is TranslateAnimation. Move TextView:


[Java]
Final View target = findViewById(R.id.tar get );
Final View targetParent = (View) target. getParent ();

Animation a = new TranslateAnimation (0.0f,
TargetParent. getWidth ()-target. getWidth ()
-TargetParent. getPaddingLeft ()-
TargetParent. getPaddingRight (), 0.0f, 0.0f );
A. setDuration (1000 );
A. setStartOffset (300 );
A. setRepeatMode (Animation. RESTART );
A. setRepeatCount (Animation. INFINITE );

...

A. setInterpolator (AnimationUtils. loadInterpolator (this,
Android. R. anim. accelerate_decelerate_interpolator ));
...

Target. startAnimation ();

Final View target = findViewById(R.id.tar get );
Final View targetParent = (View) target. getParent ();
 
Animation a = new TranslateAnimation (0.0f,
TargetParent. getWidth ()-target. getWidth ()
-TargetParent. getPaddingLeft ()-
TargetParent. getPaddingRight (), 0.0f, 0.0f );
A. setDuration (1000 );
A. setStartOffset (300 );
A. setRepeatMode (Animation. RESTART );
A. setRepeatCount (Animation. INFINITE );
 
...
 
A. setInterpolator (AnimationUtils. loadInterpolator (this,
Android. R. anim. accelerate_decelerate_interpolator ));
...
 
Target. startAnimation ();
 

 


You can also customize Interpolator as long as you implement the Interpolator interface.

Author: mapdigit
 


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.