/**
* 繪制自旋轉位圖
*
* @param canvas
* @param paint
* @param bitmap
* 位圖對象
* @param rotation
* 旋轉度數
* @param posX
* 在canvas的位置坐標
* @param posY
*/
private void drawRotateBitmap(Canvas canvas, Paint paint, Bitmap bitmap,
float rotation, float posX, float posY) {
Matrix matrix = new Matrix();
int offsetX = bitmap.getWidth() / 2;
int offsetY = bitmap.getHeight() / 2;
matrix.postTranslate(-offsetX, -offsetY);
matrix.postRotate(rotation);
matrix.postTranslate(posX + offsetX, posY + offsetY);
canvas.drawBitmap(bitmap, matrix, paint);
}
首先,我們將bitmap向左上角移動1半(xy各1半),然后旋轉需要的度數。最后再將center移動回來。然后再移動到位置坐標(posX,posY)上。注意,坐標(posX,posY)是位圖的左上角的點。
另外,為了使旋轉聯貫,調用該方法時:
rotation += 0.1f * (new Random().nextInt(20));
drawRotateBitmap(canvas, paint, bitmap, rotation, posX, posY);
Android開發同盟QQ群:272209595