Skip to content

Instantly share code, notes, and snippets.

@abdularis
Created February 26, 2018 11:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdularis/976d6dda553349a75860f71a263b1ff7 to your computer and use it in GitHub Desktop.
Save abdularis/976d6dda553349a75860f71a263b1ff7 to your computer and use it in GitHub Desktop.
android bitmap slicer
package abdularis.github.com.slidingpuzzle;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
/**
* Created by abdularis on 29/12/17.
*/
public final class ImageUtil {
public static Bitmap[] sliceBitmap(Bitmap original, int rowCount, int colCount) {
Bitmap pieces[] = new Bitmap[rowCount * colCount];
Canvas canvas = new Canvas();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
int pieceWidth = original.getWidth() / colCount;
int pieceHeight = original.getHeight() / rowCount;
int currPieceIndex = 0;
Rect srcRect = new Rect();
Rect dstRect = new Rect(0, 0, pieceWidth, pieceHeight);
for (int row = 0; row < rowCount; row++) {
for (int col = 0; col < colCount; col++) {
Bitmap currPiece = Bitmap.createBitmap(pieceWidth, pieceHeight, Bitmap.Config.ARGB_8888);
srcRect.left = pieceWidth * col;
srcRect.top = pieceHeight * row;
srcRect.right = srcRect.left + pieceWidth;
srcRect.bottom = srcRect.top + pieceHeight;
canvas.setBitmap(currPiece);
canvas.drawBitmap(original, srcRect, dstRect, paint);
pieces[currPieceIndex++] = currPiece;
}
}
return pieces;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment