|
1 | 1 | package dragosholban.com.androidpuzzlegame;
|
2 | 2 |
|
| 3 | +import android.content.Intent; |
| 4 | +import android.content.res.AssetManager; |
3 | 5 | import android.graphics.Bitmap;
|
4 | 6 | import android.graphics.Canvas;
|
5 | 7 | import android.graphics.Matrix;
|
|
12 | 14 | import android.support.constraint.ConstraintLayout;
|
13 | 15 | import android.support.v7.app.AppCompatActivity;
|
14 | 16 | import android.os.Bundle;
|
| 17 | +import android.view.View; |
| 18 | +import android.widget.AdapterView; |
| 19 | +import android.widget.GridView; |
15 | 20 | import android.widget.ImageView;
|
16 | 21 | import android.widget.RelativeLayout;
|
| 22 | +import android.widget.Toast; |
17 | 23 |
|
| 24 | +import java.io.IOException; |
18 | 25 | import java.util.ArrayList;
|
19 | 26 |
|
20 | 27 | import static java.lang.Math.abs;
|
21 | 28 |
|
22 | 29 | public class MainActivity extends AppCompatActivity {
|
23 |
| - ArrayList<PuzzlePiece> pieces; |
24 | 30 |
|
25 | 31 | @Override
|
26 | 32 | protected void onCreate(Bundle savedInstanceState) {
|
27 | 33 | super.onCreate(savedInstanceState);
|
28 | 34 | setContentView(R.layout.activity_main);
|
29 | 35 |
|
30 |
| - final RelativeLayout layout = findViewById(R.id.layout); |
31 |
| - ImageView imageView = findViewById(R.id.imageView); |
32 |
| - |
33 |
| - // run image related code after the view was laid out |
34 |
| - // to have all dimensions calculated |
35 |
| - imageView.post(new Runnable() { |
36 |
| - @Override |
37 |
| - public void run() { |
38 |
| - pieces = splitImage(); |
39 |
| - TouchListener touchListener = new TouchListener(); |
40 |
| - for(PuzzlePiece piece : pieces) { |
41 |
| - piece.setOnTouchListener(touchListener); |
42 |
| - layout.addView(piece); |
43 |
| - } |
44 |
| - } |
45 |
| - }); |
46 |
| - } |
47 |
| - |
48 |
| - private ArrayList<PuzzlePiece> splitImage() { |
49 |
| - int piecesNumber = 12; |
50 |
| - int rows = 4; |
51 |
| - int cols = 3; |
52 |
| - |
53 |
| - ImageView imageView = findViewById(R.id.imageView); |
54 |
| - ArrayList<PuzzlePiece> pieces = new ArrayList<>(piecesNumber); |
55 |
| - |
56 |
| - // Get the scaled bitmap of the source image |
57 |
| - BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); |
58 |
| - Bitmap bitmap = drawable.getBitmap(); |
59 |
| - |
60 |
| - int[] dimensions = getBitmapPositionInsideImageView(imageView); |
61 |
| - int scaledBitmapLeft = dimensions[0]; |
62 |
| - int scaledBitmapTop = dimensions[1]; |
63 |
| - int scaledBitmapWidth = dimensions[2]; |
64 |
| - int scaledBitmapHeight = dimensions[3]; |
65 |
| - |
66 |
| - int croppedImageWidth = scaledBitmapWidth - 2 * abs(scaledBitmapLeft); |
67 |
| - int croppedImageHeight = scaledBitmapHeight - 2 * abs(scaledBitmapTop); |
68 |
| - |
69 |
| - Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledBitmapWidth, scaledBitmapHeight, true); |
70 |
| - Bitmap croppedBitmap = Bitmap.createBitmap(scaledBitmap, abs(scaledBitmapLeft), abs(scaledBitmapTop), croppedImageWidth, croppedImageHeight); |
71 |
| - |
72 |
| - // Calculate the with and height of the pieces |
73 |
| - int pieceWidth = croppedImageWidth/cols; |
74 |
| - int pieceHeight = croppedImageHeight/rows; |
75 |
| - |
76 |
| - // Create each bitmap piece and add it to the resulting array |
77 |
| - int yCoord = 0; |
78 |
| - for (int row = 0; row < rows; row++) { |
79 |
| - int xCoord = 0; |
80 |
| - for (int col = 0; col < cols; col++) { |
81 |
| - // calculate offset for each piece |
82 |
| - int offsetX = 0; |
83 |
| - int offsetY = 0; |
84 |
| - if (col > 0) { |
85 |
| - offsetX = pieceWidth / 3; |
| 36 | + AssetManager am = getAssets(); |
| 37 | + try { |
| 38 | + final String[] files = am.list("img"); |
| 39 | + |
| 40 | + GridView grid = findViewById(R.id.grid); |
| 41 | + grid.setAdapter(new ImageAdapter(this)); |
| 42 | + grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 43 | + @Override |
| 44 | + public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { |
| 45 | + Intent intent = new Intent(getApplicationContext(), PuzzleActivity.class); |
| 46 | + intent.putExtra("assetName", files[i % files.length]); |
| 47 | + startActivity(intent); |
86 | 48 | }
|
87 |
| - if (row > 0) { |
88 |
| - offsetY = pieceHeight / 3; |
89 |
| - } |
90 |
| - |
91 |
| - // apply the offset to each piece |
92 |
| - Bitmap pieceBitmap = Bitmap.createBitmap(croppedBitmap, xCoord - offsetX, yCoord - offsetY, pieceWidth + offsetX, pieceHeight + offsetY); |
93 |
| - PuzzlePiece piece = new PuzzlePiece(getApplicationContext()); |
94 |
| - piece.setImageBitmap(pieceBitmap); |
95 |
| - piece.xCoord = xCoord - offsetX + imageView.getLeft(); |
96 |
| - piece.yCoord = yCoord - offsetY + imageView.getTop(); |
97 |
| - piece.pieceWidth = pieceWidth + offsetX; |
98 |
| - piece.pieceHeight = pieceHeight + offsetY; |
99 |
| - |
100 |
| - // this bitmap will hold our final puzzle piece image |
101 |
| - Bitmap puzzlePiece = Bitmap.createBitmap(pieceWidth + offsetX, pieceHeight + offsetY, Bitmap.Config.ARGB_8888); |
102 |
| - |
103 |
| - // draw path |
104 |
| - int bumpSize = pieceHeight / 4; |
105 |
| - Canvas canvas = new Canvas(puzzlePiece); |
106 |
| - Path path = new Path(); |
107 |
| - path.moveTo(offsetX, offsetY); |
108 |
| - if (row == 0) { |
109 |
| - // top side piece |
110 |
| - path.lineTo(pieceBitmap.getWidth(), offsetY); |
111 |
| - } else { |
112 |
| - // top bump |
113 |
| - path.lineTo(offsetX + (pieceBitmap.getWidth() - offsetX) / 3, offsetY); |
114 |
| - path.cubicTo(offsetX + (pieceBitmap.getWidth() - offsetX) / 6, offsetY - bumpSize, offsetX + (pieceBitmap.getWidth() - offsetX) / 6 * 5, offsetY - bumpSize, offsetX + (pieceBitmap.getWidth() - offsetX) / 3 * 2, offsetY); |
115 |
| - path.lineTo(pieceBitmap.getWidth(), offsetY); |
116 |
| - } |
117 |
| - |
118 |
| - if (col == cols - 1) { |
119 |
| - // right side piece |
120 |
| - path.lineTo(pieceBitmap.getWidth(), pieceBitmap.getHeight()); |
121 |
| - } else { |
122 |
| - // right bump |
123 |
| - path.lineTo(pieceBitmap.getWidth(), offsetY + (pieceBitmap.getHeight() - offsetY) / 3); |
124 |
| - path.cubicTo(pieceBitmap.getWidth() - bumpSize,offsetY + (pieceBitmap.getHeight() - offsetY) / 6, pieceBitmap.getWidth() - bumpSize, offsetY + (pieceBitmap.getHeight() - offsetY) / 6 * 5, pieceBitmap.getWidth(), offsetY + (pieceBitmap.getHeight() - offsetY) / 3 * 2); |
125 |
| - path.lineTo(pieceBitmap.getWidth(), pieceBitmap.getHeight()); |
126 |
| - } |
127 |
| - |
128 |
| - if (row == rows - 1) { |
129 |
| - // bottom side piece |
130 |
| - path.lineTo(offsetX, pieceBitmap.getHeight()); |
131 |
| - } else { |
132 |
| - // bottom bump |
133 |
| - path.lineTo(offsetX + (pieceBitmap.getWidth() - offsetX) / 3 * 2, pieceBitmap.getHeight()); |
134 |
| - path.cubicTo(offsetX + (pieceBitmap.getWidth() - offsetX) / 6 * 5,pieceBitmap.getHeight() - bumpSize, offsetX + (pieceBitmap.getWidth() - offsetX) / 6, pieceBitmap.getHeight() - bumpSize, offsetX + (pieceBitmap.getWidth() - offsetX) / 3, pieceBitmap.getHeight()); |
135 |
| - path.lineTo(offsetX, pieceBitmap.getHeight()); |
136 |
| - } |
137 |
| - |
138 |
| - if (col == 0) { |
139 |
| - // left side piece |
140 |
| - path.close(); |
141 |
| - } else { |
142 |
| - // left bump |
143 |
| - path.lineTo(offsetX, offsetY + (pieceBitmap.getHeight() - offsetY) / 3 * 2); |
144 |
| - path.cubicTo(offsetX - bumpSize, offsetY + (pieceBitmap.getHeight() - offsetY) / 6 * 5, offsetX - bumpSize, offsetY + (pieceBitmap.getHeight() - offsetY) / 6, offsetX, offsetY + (pieceBitmap.getHeight() - offsetY) / 3); |
145 |
| - path.close(); |
146 |
| - } |
147 |
| - |
148 |
| - // mask the piece |
149 |
| - Paint paint = new Paint(); |
150 |
| - paint.setColor(0XFF000000); |
151 |
| - paint.setStyle(Paint.Style.FILL); |
152 |
| - |
153 |
| - canvas.drawPath(path, paint); |
154 |
| - paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); |
155 |
| - canvas.drawBitmap(pieceBitmap, 0, 0, paint); |
156 |
| - |
157 |
| - // draw a white border |
158 |
| - Paint border = new Paint(); |
159 |
| - border.setColor(0X80FFFFFF); |
160 |
| - border.setStyle(Paint.Style.STROKE); |
161 |
| - border.setStrokeWidth(8.0f); |
162 |
| - canvas.drawPath(path, border); |
163 |
| - |
164 |
| - // draw a black border |
165 |
| - border = new Paint(); |
166 |
| - border.setColor(0X80000000); |
167 |
| - border.setStyle(Paint.Style.STROKE); |
168 |
| - border.setStrokeWidth(3.0f); |
169 |
| - canvas.drawPath(path, border); |
170 |
| - |
171 |
| - // set the resulting bitmap to the piece |
172 |
| - piece.setImageBitmap(puzzlePiece); |
173 |
| - |
174 |
| - pieces.add(piece); |
175 |
| - xCoord += pieceWidth; |
176 |
| - } |
177 |
| - yCoord += pieceHeight; |
| 49 | + }); |
| 50 | + } catch (IOException e) { |
| 51 | + Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT); |
178 | 52 | }
|
179 |
| - |
180 |
| - return pieces; |
181 |
| - } |
182 |
| - |
183 |
| - private int[] getBitmapPositionInsideImageView(ImageView imageView) { |
184 |
| - int[] ret = new int[4]; |
185 |
| - |
186 |
| - if (imageView == null || imageView.getDrawable() == null) |
187 |
| - return ret; |
188 |
| - |
189 |
| - // Get image dimensions |
190 |
| - // Get image matrix values and place them in an array |
191 |
| - float[] f = new float[9]; |
192 |
| - imageView.getImageMatrix().getValues(f); |
193 |
| - |
194 |
| - // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY) |
195 |
| - final float scaleX = f[Matrix.MSCALE_X]; |
196 |
| - final float scaleY = f[Matrix.MSCALE_Y]; |
197 |
| - |
198 |
| - // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight) |
199 |
| - final Drawable d = imageView.getDrawable(); |
200 |
| - final int origW = d.getIntrinsicWidth(); |
201 |
| - final int origH = d.getIntrinsicHeight(); |
202 |
| - |
203 |
| - // Calculate the actual dimensions |
204 |
| - final int actW = Math.round(origW * scaleX); |
205 |
| - final int actH = Math.round(origH * scaleY); |
206 |
| - |
207 |
| - ret[2] = actW; |
208 |
| - ret[3] = actH; |
209 |
| - |
210 |
| - // Get image position |
211 |
| - // We assume that the image is centered into ImageView |
212 |
| - int imgViewW = imageView.getWidth(); |
213 |
| - int imgViewH = imageView.getHeight(); |
214 |
| - |
215 |
| - int top = (int) (imgViewH - actH)/2; |
216 |
| - int left = (int) (imgViewW - actW)/2; |
217 |
| - |
218 |
| - ret[0] = left; |
219 |
| - ret[1] = top; |
220 |
| - |
221 |
| - return ret; |
222 | 53 | }
|
223 | 54 | }
|
0 commit comments