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: