2012-09-26 57 views
0

我想畫一個精靈表動畫在屏幕上,當我使用此代碼這個工作的罰款之前的部分圖像。創建的其他圖像

canvas.drawColor(Color.TRANSPARENT); 
    Rect src = new Rect(CurrentSprite.x * SheetSize.x, CurrentSprite.y * SheetSize.y, (CurrentSprite.x * SheetSize.x) + SheetSize.x, (CurrentSprite.y * SheetSize.y) + SheetSize.y); 
    Rect dst = new Rect(X, Y, X + SheetSize.x, Y + SheetSize.y); 
    canvas.drawBitmap(bmp, src, dst, null); 

但是我現在需要旋轉圖像,然後才能在屏幕上繪製圖像。所以我更改了代碼,以便製作精靈的本地副本。我要畫這個但是在這裏我遇到了一個問題。我使用的是Bitmap.createBitmap方法,但似乎這種方法不執行任何操作,因爲當我看着屏幕上的完整的精靈表是在屏幕上繪製,有時我得到OutOfMemoryException異常(理解的,因爲完整的圖像是相當大的)。

canvas.drawColor(Color.TRANSPARENT); 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(Degrees); 
    Bitmap tempAnimationBmp = Bitmap.createBitmap(bmp, CurrentSprite.x * SheetSize.x, CurrentSprite.y * SheetSize.y, (CurrentSprite.x * SheetSize.x) + SheetSize.x, (CurrentSprite.y * SheetSize.y) + SheetSize.y, matrix, true); 
    canvas.drawBitmap(tempAnimationBmp, X, Y, null); 
    tempAnimationBmp.recycle(); 

爲什麼我會得到完整的精靈表?

+0

看看我在這篇文章中的答案。我認爲你可以改變它到你的要求:http://stackoverflow.com/questions/12330220/androiddraw-image-in-the-center-of-another-image/12332941#12332941 –

回答

0

我建議使用DrawableContainer s,而不是試圖創造一堆位圖之一。 Drawables非常易於使用 - 您只需設置尺寸,將其傳遞給畫布,並將其自身繪製到該位置的畫布上。關於Drawables的好處在於它們也很容易製作,所以你可以創建一個自定義的SpriteDrawable類,它從系統獲取一個資源,並只將其區域繪製到畫布上。

通過對這種流動的工作,而不是試圖創建一批的位圖,你會節省大量的內存和可能很多頭痛的,當你試圖用Android的圖形系統更多的工作。