自定义Animation动画,完成跑圈动作

本文介绍了一个自定义动画的实现过程,通过继承Animation类并重写applyTransformation方法来创建正弦和圆形路径动画效果。此外,还实现了动画完成后自动反转动画的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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; // 偏移量

/**
* @param totalScrollX
*            偏移量X
* @param totalScrollY
*            偏移量Y
*/
public MyAnimation(int totalScrollX, int totalScrollY) {
this.totalScrollX = totalScrollX;
this.totalScrollY = totalScrollY;
}

//从写applyTransformation方法
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
// animationSin(t, interpolatedTime); //设置正弦运动
animationCircle(t, interpolatedTime); //设置跑圈运动
}

//设置后是正弦曲线运动
public void animationSin(Transformation t, float interpolatedTime) {
float currentScrollX = totalScrollX * interpolatedTime;
float currentScrollY = (float) (totalScrollY * Math.sin(2 * Math.PI
* interpolatedTime));
t.getMatrix().setTranslate(currentScrollX, currentScrollY);
}

//设置后是圆圈运动
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();
}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值