如何链接自定义动画?

时间:2012-11-18 07:14:22

标签: android animation

    class SceneAnimation extends Animation{
public int x;
public ImageView mImageView;
public int[] mFrameRess;
public int[] mDurations;
public int mDuration;

public int mLastFrameNo;
public long mBreakDelay;

 public SceneAnimation(ImageView pImageView, int[] pFrameRess, int[] pDurations)
 {

    mImageView = pImageView;
    mFrameRess = pFrameRess;
    mDurations = pDurations;
    mLastFrameNo = pFrameRess.length - 1;

    play(1);
}

    public SceneAnimation(ImageView pImageView, int[] pFrameRess, int pDuration){
    mImageView = pImageView;
    mFrameRess = pFrameRess;
    mDuration = pDuration;
    mLastFrameNo = pFrameRess.length - 1;

    mImageView.setImageResource(mFrameRess[0]);
    playConstant(1);
}

    public SceneAnimation(ImageView pImageView, int[] pFrameRess, int pDuration, long pBreakDelay){            
    mImageView = pImageView;
    mFrameRess = pFrameRess;
    mDuration = pDuration;
    mLastFrameNo = pFrameRess.length - 1;
    mBreakDelay = pBreakDelay;

    mImageView.setImageResource(mFrameRess[0]);
    playConstant(1);
}


synchronized void play(final int pFrameNo)
{


    mImageView.postDelayed(new Runnable(){
        public void run() {


            mImageView.setImageResource(mFrameRess[pFrameNo]);
            if(pFrameNo == mLastFrameNo)
                {
                return;}
            else
                play(pFrameNo + 1);
        }
    }, mDurations[pFrameNo]);
}


public void playConstant(final int pFrameNo){
    mImageView.postDelayed(new Runnable(){
        public void run() {                    
            mImageView.setImageResource(mFrameRess[pFrameNo]);

            if(pFrameNo == mLastFrameNo)
                playConstant(0);
            else
                playConstant(pFrameNo + 1);
        }
    }, pFrameNo==mLastFrameNo && mBreakDelay>0 ? mBreakDelay : mDuration);
}



};

我可以使用它来播放很多帧,我不能播放一个动画,或者在动画之间暂停,所以我试图想办法在我实例化这个类之后使用AnimationListener

1 个答案:

答案 0 :(得分:0)

将AnimationListener添加为类的字段,添加setter。然后其他类可以为SceneAnimation添加一个监听器。

之后,创建一个方法fireFooEvent,如果它不为null,则向侦听器触发事件​​。

然后在SceneAnimation类中使用这些fire方法来通知潜在的侦听器动画的开始和结束。

接下来,在您的活动中,为第一个动画添加一个监听器,启动它。当侦听器收到第一个动画的结尾时,启动第二个动画。

---更新了某些代码

public void setAnimationLIstener( AnimationListener listener ) {
 this.animationListener = listener;
}

public void fireOnAnimationEnd(Animation animation)() {
 if( this.animationListener == null ) {
    return;
 }
 this.animationListener.onAnimationEnd(animation);
}

为每个活动做这件事。然后,您可以轻松地通知您的AnimationScene对象中发生的事件。