Created
February 26, 2018 11:01
-
-
Save abdularis/976d6dda553349a75860f71a263b1ff7 to your computer and use it in GitHub Desktop.
android bitmap slicer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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