我有一个位图,我想创建一个新的位图,该位图是由第一个围绕其中心旋转而获得的。实际上我使用这个代码,但它不起作用。
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);
绘制所有画布并确认命令被忽略。
答案 0 :(得分:1)
试试这个:
Canvas c = new Canvas(target);
c.rotate(rotationAngle, centerX, centerY);
c.drawBitmap(source, null);
希望这会有所帮助:)
答案 1 :(得分:0)
int count = canvas.save();
canvas.rotate(rotationAngle, centerX, centerY);
c.drawBitmap(source, null);
canvas.restoreToCount(count);