Android animation TranslateAnimation

Source: Internet
Author: User

Android provides 4 Animation:
AlphaAnimation transparency animation effect
ScaleAnimation scaling animation effect
TranslateAnimation displacement animation effect
RotateAnimation rotation animation effect

This section describes the TranslateAnimation animation, which is commonly used in TranslateAnimation, such as QQ and Netease news menu bar animations,
You can use TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) to define an animation.

Parameter description:Copy codeThe Code is as follows: float fromXDelta animation start point difference from current View X coordinate
Float toXDelta animation end point difference from the current View X coordinate
The difference between the start point of the float fromYDelta animation and the Y coordinate of the current View.
Difference between the start point of the float toYDelta animation and the Y coordinate of the current View

Common Methods:Copy codeThe Code is as follows: animation. setDuration (long durationMillis); // you can specify the animation duration.
Animation. setRepeatCount (int I); // you can specify the number of repetitions.
Animation. setRepeatMode (Animation. REVERSE); // sets the REVERSE direction for execution.

Xml attributes:Copy codeThe Code is as follows: android: duration: animation running time
Android: repeatCount: defines the animation repetition time

Code:Copy codeThe Code is as follows: public class MainActivity extends Activity {
ImageView image;
Button start;
Button cancel;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Image = (ImageView) findViewById (R. id. main_img );
Start = (Button) findViewById (R. id. main_start );
Cancel = (Button) findViewById (R. id. main_cancel );
/** Set the displacement animation to 150 to the right */
Final TranslateAnimation animation = new TranslateAnimation (0, 150,0, 0 );
Animation. setDuration (2000); // sets the animation duration.
Animation. setRepeatCount (2); // you can specify the number of repetitions.
Animation. setRepeatMode (Animation. REVERSE); // sets the REVERSE direction for execution.
Start. setOnClickListener (new OnClickListener (){
Public void onClick (View arg0 ){
Image. setAnimation (animation );
/** Start animation */
Animation. startNow ();
}
});
Cancel. setOnClickListener (new OnClickListener (){
Public void onClick (View v ){
/** End animation */
Animation. cancel ();
}
});
}
}

Effect:

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.