我正在使用此代码将可绘制对象旋转 45 度:
public static Drawable rotateImage(Context ctx) {
// load the origial Bitmap
Bitmap BitmapOrg = BitmapFactory.decodeResource(ctx.getResources(),
R.drawable.car);
int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = 90;
int newHeight = newWidth * height / width;
// calculate the scale
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the Bitmap
matrix.postScale(scaleWidth, scaleHeight);
// if you want to rotate the Bitmap
matrix.postRotate(-45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the Bitmap
// to the ImageView, ImageButton or what ever
return new BitmapDrawable(resizedBitmap);
}
问题是虽然我给了它一个newWidth,但Drawable的大小还是一样的,旋转的图像看起来被拉伸了。如果它是一个矩形,我如何旋转可绘制对象以获得旋转图像的良好外观?