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

89 views Asked by At

I'm designing a puzzle game in android eclipse. I have a picture in resources drawable file named "puzzle_image01". i want to divide this image to 9 parts and put it to some variables. then using them for the puzzle pieces. Now, how can i divide the image to 9 parts?

thank you for your advice.

2

There are 2 answers

0
Stefano Vuerich On BEST ANSWER
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)
}
0
Cruceo On

Interesting challenge. Didn't test this, but it should work for any row/column combination where the image's dimensions divided by their respective value is greater than zero:

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;
}