2012-11-23 119 views
0

我這裏在Android中畫出一個旋轉縮放位圖的方法:在Android中繪製位圖部分?

public void drawRotatedScaledBitmap(Bitmap b, 
     float centerX, float centerY, float width, float height, float angle) 
{ 
    float scaleX = width/b.getWidth(); 
    float scaleY = height/b.getHeight(); 
    centerX -= (b.getWidth() * scaleX)/2.0f; 
    centerY -= (b.getHeight() * scaleY)/2.0f; 
    matrix.reset(); 
    matrix.setTranslate(centerX, centerY); 
    matrix.postRotate(angle * (180.0f/(float)(Math.PI)), 
      centerX + (b.getWidth() * scaleX)/2.0f, 
      centerY + (b.getHeight() * scaleY)/2.0f); 
    matrix.preScale(scaleX,scaleY); 
    canvas.drawBitmap(b, matrix, null); 
} 

我不知道我怎麼能修改此採取浮動sourceX,浮sourceY,浮sourceW,浮sourceH。

我想呈現瓷磚集,所以我需要告訴它在位圖上說64,64,64,64。

我怎麼能這樣做?

謝謝

回答

0

這是我如何解決它:

public void drawRotatedScaledBitmap(Bitmap b, 
     float centerX, float centerY, float width, float height, 
     float sourceX, float sourceY, 
     float sourceWidth, float sourceHeight, float angle) 
{ 
    float cx = centerX; 
    float cy = centerY; 

    dstRect.left = (int)centerX - (int)(width/2); 
    dstRect.top = (int)centerY - (int)(height/2); 
    dstRect.right = dstRect.left + (int)width; 
    dstRect.bottom = dstRect.top + (int)height; 

    srcRect.left = (int)sourceX; 
    srcRect.top = (int)sourceY; 
    srcRect.right = srcRect.left + (int)sourceWidth; 
    srcRect.bottom = srcRect.top + (int)sourceHeight; 

    canvas.rotate(angle * (180.0f/(float)(Math.PI)),cx,cy); 
    canvas.drawBitmap(b, srcRect, dstRect, null); 
    canvas.rotate(-angle * (180.0f/(float)(Math.PI)),cx,cy); 
} 
0

也許你可以試試canvas.clipRect()來指定繪圖區域。

+0

能否請你解釋一下?我沒有看到這是如何工作,因爲我正在旋轉... – jmasterx

+0

Android有一個drawBitmap(源矩形,目標矩形),所以它必須是可能的。 – jmasterx

+0

您可以旋轉畫布而不是旋轉位圖嗎?然後,您可以使用drawBitmap(源矩形,目標矩形)以旋轉角度繪製一部分位圖。 – Neil