Skip to content

Commit 63993ef

Browse files
unknownunknown
authored andcommitted
TileSlicer refactoring and decrease memory consumption
1 parent 54c0b09 commit 63993ef

File tree

6 files changed

+101
-76
lines changed

6 files changed

+101
-76
lines changed

res/layout/main.xml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<merge xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:layout_width="fill_parent"
4-
android:layout_height="fill_parent">
5-
<cz.destil.sliderpuzzle.GameboardView android:id="@+id/gameboard"
6-
android:layout_width="match_parent"
7-
android:layout_height="match_parent">
8-
</cz.destil.sliderpuzzle.GameboardView>
9-
</merge>
2+
<cz.destil.sliderpuzzle.GameboardView xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/gameboard"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:background="@color/background" >
7+
8+
</cz.destil.sliderpuzzle.GameboardView>

res/values/colors.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<color name="background">#585858</color>
5+
<color name="tile_border">#fbfdff</color>
6+
7+
</resources>

res/values/strings.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33

4-
<string name="hello">Hello World, SliderPuzzleActivity!</string>
5-
<string name="app_name">SliderPuzzle</string>
4+
<string name="app_name">Slider Puzzle</string>
65

76
</resources>

src/cz/destil/sliderpuzzle/GameboardView.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@
2222

2323
public class GameboardView extends RelativeLayout implements OnTouchListener {
2424

25+
public static final int GRID_SIZE = 4; //4x4
2526
protected Size tileSize;
2627
protected RectF gameboardRect;
2728
protected HashSet<GameTile> tiles;
2829
protected GameTile emptyTile, movedTile;
2930
private boolean boardCreated;
3031
private PointF lastDragPoint;
31-
private TileServer tileServer;
32+
private TileSlicer tileSlicer;
3233
protected ArrayList<GameTileMotionDescriptor> currentMotionDescriptors;
3334

3435
public GameboardView(Context context, AttributeSet attrSet) {
3536
super(context, attrSet);
3637
Drawable globe = getResources().getDrawable(R.drawable.globe);
3738
Bitmap original = ((BitmapDrawable)globe).getBitmap();
38-
tileServer = new TileServer(original, 4, 4, 68);
39+
tileSlicer = new TileSlicer(original, GRID_SIZE);
3940

4041
createTiles();
4142
}
@@ -62,13 +63,13 @@ protected void placeTile(GameTile tile) {
6263
params.topMargin = tileRect.top;
6364
params.leftMargin = tileRect.left;
6465
addView(tile, params);
65-
tile.setImageBitmap(tileServer.serveRandomSlice());
66+
tile.setImageBitmap(tileSlicer.getRandomSlice());
6667
}
6768

6869
protected void createTiles() {
6970
tiles = new HashSet<GameTile>();
70-
for (int rowI=0; rowI<4; rowI++) {
71-
for (int colI=0; colI<4; colI++) {
71+
for (int rowI=0; rowI<GRID_SIZE; rowI++) {
72+
for (int colI=0; colI<GRID_SIZE; colI++) {
7273
GameTile tile = createTileAtCoordinate( new Coordinate(rowI, colI) );
7374
if (rowI == 3 && colI == 3) {
7475
emptyTile = tile;
@@ -362,13 +363,13 @@ protected void determineGameboardSizes() {
362363
int viewHeight = getHeight();
363364
int tileWidth = 0;
364365
if (viewWidth > viewHeight) {
365-
tileWidth = viewHeight/4;
366+
tileWidth = viewHeight/GRID_SIZE;
366367
} else {
367-
tileWidth = viewWidth/4;
368+
tileWidth = viewWidth/GRID_SIZE;
368369
}
369370
tileSize = new Size(tileWidth, tileWidth);
370-
int gameboardWidth = tileSize.width * 4;
371-
int gameboardHeight = tileSize.height * 4;
371+
int gameboardWidth = tileSize.width * GRID_SIZE;
372+
int gameboardHeight = tileSize.height * GRID_SIZE;
372373
int gameboardTop = viewHeight/2 - gameboardHeight/2;
373374
int gameboardLeft = viewWidth/2 - gameboardWidth/2;
374375
gameboardRect = new RectF(gameboardLeft, gameboardTop, gameboardLeft + gameboardWidth, gameboardTop + gameboardHeight);

src/cz/destil/sliderpuzzle/TileServer.java

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package cz.destil.sliderpuzzle;
2+
3+
import java.util.ArrayList;
4+
import java.util.Random;
5+
6+
import android.graphics.Bitmap;
7+
8+
/**
9+
*
10+
* Slices original bitmap into tiles and adds border. Provides randomized access
11+
* to tiles.
12+
*
13+
* Based on
14+
* https://github.com/thillerson/Android-Slider-Puzzle/blob/master/src/com
15+
* /tackmobile/TileServer.java
16+
*
17+
* @author David Vavra
18+
*/
19+
public class TileSlicer {
20+
21+
private static final String TAG = "TileSlicer";
22+
private Bitmap original;
23+
private int tileSize;
24+
private ArrayList<Bitmap> slices;
25+
private Random random;
26+
27+
/**
28+
* Initializes TileSlicer.
29+
*
30+
* @param original
31+
* Bitmap which should be sliced
32+
* @param gridSize
33+
* Grid size, for example 4 for 4x4 grid
34+
*/
35+
public TileSlicer(Bitmap original, int gridSize) {
36+
super();
37+
this.original = original;
38+
this.tileSize = original.getWidth() / gridSize;
39+
random = new Random();
40+
slices = new ArrayList<Bitmap>();
41+
sliceOriginal();
42+
}
43+
44+
/**
45+
* Slices original bitmap and adds border to slices.
46+
*/
47+
private void sliceOriginal() {
48+
int x, y;
49+
Bitmap bitmap;
50+
for (int rowI = 0; rowI < 4; rowI++) {
51+
for (int colI = 0; colI < 4; colI++) {
52+
x = rowI * tileSize;
53+
y = colI * tileSize;
54+
bitmap = Bitmap.createBitmap(original, x, y, tileSize, tileSize);
55+
slices.add(bitmap);
56+
}
57+
}
58+
// remove original bitmap from memory
59+
original = null;
60+
}
61+
62+
/**
63+
* Serves random slice and frees it from memory
64+
*
65+
* @return Bitmap of random slice
66+
*/
67+
public Bitmap getRandomSlice() {
68+
if (slices.size() > 0) {
69+
int randomIndex = random.nextInt(slices.size());
70+
Bitmap drawable = slices.remove(randomIndex);
71+
return drawable;
72+
}
73+
return null;
74+
}
75+
76+
}

0 commit comments

Comments
 (0)