Java 自定义动画结束触发器
我正在写一个“太空入侵者”风格的应用程序,我需要做的移动停止重复动画,这是经典的游戏。使用下面的代码,我让进程以一种方式运行,但是当我想返回时,似乎没有一种简单的方法可以做到这一点。我认为最简单的方法是让end动画事件发生一些事情,但是我无法触发事件,即使调用Java 自定义动画结束触发器,java,android,events,animation,Java,Android,Events,Animation,我正在写一个“太空入侵者”风格的应用程序,我需要做的移动停止重复动画,这是经典的游戏。使用下面的代码,我让进程以一种方式运行,但是当我想返回时,似乎没有一种简单的方法可以做到这一点。我认为最简单的方法是让end动画事件发生一些事情,但是我无法触发事件,即使调用super.end()。我四处寻找了一种手动触发事件的方法,但没有结果。我做这件事的方式可能有点粗略,但它现在已经足够好了,可以让它一直工作下去 public void start() { if(begin < end) //
super.end()代码>。我四处寻找了一种手动触发事件的方法,但没有结果。我做这件事的方式可能有点粗略,但它现在已经足够好了,可以让它一直工作下去
public void start()
{
if(begin < end) //recursive end condition
{
int distance = interval + begin; //change the distance to be traveled
//create new animation to do part of the whole animation
ObjectAnimator anim = ObjectAnimator.ofFloat(toAnimate, property, begin,distance);
TimeInterpolator inter = new TimeInterpolator() //makes the animation move with only one frame
{
public float getInterpolation(float prog)
{
return Math.round(prog * 10) / 10;
}
};
anim.setInterpolator(inter);
anim.setDuration((long)(500));
anim.addListener(new AnimatorListener()
{
@Override
public void onAnimationStart(Animator animation){}
@Override
public void onAnimationEnd(Animator animation)
{
start(); //start the next part of the movement
}
@Override
public void onAnimationCancel(Animator animation){}
@Override
public void onAnimationRepeat(Animator animation){}
});
begin = begin + interval; //update end recursion value
anim.start(); //begin the animation
}
super.end(); //this doesn't work... rip
}
public void start()
{
if(begin
公共作废开始()
{
if(begin
为什么不使用TimeAnimator?
public void start()
{
if(begin < end) //recursive end condition
{
int distance = interval + begin; //change the distance to be traveled
//create new animation to do part of the whole animation
ObjectAnimator anim = ObjectAnimator.ofFloat(toAnimate, property, begin,distance);
TimeInterpolator inter = new TimeInterpolator() //makes the animation move with only one frame
{
public float getInterpolation(float prog)
{
return Math.round(prog * 10) / 10;
}
};
anim.setInterpolator(inter);
anim.setDuration((long)(500));
anim.addListener(new AnimatorListener()
{
@Override
public void onAnimationStart(Animator animation){}
@Override
public void onAnimationEnd(Animator animation)
{
begin += interval;
if(begin < end){
start(); //start the next part of the movement
}
else{
// Do something else
}
}
@Override
public void onAnimationCancel(Animator animation){}
@Override
public void onAnimationRepeat(Animator animation){}
});
anim.start(); //begin the animation
}
}