0
嗨在我的應用程序中,我有一個經常出現的爆炸動畫。當創建爆炸時,我從資源加載3個圖像,然後一旦爆炸動畫結束,我回收這3個圖像。我經常這樣做,並已注意到幀率下降。他們是一個更好的方式來做這個像靜態位圖或什麼的?回收位圖的後果?
嗨在我的應用程序中,我有一個經常出現的爆炸動畫。當創建爆炸時,我從資源加載3個圖像,然後一旦爆炸動畫結束,我回收這3個圖像。我經常這樣做,並已注意到幀率下降。他們是一個更好的方式來做這個像靜態位圖或什麼的?回收位圖的後果?
我曾經做過一個應用程序,一個炸彈爆炸的簡單畫布。 我使用了由爆炸的每個步驟組成的平鋪位圖,並僅繪製了它的一部分,這經常會爲了創建動畫效果而進行更改。 它更新的步驟之後自動currentTimestamp
所以這是爆炸類:
public class ExplosionAnimated {
private static final String TAG = ExplosionAnimated.class.getSimpleName();
private Bitmap mBitmap;
private Rect mSourceRect;
private int mFrameCountX;
private int mFrameCountY;
private int mCurrentFrame;
private long mFrameTicker;
private int mFramePeriod;
private int mSpriteWidth;
private int mSpriteHeight;
private int mX;
private int mY;
private boolean mFinished = false;
public ExplosionAnimated(Bitmap pBitmap, int pX, int pY,
int pFrameCountX, int pFrameCountY, int pFps) {
this.mBitmap = pBitmap;
this.mX = pX;
this.mY = pY;
this.mCurrentFrame = 0;
this.mFrameCountX = pFrameCountX;
this.mFrameCountY = pFrameCountY;
this.mSpriteWidth = pBitmap.getWidth()/pFrameCountX;
this.mSpriteHeight = pBitmap.getHeight()/pFrameCountY;
this.mSourceRect = new Rect(0, 0, this.mSpriteWidth, this.mSpriteHeight);
this.mFramePeriod = 1000/pFps;
this.mFrameTicker = 0l;
}
public void update(long gameTime) {
if (gameTime > this.mFrameTicker + this.mFramePeriod) {
this.mFrameTicker = gameTime;
this.mCurrentFrame++;
if (this.mCurrentFrame >= this.mFramePeriod) {
this.mCurrentFrame = 0;
this.mFinished = true;
}
}
if (!this.mFinished) {
this.mSourceRect.left = this.mCurrentFrame * this.mSpriteWidth;
this.mSourceRect.right = this.mSourceRect.left + this.mSpriteWidth;
}
}
public void draw(Canvas canvas) {
Rect destRect = new Rect(this.mX, this.mY,
this.mX + this.mSpriteWidth,
this.mY + this.mSpriteHeight);
canvas.drawBitmap(this.mBitmap, this.mSourceRect, destRect, null);
}
public boolean isFinished() {
return this.mFinished;
}
}
這些都是從一個對象的方法(Bomb.java的爲例)啓動爆炸和繪製:
public void explode(Context pContext, Canvas pCanvas) {
this.mState = State.EXPLODING;
this.mExplosion = new ExplosionAnimated(this.mExplosionBitmap,
(int) this.mX, (int) this.mY, 7, 3, 7);
}
public void doDraw(Canvas pCanvas) {
if (this.mState == State.EXPLODING) {
if (this.mExplosion.isFinished()) {
this.mState = State.EXPLODED;
} else {
this.mExplosion.update(System.currentTimeMillis());
this.mExplosion.draw(pCanvas);
}
} else {
pCanvas.drawBitmap(this.mBombBitmap, this.mX, this.mY, null);
}
}
我用一個Thread和一個SurfaceView來連續地繪製炸彈(或爆炸),只給出currentTimestamp來更新爆炸。
我希望它能幫助,如果你需要我可以顯示和講解更多的代碼
感謝您的代碼可以幫助很多。因此,不要將您的爆炸類中的位圖加載到您的surfaceview類中,然後將其傳入,因此只需加載一次。知道了謝謝! – MikeT 2012-08-12 21:27:05