我有一个位图,我想创建一个由第一个围绕其中心旋转获得的新位图。实际上我使用此代码,但它不起作用。
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);
在这种情况下,在画布上调用的旋转命令(但缩放命令也是如此)对于任何参数都将被完全忽略。我也尝试使用:
c.drawColor(Color.BLACK)
代替
c.drawBitmap(temp, 0, 0, null);
绘制所有画布并确认命令被忽略。