帧动画 FrameMovie

public class FrameMovie extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(new FrameMovieView(this));
	}
}

class FrameMovieView extends SurfaceView implements Callback,Runnable{

	private SurfaceHolder mSurfaceHolder;
	private Thread mThread;
	private Canvas mCanvas;
	private boolean mFlag;
	private Bitmap[] mFishBitmaps=new Bitmap[10];
	private int mCurrentFrame;
	private Paint mPaint;

	public FrameMovieView(Context context) {
		super(context);
		mSurfaceHolder=this.getHolder();
		mSurfaceHolder.addCallback(this);
		mPaint=new Paint();
		mPaint.setColor(Color.WHITE);
		mPaint.setAntiAlias(true);
		setFocusable(true);
		
		
		for (int i = 0; i < mFishBitmaps.length; i++) {
			mFishBitmaps[i]=BitmapFactory.decodeResource(getResources(), R.drawable.fish0+i);
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		mFlag=true;
		mThread=new Thread(this,"My Thread");
		mThread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		mFlag=false;
	}

	@Override
	public void run() {
		while (mFlag) {
			long start=System.currentTimeMillis();
			myDraw();
			logic();
			long end=System.currentTimeMillis();
			if((end-start)<50){
				try {
					Thread.sleep(50-(end-start));
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	private void logic() {
		mCurrentFrame++;
		if(mCurrentFrame>=mFishBitmaps.length){
			mCurrentFrame=0;
		}
	}

	private void myDraw() {
		mCanvas=mSurfaceHolder.lockCanvas();
		if (mCanvas != null) {
			mCanvas.drawColor(Color.WHITE);
			mCanvas.drawBitmap(mFishBitmaps[mCurrentFrame], 0,0,mPaint);
		}
		mSurfaceHolder.unlockCanvasAndPost(mCanvas);
	}

}

问题一:

private void logic() {
		mCurrentFrame++;
		if(mCurrentFrame>=mFishBitmaps.length){
			mCurrentFrame=0;
		}
	}
在刚开始写这个函数的时候将if(mCurrentFrame>=mFishBitmaps.length)写成if(mCurrentFrame>mFishBitmaps.length)导致画面播放一会之后出现 java.lang.ArrayIndexOutOfBoundsException

你可能感兴趣的:(帧动画 FrameMovie)