簡體   English   中英

旋轉位圖不旋轉

[英]Rotating Bitmap Not Rotating

我已經嘗試了幾個小時旋轉位圖但沒有成功。 我在這個網站上看過很多關於這個主題的文章,似乎首選的解決方案是創建一個臨時畫布。 好吧,我做了這個,我仍然沒有看到一個roated位圖。

我的位圖是一個40x40的藍色方塊,我試圖將它旋轉45度。 那不是要求太多了嗎? 代碼運行時,屏幕上顯示的位圖是未旋轉的原件。 (我也試過翻譯也沒有成功)

這是我的代碼:

// Load the bitmap resource
   fillBMP2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.test1);
   // Copy to a mutable bitmap
   mb = fillBMP2.copy(Bitmap.Config.ARGB_8888, true);
   // Create canvas to draw to
   Canvas offscreenCanvas = new Canvas (mb);
   // Create Matrix so I can rotate it 
   Matrix matrix = new Matrix();
   matrix.setRotate (45); 
   offscreenCanvas.setMatrix (matrix);
   // Send the contents of the canvas into a bitmap
   offscreenCanvas.setBitmap(mb);

稍后在OnDraw中我執行以下操作:

canvas.drawBitmap(mb, 200, 200, null);

我有什么想法我做錯了嗎? 好像它應該工作。

謝謝

試試這個

Matrix matrix = new Matrix();
matrix.setRotate(15);
canvas.drawBitmap(bmp, matrix, paint);

setRotation方法接受表示旋轉度的浮點數。

嘗試這個...

    Matrix matrix = new Matrix();

    float px = 200;

    float py = 200;

    matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getWidth()/2);

    matrix.postRotate(45);

    matrix.postTranslate(px, py);

    canvas.drawBitmap(bitmap, matrix, paint);

你肯定想要使用轉換:看看這個鏈接

基本上是這樣的:

// save the canvas
ctx.save();

// move origin to center
ctx.translate(x,y); 

// rotate
ctx.rotate(angle * (Math.PI / 180));

// draw image
ctx.drawImage(image, x, y, w, h, .w/2, h/2, w, h);

// restore
ctx.restore();

暫無
暫無

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

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