自定义动画结束触发

时间:2015-04-22 17:22:58

标签: java android events animation

我正在写一个"太空入侵者"风格的应用程序和我需要做的移动停止重复动画,这是游戏的经典。我使用下面的代码获得了一种方法,但是当我想回去时,似乎没有一种简单的方法可以做到这一点。我认为最简单的方法是在结束动画事件中发生一些事情,但即使调用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
}

1 个答案:

答案 0 :(得分:0)

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
    }
}