我創建一個瓷磚的棋盤遊戲。 我想旋轉一個位圖瓷磚塊幾個預先確定的程度。
當我旋轉我的位圖時,大小改變。
例如,如果我想要一個75x75三角瓦塊,對旋轉我得到一個68x68從這個代碼回來。我怎樣才能保持相同的尺寸,所以一切都保持在董事會的規模?旋轉位並保持其大小
下面是我使用的旋轉什麼:
public class RotatebitmapActivity extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.t123);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 75;
int newHeight = 75;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(120);
// 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
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);
// center the Image
imageView.setScaleType(ScaleType.CENTER);
// add ImageView to the Layout
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// set LinearLayout as ContentView
setContentView(linLayout);
}
檢查我的答案[這裏] [1],workd沒有問題 [1]:http://stackoverflow.com/questions/8712652/rotating-image-on-a-canvas-in -android/28293393#28293393 – MSaudi 2015-02-03 07:23:28