用自定义继承Animation写一个跑圈运动
2015-10-27 23:18
351 查看
写一个继承Animation的类,我们定义为MyAnimation就行
MyAnimation:
public class MyAnimation extends Animation {
private int totalScrollX, totalScrollY; // 偏移量
public MyAnimation(int totalScrollX, int totalScrollY) {
this.totalScrollX = totalScrollX;
this.totalScrollY = totalScrollY;
}
//重写applyTransformation方法
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
animationCircle(t, interpolatedTime);
}
public void animationCircle(Transformation t, float interpolatedTime) {
float currentScrollX = 0;
float currentScrollY = 0;
if (totalScrollX > 0) {
currentScrollX = totalScrollX * interpolatedTime;
} else {
currentScrollX = Math.abs(totalScrollX) * (1-interpolatedTime);
}
if (totalScrollY > 0) {
currentScrollY = (float) Math.sqrt(Math.pow(totalScrollY, 2)
- Math.pow(currentScrollX - Math.abs(totalScrollY), 2));
} else {
currentScrollY = -(float) Math.sqrt(Math.pow(totalScrollY, 2)
- Math.pow(currentScrollX - Math.abs(totalScrollY), 2));
}
t.getMatrix().setTranslate(currentScrollX, currentScrollY);
if (listener != null && interpolatedTime == 1.0) {
if (isFinish) {
return;
}
isFinish = true;
listener.onAnimationAfter();
}
}
private OnAnimationAfterListener listener;
private boolean isFinish = false;
public void setOnAnimationAfterListener(OnAnimationAfterListener listener) {
this.listener = listener;
}
public interface OnAnimationAfterListener {
public void onAnimationAfter();
}
}
MainActivity:
public class MainActivity extends Activity {
private ImageView iv_circle;
private int totalX;
private int totalY;
private MyAnimation animation;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_circle = (ImageView) findViewById(R.id.iv_circle);
}
public void startAnimation(View view) {
totalX = 200;
totalY = 100;
animation = new MyAnimation(totalX, totalY);
animation.setDuration(2000);
animation.setFillAfter(true);
animation.setOnAnimationAfterListener(listener);
iv_circle.startAnimation(animation);
}
private OnAnimationAfterListener listener = new OnAnimationAfterListener() {
public void onAnimationAfter() {
if (animation != null) {
animation.cancel();
animation = null;
}
animation = new MyAnimation(-totalX, -totalY);
animation.setDuration(2000);
animation.setFillAfter(true);
animation.setOnAnimationAfterListener(listener);
iv_circle.startAnimation(animation);
totalX = -totalX;
totalY = -totalY;
}
};
}
MyAnimation:
public class MyAnimation extends Animation {
private int totalScrollX, totalScrollY; // 偏移量
public MyAnimation(int totalScrollX, int totalScrollY) {
this.totalScrollX = totalScrollX;
this.totalScrollY = totalScrollY;
}
//重写applyTransformation方法
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
animationCircle(t, interpolatedTime);
}
public void animationCircle(Transformation t, float interpolatedTime) {
float currentScrollX = 0;
float currentScrollY = 0;
if (totalScrollX > 0) {
currentScrollX = totalScrollX * interpolatedTime;
} else {
currentScrollX = Math.abs(totalScrollX) * (1-interpolatedTime);
}
if (totalScrollY > 0) {
currentScrollY = (float) Math.sqrt(Math.pow(totalScrollY, 2)
- Math.pow(currentScrollX - Math.abs(totalScrollY), 2));
} else {
currentScrollY = -(float) Math.sqrt(Math.pow(totalScrollY, 2)
- Math.pow(currentScrollX - Math.abs(totalScrollY), 2));
}
t.getMatrix().setTranslate(currentScrollX, currentScrollY);
if (listener != null && interpolatedTime == 1.0) {
if (isFinish) {
return;
}
isFinish = true;
listener.onAnimationAfter();
}
}
private OnAnimationAfterListener listener;
private boolean isFinish = false;
public void setOnAnimationAfterListener(OnAnimationAfterListener listener) {
this.listener = listener;
}
public interface OnAnimationAfterListener {
public void onAnimationAfter();
}
}
MainActivity:
public class MainActivity extends Activity {
private ImageView iv_circle;
private int totalX;
private int totalY;
private MyAnimation animation;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_circle = (ImageView) findViewById(R.id.iv_circle);
}
public void startAnimation(View view) {
totalX = 200;
totalY = 100;
animation = new MyAnimation(totalX, totalY);
animation.setDuration(2000);
animation.setFillAfter(true);
animation.setOnAnimationAfterListener(listener);
iv_circle.startAnimation(animation);
}
private OnAnimationAfterListener listener = new OnAnimationAfterListener() {
public void onAnimationAfter() {
if (animation != null) {
animation.cancel();
animation = null;
}
animation = new MyAnimation(-totalX, -totalY);
animation.setDuration(2000);
animation.setFillAfter(true);
animation.setOnAnimationAfterListener(listener);
iv_circle.startAnimation(animation);
totalX = -totalX;
totalY = -totalY;
}
};
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories