Android 旋转后调整大小或剪裁

Android 旋转后调整大小或剪裁,android,Android,我有一个10x10的表格布局,每个单元格包含一个ImageView。我通过在移动事件中更新每个网格中的图片来模拟在这个游戏网格中移动。我还旋转图像,使图像与行进方向对齐。我的问题是,当图像旋转时,我的单元格行和列会展开以适应旋转后的图像 我想要的是将图像裁剪为保持在表行和列的维度内。如果这是不可能的,那么我想缩放图像,使其保持在原始表格单元格的尺寸范围内 另一个潜在问题是,图像最初会进行缩放以适应单元格(这是在表格展开以填充屏幕时自动完成的)。我不想改变这种行为。我只想修剪图像的角点,因为它们在

我有一个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>
...
我的表格配置如下:

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>
...
尝试添加

android:scaleType="centerCrop" 

到xml布局中的每个ImageView项。

最终解决方案必须利用图像的“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);
}

我以前也试过,但它不起作用,因为我的图像最初需要缩放以适应细胞。表格在启动时动态调整大小以填充屏幕,然后将其显示的图像缩放以适合单元格。一旦初始化完成。我需要防止图像旋转时细胞生长。我希望系统首先确定所有大小和初始缩放,然后在设置之后不要更改。在这方面有几个注意事项:1)表中的所有图像都必须是相同的大小,否则必须添加更多逻辑才能处理不同的大小。2) 如果需要进行一些缩放,则可能需要编写更多的逻辑,以确保图像不大于位图。