Android animation TranslateAnimation application detail _android_script home

Android animation TranslateAnimation application details

Updated: December 02, 2012 16:15:20 Author:
This section explains TranslateAnimation, which is commonly used. For example, the animation of the menu bar of QQ and NetEase news can be realized with TranslateAnimation. This article will introduce in detail the definition of animation through TranslateAnimation, the need of friends can refer to the next
android provides 4 animations:
AlphaAnimation Transparency Animation effect
ScaleAnimation Scales the animation effect
TranslateAnimation Displacement animation effect
RotateAnimation Rotation animation effect

This section explains TranslateAnimation, which is commonly used. For example, the animation of the menu bar of QQ and NetEase news can be realized with TranslateAnimation.
Animation is defined by TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

Parameter description:
Copy codeThe code is as follows:

float fromXDelta The difference between the point at which the animation starts and the X coordinate of the current View
The difference between the end point of the float toXDelta animation and the current View X coordinate
The difference between the point at which the float fromYDelta animation starts and the current View Y coordinate
The difference between the point at which the float toYDelta animation starts and the current View Y coordinate

Common methods:
Copy codeThe code is as follows:

animation.setDuration(long durationMillis); // Set the animation duration
animation.setRepeatCount(int i); // Set the number of repetitions
animation.setRepeatMode(Animation.REVERSE); // Set the execution in the reverse direction

Xml attributes:
Copy codeThe code is as follows:

android:duration: indicates the duration of the animation
android:repeatCount: defines the time for the animation to repeat

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 shift to the right 150 */
final TranslateAnimation animation = new TranslateAnimation(0, 150,0, 0); Final TranslateAnimation Animation = New translateAnimation (0, 150,0);
animation.setDuration(2000); // Set the animation duration
animation.setRepeatCount(2); // Set the number of repetitions
animation.setRepeatMode(Animation.REVERSE); // Set the execution in the reverse direction
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

Latest comments