2015-07-20 137 views
0

我試圖繪製由紋理包裝器創建的精靈(每個精靈的寬度,高度,位置,紋理矩形等信息的一個大位圖) 目前還不清楚如何繪製一個精靈的情況下,它應該旋轉90度。 我試着旋轉畫布,但我沒有成功(精靈並沒有出現在正確的位置)如何在畫布上繪製由特定值旋轉的位圖的一部分

這是我畫定期(不旋轉)精靈:

Rect srcRect = <a rect in the big bitmap> (for example left=0, top=0, right=100, bottom=80) 
Rect destRect = new Rect(spriteOffsetX, spriteOffsetY, spriteOffset.x + spriteWidth, spriteOffset.y + spriteHeight); 
canvas.drawBitmap(bitmap, srcRect, destRect, null); 

隨着旋轉的精靈我旋轉畫布

canvas.rotate(-90f, canvas.getWidth()/2.0f, canvas.getHeight()/2.0f); 

並以與正常(不旋轉)的精靈相同的方式繪製精靈。但它出現在錯誤的位置上。

有人可以幫助解決這個問題嗎?

回答

0

我結束了旋轉位圖,而不是畫布。這對我來說似乎更容易一些。

考慮: textureRect - 在spritesheet精靈矩形, spriteOffset - 地步精靈應該畫布, 旋轉上繪製 - 在我的情況下,它是-90度

//this code is required because we need to clip the rest of bitmap 
//we only need a sprite to be drawn 
destRect.left = spriteOffset.x; 
destRect.top = spriteOffset.y; 
destRect.right = spriteOffset.x + textureRect.height(); 
destRect.bottom = spriteOffset.y + textureRect.width(); 
canvas.clipRect(destRect); 

注意,我想補充的高度到X和寬度到Y.這不是錯誤打印,這是故意的,因爲位圖旋轉。

//rotate, after this the bitmap will be above 0,0 
matrix.postRotate(-90); 
//move it so that the bitmap will be in 0,0 i.e. right left corner of the bitmap 
//will be in left top corner if we draw the bitmap right now 
matrix.postTranslate(0.0f, imageWidth); 
//move so that sprite we need to draw will be in 0,0 
matrix.postTranslate(-textureRect.top, -(imageWidth - textureRect.right)); 
//move again to final position 
matrix.postTranslate(spriteOffset.x, spriteOffset.y); 

事實上,三個postTranslate可以被簡化爲一個,但我想要發表他們所有的評論來說清楚。

相關問題