Skip to content

Commit de6fe4f

Browse files
committed
Adding More Images
1 parent abf3850 commit de6fe4f

12 files changed

+472
-209
lines changed

Diff for: app/src/main/AndroidManifest.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/AppTheme">
12-
<activity android:name=".MainActivity"
12+
<activity
13+
android:name=".MainActivity"
1314
android:screenOrientation="portrait">
1415
<intent-filter>
1516
<action android:name="android.intent.action.MAIN" />
1617

1718
<category android:name="android.intent.category.LAUNCHER" />
1819
</intent-filter>
1920
</activity>
21+
<activity android:name=".PuzzleActivity"></activity>
2022
</application>
2123

2224
</manifest>
3.54 MB
Loading
122 KB
Loading
2.25 MB
Loading
706 KB
Loading
4.39 MB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package dragosholban.com.androidpuzzlegame;
2+
3+
import android.content.Context;
4+
import android.content.res.AssetManager;
5+
import android.graphics.Bitmap;
6+
import android.graphics.BitmapFactory;
7+
import android.graphics.Rect;
8+
import android.os.AsyncTask;
9+
import android.view.LayoutInflater;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
import android.widget.BaseAdapter;
13+
import android.widget.ImageView;
14+
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
18+
public class ImageAdapter extends BaseAdapter {
19+
private Context mContext;
20+
private AssetManager am;
21+
private String[] files;
22+
23+
public ImageAdapter(Context c) {
24+
mContext = c;
25+
am = mContext.getAssets();
26+
try {
27+
files = am.list("img");
28+
} catch (IOException e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
33+
public int getCount() {
34+
return files.length;
35+
}
36+
37+
public Object getItem(int position) {
38+
return null;
39+
}
40+
41+
public long getItemId(int position) {
42+
return 0;
43+
}
44+
45+
// create a new ImageView for each item referenced by the Adapter
46+
public View getView(final int position, View convertView, ViewGroup parent) {
47+
if (convertView == null) {
48+
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
49+
convertView = layoutInflater.inflate(R.layout.grid_element, null);
50+
}
51+
52+
final ImageView imageView = convertView.findViewById(R.id.gridImageview);
53+
imageView.setImageBitmap(null);
54+
// run image related code after the view was laid out
55+
imageView.post(new Runnable() {
56+
@Override
57+
public void run() {
58+
new AsyncTask<Void, Void, Void>() {
59+
private Bitmap bitmap;
60+
@Override
61+
protected Void doInBackground(Void... voids) {
62+
bitmap = getPicFromAsset(imageView, files[position]);
63+
return null;
64+
}
65+
66+
@Override
67+
protected void onPostExecute(Void aVoid) {
68+
super.onPostExecute(aVoid);
69+
imageView.setImageBitmap(bitmap);
70+
}
71+
}.execute();
72+
}
73+
});
74+
75+
return convertView;
76+
}
77+
78+
private Bitmap getPicFromAsset(ImageView imageView, String assetName) {
79+
// Get the dimensions of the View
80+
int targetW = imageView.getWidth();
81+
int targetH = imageView.getHeight();
82+
83+
if(targetW == 0 || targetH == 0) {
84+
// view has no dimensions set
85+
return null;
86+
}
87+
88+
try {
89+
InputStream is = am.open("img/" + assetName);
90+
// Get the dimensions of the bitmap
91+
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
92+
bmOptions.inJustDecodeBounds = true;
93+
BitmapFactory.decodeStream(is, new Rect(-1, -1, -1, -1), bmOptions);
94+
int photoW = bmOptions.outWidth;
95+
int photoH = bmOptions.outHeight;
96+
97+
// Determine how much to scale down the image
98+
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
99+
100+
is.reset();
101+
102+
// Decode the image file into a Bitmap sized to fill the View
103+
bmOptions.inJustDecodeBounds = false;
104+
bmOptions.inSampleSize = scaleFactor;
105+
bmOptions.inPurgeable = true;
106+
107+
return BitmapFactory.decodeStream(is, new Rect(-1, -1, -1, -1), bmOptions);
108+
} catch (IOException e) {
109+
e.printStackTrace();
110+
111+
return null;
112+
}
113+
}
114+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dragosholban.com.androidpuzzlegame;
22

3+
import android.content.Intent;
4+
import android.content.res.AssetManager;
35
import android.graphics.Bitmap;
46
import android.graphics.Canvas;
57
import android.graphics.Matrix;
@@ -12,212 +14,41 @@
1214
import android.support.constraint.ConstraintLayout;
1315
import android.support.v7.app.AppCompatActivity;
1416
import android.os.Bundle;
17+
import android.view.View;
18+
import android.widget.AdapterView;
19+
import android.widget.GridView;
1520
import android.widget.ImageView;
1621
import android.widget.RelativeLayout;
22+
import android.widget.Toast;
1723

24+
import java.io.IOException;
1825
import java.util.ArrayList;
1926

2027
import static java.lang.Math.abs;
2128

2229
public class MainActivity extends AppCompatActivity {
23-
ArrayList<PuzzlePiece> pieces;
2430

2531
@Override
2632
protected void onCreate(Bundle savedInstanceState) {
2733
super.onCreate(savedInstanceState);
2834
setContentView(R.layout.activity_main);
2935

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);
8648
}
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);
17852
}
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;
22253
}
22354
}

0 commit comments

Comments
 (0)