簡體   English   中英

如何在Android中繪制到畫布的多個圖像之間旋轉特定圖像?

[英]How to rotate particular image among multiple images drawn to canvas in android?

我需要一些幫助,在多個圖像中圍繞其中心旋轉一個圖像,這些圖像被繪制到android中的畫布。

我正在將圖像加載到畫布,如下所示。

canvas.drawBitmap(mMachineBackground, 0, 0, null);
canvas.drawBitmap(mMachineRotator, 0, 0, null);

我想只圍繞軸的中心旋轉第二個位圖,而不是旋轉整個畫布(也包括第一個位圖)。

提前致謝。

您可以圍繞中心軸旋轉:

Matrix matrix = new Matrix();

//move image

matrix.setTranslate(getXPos() - (imageWidth / 2), getYPos() - (imageHeight / 2));

//rotate image, getXPos, getYPos are x & y coords of the image

matrix.postRotate(angleInDegrees, getXPos() - imageWidth / 2, getYPos() - imageHeight / 2);

//rotatedBMP is the image you are drawing, 

canvas.drawBitmap(rotatedBMP, matrix, Paint);
//Drawing the Player Canon.
           Matrix matrix = new Matrix();
           //move image
           newHeight = getHeight() - canon1[0].getHeight() - stand1.getHeight();
           Log.d(TAG, "New Height : " + newHeight);
           //matrix.setTranslate(0, getHeight() - canon1[0].getHeight() + stand1.getHeight());
           matrix.setTranslate(-newHeight,newHeight);

           //   rotate image, getXPos, getYPos are x & y coords of the image (ANgle in degree)).
           //matrix.postRotate(45, 0, getHeight() - canon1[0].getHeight() + stand1.getHeight());
           matrix.postRotate(-30,0,0);
           //Draw function. 
           canvas.drawBitmap(canon1[0], matrix, null);

我從上面的代碼的參考寫的這段代碼非常好。

我能夠在畫布上旋轉特定圖像。

恐怕你不能這樣做。 就我到目前為止所學到的,您可以旋轉整個上下文,但不能旋轉單個位圖。 我所知道的轉換矩陣只能應用於整個畫布。 (我不是畫布大師,但我正在對你同樣的問題做廣泛的研究)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM