一、通过Java实现动画:
package com.jikexueyuan.translateanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
public class MainActivity extends Activity {
private TranslateAnimation ta;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* fromXDelta Change in X coordinate to apply at the start of the animation
toXDelta Change in X coordinate to apply at the end of the animation
fromYDelta Change in Y coordinate to apply at the start of the animation
toYDelta Change in Y coordinate to apply at the end of the animation
*/
ta = new TranslateAnimation(
0,//fromXDelta
200, //toXDelta
0, //fromYDelta
200);//toYDelta
ta.setDuration(1000);
findViewById(R.id.btnTranslateMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
arg0.startAnimation(ta);
}
});
}
}
二、通过ta.xml实现动画:
package com.jikexueyuan.translateanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
public class MainActivity extends Activity {
private TranslateAnimation ta;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* fromXDelta Change in X coordinate to apply at the start of the animation
toXDelta Change in X coordinate to apply at the end of the animation
fromYDelta Change in Y coordinate to apply at the start of the animation
toYDelta Change in Y coordinate to apply at the end of the animation
ta = new TranslateAnimation(
0,//fromXDelta
200, //toXDelta
0, //fromYDelta
200);//toYDelta
ta.setDuration(1000);
*/
findViewById(R.id.btnTranslateMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
arg0.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.ta));
}
});
}
}
ta.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="200"
android:fromYDelta="0"
android:toYDelta="200"
android:duration="1000" >
</translate>