如何链接动画api10?

时间:2012-11-18 20:31:06

标签: android api animation

@Override
 public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.g_out);



        g = (ImageView)findViewById(R.id.gout);
        startService(new Intent(this, MyService2.class));



        SceneAnimation come = new SceneAnimation(g, out,duration);
        SceneAnimation circled = new SceneAnimation(g, circled,durationc);

       come.play(1);



       circled.play(1);









    }







}



class SceneAnimation {
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);
}


   public 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);
}



};

我正在寻找让onCreate()中的2个SceneAnimation()一个接一个地播放的解决方案,它们同时播放,一个比另一个长,所以第二个播放然后播放首先踢第二个结束的帧所在的地方,我听说过AnimationListener,但我不知道如何将它合并到要听的类中,我将非常感谢一个演练或代码示例和解释我很新这个,谢谢你的时间

2 个答案:

答案 0 :(得分:0)

实际上这很简单。创建一个Animation实例并将AnimationListener附加到它。在onAnimationEnd中,您将开始第二个动画。 Et voila,两个按顺序执行的动画。

    Animation firstAnimation = new TranslateAnimation(getApplicationContext(), null);
    firstAnimation.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) { 
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            Animation secondAnimation = new TranslateAnimation(getApplicationContext(), null);
            secondAnimation.start();
        }
    });

    firstAnimation.start();

答案 1 :(得分:0)

import android.widget.ImageView;

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

public int mLastFrameNo;
public long mBreakDelay;

private AnimationListener mAnimationListener;

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

public void setAnimationListener(AnimationListener listener){
this.mAnimationListener = listener;
}

public void play( final int pFrameNo ) {

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

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

        // Callback when animation ends.
        if( mAnimationListener != null ){
            mAnimationListener.onAnimationEnd();
        }
    }
}, 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 );

        // Callback when animation ends.
        if( mAnimationListener != null ){
            mAnimationListener.onAnimationEnd();
        }
    }
}, pFrameNo == mLastFrameNo && mBreakDelay > 0 ? mBreakDelay : mDuration );
}


public static interface AnimationListener {
public void onAnimationEnd();
// You can add onAnimationStart(), and do the same thing like onAnimationEnd.
}
};