2011-04-25 160 views
0

我有一個10x10的表格佈局,每個單元包含一個ImageView。我通過在移動事件中更新每個網格中的圖片來模擬在這個遊戲網格中移動。我還旋轉圖像以將圖像與行進方向對齊。我的問題是,隨着圖像旋轉,它會導致我的細胞行和列擴大以適應旋轉的圖像。旋轉後調整大小或剪切

我想要的是裁剪圖像保留在錶行和列的維度。如果不可能,那麼我想縮放圖像以保持原始表格單元格的尺寸。

另一個潛在的問題是圖像最初會縮放以適合單元格(這是在表格展開以填充屏幕時自動完成的)。我不想改變這種行爲。我只是想在它們旋轉時通過表格單元的原始邊界來修剪圖像的角落。

我的旋轉代碼如下:

Matrix matrix = new Matrix(); 
matrix.postRotate((float) (360 - sector.getShip().getHeading())); 
Bitmap bMap = BitmapFactory.decodeResource(image.getContext().getResources(), getShip().getGridSymbol()); 
image.setImageBitmap(Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), matrix, true)); 
image.setImageResource(sector.getShip().gridParams.default); 

我的表設置爲以下:

<TableLayout android:id="@+id/quadrantGrid" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignWithParentIfMissing="true" android:stretchColumns="*" android:background="#000000"> 
... 
<TableRow> 
    <TextView android:text="1" android:background="#404040" android:gravity="center"></TextView> 
    <ImageView android:id="@+id/grid00"></ImageView> 
    <ImageView android:id="@+id/grid01"></ImageView> 
    <ImageView android:id="@+id/grid02"></ImageView> 
    <ImageView android:id="@+id/grid03"></ImageView> 
    <ImageView android:id="@+id/grid04"></ImageView> 
    <ImageView android:id="@+id/grid05"></ImageView> 
    <ImageView android:id="@+id/grid06"></ImageView> 
    <ImageView android:id="@+id/grid07"></ImageView> 
    <ImageView android:id="@+id/grid08"></ImageView> 
    <ImageView android:id="@+id/grid09"></ImageView> 
</TableRow> 
... 

UPDATE: 我找到了一個解決方案,但我不知道它的最有效解。

我的所有圖片都是25x25。因此,操作系統計算出如何縮放這些圖像以適應單元格。該解決方案只執行兩個操作。首先旋轉,然後從旋轉後的圖像創建一個較小的圖像。 (注意:當我創建我的圖像時,我確保圖像的重要部分將包含在具有圖像寬度的直徑的圓內以允許此類裁剪)。

我的代碼如下。我寧願在一種方法中執行這兩種操作,但林不知道如何將它分解爲兩個步驟。

Matrix matrix = new Matrix(); 
matrix.postRotate((float) (360 - sector.getShip().getHeading())); 
Bitmap bMap = BitmapFactory.decodeResource(image.getContext().getResources(), getShip().getGridSymbol()); 
bMap = Bitmap.createBitmap(bMap, 0, 0, 25, 25, matrix, true); 
int x = (bMap.getWidth() - 25)/2; 
int y = (bMap.getHeight() - 25)/2; 
image.setImageBitmap(Bitmap.createBitmap(bMap, x, y, 25, 25)); 

回答

1

最終的解決方案必須利用圖像的「getMeasuredHeight()」。我沒有必要使用getMeasuredWidth(),因爲對於我的特定佈局來說,高度是限制因素。

下面是最終位代碼的工作原理:

private Bitmap getRotatedShipBitmap(SectorGridImageView image, 
     Bitmap bitmap, Matrix matrix) { 
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), 
      bitmap.getHeight(), matrix, true); 
    int yHeight = (image.getMeasuredHeight() == 0) ? bitmap.getHeight() 
      : image.getMeasuredHeight(); 
    int xWidth = yHeight; 
    int x = (bitmap.getWidth() - xWidth)/2; 
    int y = (bitmap.getHeight() - yHeight)/2; 
    return Bitmap.createBitmap(bitmap, x, y, xWidth, yHeight); 
} 
+0

對此有幾點小心: 1)表格中的所有圖像需要具有相同的尺寸,否則您將不得不添加更多的邏輯來處理不同的尺寸。 2)如果你在工作中有一些縮放,你可能必須編寫更多的邏輯來確保圖像不會比位圖大。 – Kevin 2011-04-27 17:18:11

1

嘗試增加

android:scaleType="centerCrop" 

每個ImageView的項目在XML佈局。

+0

我以前試過了,但它不工作,因爲我需要的圖像縮放最初以適應細胞。該表在啓動時動態調整大小以填充屏幕,然後縮放顯示的圖像以適應單元格。一旦完成初始化。當圖像旋轉時,我需要防止細胞增長。 我希望系統初步確定所有尺寸和初始縮放,然後在設置之後不要更改。 – Kevin 2011-04-25 16:28:29

+0

哇,它在我的情況下工作。非常感謝親愛的。 – 2014-01-31 06:51:54