2012-01-07 252 views
1

我創建一個瓷磚的棋盤遊戲。 我想旋轉一個位圖瓷磚塊幾個預先確定的程度。
當我旋轉我的位圖時,大小改變。
例如,如果我想要一個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); 
} 
+0

檢查我的答案[這裏] [1],workd沒有問題 [1]:http://stackoverflow.com/questions/8712652/rotating-image-on-a-canvas-in -android/28293393#28293393 – MSaudi 2015-02-03 07:23:28

回答

1

什麼是你的代碼發生的事情,是創建的位圖的尺寸是否合適。但是,所有Bitmaps會自動縮放以適應當前正在使用的屏幕密度。

有兩種方式(即我所知道的),以得到你想要的結果。

  1. 您可以在創建位圖之後並在將其包裝到繪圖之前設置位圖的密度。要做到這一點,添加類似代碼:

    resizedBitmap.setDensity(DisplayMetric.DENSITY_HIGH);

    這個代碼設置該位圖被設計爲密度,並且可以防止從Android的自動縮放的圖像。

  2. 第二種解決方案是超過一個特定的解決方案,這個特定的問題的方法的。我不細講,對於看Supporting Multiple Screens | Android Developers

希望有人會發生沿,可以回答你的問題不是我能更好。

+0

沒有一點區別。無論SetMinimums是否被使用,imageView仍然是68x68。 FIT_XY只是將整個imageView縮放到全屏 – Martin 2012-01-07 05:20:30

+0

它與ImageView無關 - 它是BitMap。當位圖被調整大小和旋轉時,它不是正確的大小。在ImageView上使用包裝內容將僅適用於其當前(不正確)尺寸。 – Martin 2012-01-08 01:33:51

+0

例如,如果我有一個150x150位圖,希望它縮小到75x75並旋轉,在上面的代碼中,resizedBitmap從102x102處的createBitmap出來。然後創建新的BitmapDrawable,bmd,它是68x68。我無法弄清楚如何旋轉一個對象,並保持瓷磚相同的大小 – Martin 2012-01-08 01:36:47