1
I'm使用此代码以旋转抽拉45度:如何在Android中旋转矩形形状的Drawable?
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?
问题是我没有.xml布局。我正在开发一款游戏,一切都是通过编程完成的。 –