Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7c75fa
commit 59e4425
Showing
3 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
app/src/main/java/dragosholban/com/androidpuzzlegame/MainActivity.java
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,13 +1,124 @@ | |||
package dragosholban.com.androidpuzzlegame; | package dragosholban.com.androidpuzzlegame; | ||
|
|
||
import android.graphics.Bitmap; | |||
import android.graphics.Matrix; | |||
import android.graphics.drawable.BitmapDrawable; | |||
import android.graphics.drawable.Drawable; | |||
import android.support.constraint.ConstraintLayout; | |||
import android.support.v7.app.AppCompatActivity; | import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | import android.os.Bundle; | ||
import android.widget.ImageView; | |||
|
|||
import java.util.ArrayList; | |||
|
|||
import static java.lang.Math.abs; | |||
|
|
||
public class MainActivity extends AppCompatActivity { | public class MainActivity extends AppCompatActivity { | ||
ArrayList<Bitmap> pieces; | |||
|
|
||
@Override | @Override | ||
protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | setContentView(R.layout.activity_main); | ||
|
|||
final ConstraintLayout layout = findViewById(R.id.layout); | |||
ImageView imageView = findViewById(R.id.imageView); | |||
|
|||
// run image related code after the view was laid out | |||
// to have all dimensions calculated | |||
imageView.post(new Runnable() { | |||
@Override | |||
public void run() { | |||
pieces = splitImage(); | |||
for(Bitmap piece : pieces) { | |||
ImageView iv = new ImageView(getApplicationContext()); | |||
iv.setImageBitmap(piece); | |||
layout.addView(iv); | |||
} | |||
} | |||
}); | |||
} | |||
|
|||
private ArrayList<Bitmap> splitImage() { | |||
int piecesNumber = 12; | |||
int rows = 4; | |||
int cols = 3; | |||
|
|||
ImageView imageView = findViewById(R.id.imageView); | |||
ArrayList<Bitmap> pieces = new ArrayList<>(piecesNumber); | |||
|
|||
// Get the scaled bitmap of the source image | |||
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); | |||
Bitmap bitmap = drawable.getBitmap(); | |||
|
|||
int[] dimensions = getBitmapPositionInsideImageView(imageView); | |||
int scaledBitmapLeft = dimensions[0]; | |||
int scaledBitmapTop = dimensions[1]; | |||
int scaledBitmapWidth = dimensions[2]; | |||
int scaledBitmapHeight = dimensions[3]; | |||
|
|||
int croppedImageWidth = scaledBitmapWidth - 2 * abs(scaledBitmapLeft); | |||
int croppedImageHeight = scaledBitmapHeight - 2 * abs(scaledBitmapTop); | |||
|
|||
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledBitmapWidth, scaledBitmapHeight, true); | |||
Bitmap croppedBitmap = Bitmap.createBitmap(scaledBitmap, abs(scaledBitmapLeft), abs(scaledBitmapTop), croppedImageWidth, croppedImageHeight); | |||
|
|||
// Calculate the with and height of the pieces | |||
int pieceWidth = croppedImageWidth/cols; | |||
int pieceHeight = croppedImageHeight/rows; | |||
|
|||
// Create each bitmap piece and add it to the resulting array | |||
int yCoord = 0; | |||
for (int row = 0; row < rows; row++) { | |||
int xCoord = 0; | |||
for (int col = 0; col < cols; col++) { | |||
pieces.add(Bitmap.createBitmap(croppedBitmap, xCoord, yCoord, pieceWidth, pieceHeight)); | |||
xCoord += pieceWidth; | |||
} | |||
yCoord += pieceHeight; | |||
} | |||
|
|||
return pieces; | |||
} | |||
|
|||
private int[] getBitmapPositionInsideImageView(ImageView imageView) { | |||
int[] ret = new int[4]; | |||
|
|||
if (imageView == null || imageView.getDrawable() == null) | |||
return ret; | |||
|
|||
// Get image dimensions | |||
// Get image matrix values and place them in an array | |||
float[] f = new float[9]; | |||
imageView.getImageMatrix().getValues(f); | |||
|
|||
// Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY) | |||
final float scaleX = f[Matrix.MSCALE_X]; | |||
final float scaleY = f[Matrix.MSCALE_Y]; | |||
|
|||
// Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight) | |||
final Drawable d = imageView.getDrawable(); | |||
final int origW = d.getIntrinsicWidth(); | |||
final int origH = d.getIntrinsicHeight(); | |||
|
|||
// Calculate the actual dimensions | |||
final int actW = Math.round(origW * scaleX); | |||
final int actH = Math.round(origH * scaleY); | |||
|
|||
ret[2] = actW; | |||
ret[3] = actH; | |||
|
|||
// Get image position | |||
// We assume that the image is centered into ImageView | |||
int imgViewW = imageView.getWidth(); | |||
int imgViewH = imageView.getHeight(); | |||
|
|||
int top = (int) (imgViewH - actH)/2; | |||
int left = (int) (imgViewW - actW)/2; | |||
|
|||
ret[0] = left; | |||
ret[1] = top; | |||
|
|||
return ret; | |||
} | } | ||
} | } |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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