0
 public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.genie_out);



        genie = (ImageView) findViewById(R.id.genieout);
        startService(new Intent(this, MyService2.class));
        SceneAnimation come = new SceneAnimation(genie, comingout,     durationcomingout);
        SceneAnimation circled = new SceneAnimation(genie, circle,durationcircle);

       come.play(1);



       circled.play(1);


    }

有一个 ImageView 并且它占据了整个屏幕,我想知道如何等到 come.play(1) 完成播放 circle.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);
}



};
4

1 回答 1

1

这是实现自己的 AnimationListener 的一种方法

package com.example.intentfiletersample;

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

始终建议您先浏览 Android sdk 附带的 Animation API,然后再实施自己的,看看是否适合您。

希望有帮助。

于 2012-11-19T01:42:46.180 回答