[英]How to rotate bitmap around center point using canvas?
我有一個位圖,我想創建一個新的位圖,它是由第一個繞其中心旋轉的位圖獲得的。 實際上我使用此代碼,但是它不起作用。
Bitmap source = ((BitmapDrawable)r.getDrawable(R.drawable.rectangle)).getBitmap();
int targetWidth = (int)(mWidth * Math.sin(rotationAngle) + mHeight * Math.cos(rotationAngle));
int targetHeight = (int)(mWidth * Math.cos(rotationAngle) + mHeight * Math.sin(rotationAngle));
Bitmap target = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(target);
Matrix m = new Matrix();
m.setRotate(rotationAngle, targetWidth/2f, targetHeight/2f);
c.drawBitmap(source, m, null);
我也嘗試過此代碼,但無濟於事。
Bitmap source = ((BitmapDrawable)r.getDrawable(R.drawable.rectangle)).getBitmap();
int targetWidth = (int)(mWidth * Math.sin(rotationAngle) + mHeight * Math.cos(rotationAngle));
int targetHeight = (int)(mWidth * Math.cos(rotationAngle) + mHeight * Math.sin(rotationAngle));
Bitmap target = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(target);
c.rotate(rotationAngle, targetWidth/2f, targetHeight/2f);
c.drawBitmap(temp, 0, 0, null);
在這種情況下,畫布上調用的rotate命令(但scale命令也是如此)對於任何參數都將被完全忽略。 我也嘗試使用:
c.drawColor(Color.BLACK)
代替
c.drawBitmap(temp, 0, 0, null);
繪制所有畫布,並確認已忽略命令。
嘗試這個:
Canvas c = new Canvas(target);
c.rotate(rotationAngle, centerX, centerY);
c.drawBitmap(source, null);
希望這可以幫助 :)
int count = canvas.save();
canvas.rotate(rotationAngle, centerX, centerY);
c.drawBitmap(source, null);
canvas.restoreToCount(count);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.