如何在 android eclipse 中将图像划分为某些部分?

How to divide an image to some parts in android eclipse?

我正在 android eclipse 中设计一款益智游戏。我在名为 "puzzle_image01" 的资源可绘制文件中有一张图片。我想将这张图片分成 9 部分并将其放入一些变量中。然后将它们用于拼图。现在,如何将图像分成 9 个部分?

谢谢你的建议。

for(int i = 0; i < 9; ++i) {

    int indexY = 0;

    if(i < 3) {
        imageStartY = 0;
        imageFinishY = sourceBitmap.height() / 3;
    }

    else if(i < 6) {
        imageStartY = sourceBitmap.height() / 3;
        imageFinishY = (sourceBitmap-height() / 3) * 2;
    }

    else if(i < 9) {
        imageStartY = (sourceBitmap.height() / 3) * 2;
        imageFinishY = sourceBitmap.height();
    }

    Bitmap resizedbitmap = Bitmap.createBitmap(sourceBitmap
                                            , ((sourceBitmap.width()) / 3) * i
                                            , imageStartY
                                            ,((sourceBitmap.width()) / 3) * i + sourceBitmap.width()
                                            , imageFinishY)
}

有趣的挑战。没有对此进行测试,但它应该适用于任何 row/column 组合,其中图像的尺寸除以它们各自的值大于零:

private Bitmap[][] split(Bitmap bitmap, int rows, int columns){
    int[] dimens = new int[]{ bitmap.getWidth() / rows, bitmap.getHeight() / columns };

    Bitmap[][] splitMap = new Bitmap[rows][columns];
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < columns; j++){
            splitMap[i][j] = Bitmap.createBitmap(bitmap, i * dimens[0], j * dimens[1], dimens[0], dimens[1]);
        }
    }       

    return splitMap;
}