質問編集履歴
2
ソースコード追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,4 +11,331 @@
|
|
11
11
|
* Android Studio2.3.1
|
12
12
|
* AVD Manager Galaxy Nexus API 25
|
13
13
|
|
14
|
-

|
14
|
+

|
15
|
+
|
16
|
+
### ソース
|
17
|
+
[Android プログラミングバイブル](http://www.socym.co.jp/book/1087) のサンプルコードです。
|
18
|
+
|
19
|
+
```java:PuzzleGame.java
|
20
|
+
package com.example.yuji3.puzzlegame;
|
21
|
+
|
22
|
+
import android.app.Activity;
|
23
|
+
import android.os.Bundle;
|
24
|
+
import android.view.Window;
|
25
|
+
import android.view.WindowManager;
|
26
|
+
|
27
|
+
public class PuzzleGame extends Activity {
|
28
|
+
|
29
|
+
@Override
|
30
|
+
protected void onCreate(Bundle savedInstanceState) {
|
31
|
+
super.onCreate(savedInstanceState);
|
32
|
+
Util.setActivity(this);
|
33
|
+
|
34
|
+
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
|
35
|
+
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
36
|
+
|
37
|
+
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
38
|
+
|
39
|
+
setContentView(new PuzzleView(this));
|
40
|
+
|
41
|
+
}
|
42
|
+
}
|
43
|
+
```
|
44
|
+
|
45
|
+
```java:PuzzleView.java
|
46
|
+
package com.example.yuji3.puzzlegame;
|
47
|
+
|
48
|
+
import android.app.Activity;
|
49
|
+
import android.graphics.Bitmap;
|
50
|
+
import android.graphics.Canvas;
|
51
|
+
import android.graphics.Color;
|
52
|
+
import android.graphics.Point;
|
53
|
+
import android.graphics.Rect;
|
54
|
+
import android.view.MotionEvent;
|
55
|
+
import android.view.View;
|
56
|
+
|
57
|
+
import lombok.EqualsAndHashCode;
|
58
|
+
|
59
|
+
/**
|
60
|
+
* Created by yuji3 on 2017/05/03.
|
61
|
+
*/
|
62
|
+
|
63
|
+
public class PuzzleView extends View {
|
64
|
+
|
65
|
+
public final static int SIZE = 4;
|
66
|
+
|
67
|
+
|
68
|
+
private final int EMPTY_NO = SIZE*SIZE-1;
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
public int W;
|
73
|
+
public int H;
|
74
|
+
|
75
|
+
|
76
|
+
private Scene scene = Scene.TITLE;
|
77
|
+
|
78
|
+
private int[][] data = new int[SIZE][SIZE];
|
79
|
+
|
80
|
+
|
81
|
+
private int shufle;
|
82
|
+
|
83
|
+
private Graphics g;
|
84
|
+
private Rect gSrc;
|
85
|
+
private Rect gDst;
|
86
|
+
|
87
|
+
private Bitmap imgBg;
|
88
|
+
private Bitmap imgFrame;
|
89
|
+
private Bitmap imgPic;
|
90
|
+
private Bitmap imgTitle;
|
91
|
+
private Bitmap imgTap;
|
92
|
+
private Bitmap imgClear;
|
93
|
+
|
94
|
+
|
95
|
+
public PuzzleView(Activity activity) {
|
96
|
+
super(activity);
|
97
|
+
|
98
|
+
imgBg = Util.res2bmp(R.drawable.bg);
|
99
|
+
imgFrame = Util.res2bmp(R.drawable.frame);
|
100
|
+
imgPic = Util.res2bmp(R.drawable.pic);
|
101
|
+
imgTitle = Util.res2bmp(R.drawable.title);
|
102
|
+
imgTap = Util.res2bmp(R.drawable.tap);
|
103
|
+
imgClear = Util.res2bmp(R.drawable.clear);
|
104
|
+
|
105
|
+
W = imgBg.getWidth();
|
106
|
+
H = imgBg.getHeight();
|
107
|
+
|
108
|
+
Point displaySize = Util.getDisplaySize();
|
109
|
+
int srcH = W * displaySize.y / displaySize.x;
|
110
|
+
|
111
|
+
int top = (H - srcH)/2;
|
112
|
+
gSrc = new Rect(0, top, W, top + srcH);
|
113
|
+
gDst = new Rect(0, 0, displaySize.x, displaySize.y);
|
114
|
+
|
115
|
+
g = new Graphics(W, H);
|
116
|
+
|
117
|
+
initScene(Scene.TITLE);
|
118
|
+
|
119
|
+
|
120
|
+
}
|
121
|
+
|
122
|
+
private void initScene(Scene scene) {
|
123
|
+
this.scene = scene;
|
124
|
+
|
125
|
+
switch (scene) {
|
126
|
+
case TITLE:
|
127
|
+
for (int i = 0; i < SIZE; i++) {
|
128
|
+
for (int j = 0; j < SIZE; j++) {
|
129
|
+
data[i][j] = i * SIZE + j;
|
130
|
+
}
|
131
|
+
}
|
132
|
+
break;
|
133
|
+
|
134
|
+
case PLAY:
|
135
|
+
shufle = 40;
|
136
|
+
while (shufle > 0) {
|
137
|
+
if (movePiece(new Cell(Util.rand(SIZE), Util.rand(SIZE)))) {
|
138
|
+
shufle--;
|
139
|
+
}
|
140
|
+
}
|
141
|
+
break;
|
142
|
+
|
143
|
+
default:
|
144
|
+
break;
|
145
|
+
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
invalidate();
|
150
|
+
}
|
151
|
+
|
152
|
+
|
153
|
+
@Override
|
154
|
+
protected void onDraw(Canvas canvas) {
|
155
|
+
int PIECE_PIXEL = imgPic.getWidth()/SIZE;
|
156
|
+
|
157
|
+
g.drawBitmap(imgBg, 0, 0);
|
158
|
+
g.drawBitmap(imgFrame, (W-imgFrame.getWidth())/2, (H-imgFrame.getHeight())/2);
|
159
|
+
|
160
|
+
int px = (W-imgPic.getWidth())/2;
|
161
|
+
int py = (H-imgPic.getHeight())/2;
|
162
|
+
|
163
|
+
// for (int )
|
164
|
+
|
165
|
+
|
166
|
+
for(int i=0; i< SIZE; i++) {
|
167
|
+
for (int j=0; j<SIZE; j++) {
|
168
|
+
|
169
|
+
if (scene != Scene.PLAY || data[i][j] != EMPTY_NO ) {
|
170
|
+
int dx = j;
|
171
|
+
int dy = i;
|
172
|
+
|
173
|
+
int sx = data[i][j] % SIZE;
|
174
|
+
int sy = data[i][j] / SIZE;
|
175
|
+
|
176
|
+
Rect src = new Rect(PIECE_PIXEL * sx, PIECE_PIXEL * sy, PIECE_PIXEL * sx + PIECE_PIXEL, PIECE_PIXEL * sy + PIECE_PIXEL);
|
177
|
+
int dstLeft = px + PIECE_PIXEL * dx;
|
178
|
+
int dstTop = py + PIECE_PIXEL * dy;
|
179
|
+
Rect dst = new Rect(dstLeft, dstTop, dstLeft + PIECE_PIXEL, dstTop + PIECE_PIXEL);
|
180
|
+
|
181
|
+
g.drawBitmap(imgPic, src, dst);
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
}
|
186
|
+
}
|
187
|
+
|
188
|
+
switch(scene) {
|
189
|
+
case TITLE:
|
190
|
+
g.drawBitmap(imgTitle, (W-imgTitle.getWidth())/2, 120);
|
191
|
+
g.drawBitmap(imgTap, (W-imgTap.getWidth())/2, 1100);
|
192
|
+
break;
|
193
|
+
case CLEAR:
|
194
|
+
g.drawBitmap(imgClear,(W-imgClear.getWidth())/2, 120);
|
195
|
+
break;
|
196
|
+
default:
|
197
|
+
break;
|
198
|
+
}
|
199
|
+
|
200
|
+
canvas.drawColor(Color.BLACK);
|
201
|
+
canvas.drawBitmap(g.getBitmap(), gSrc, gDst, null);
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
@Override
|
207
|
+
public boolean onTouchEvent(MotionEvent event) {
|
208
|
+
int PIECE_PIXEL = imgPic.getWidth()/SIZE;
|
209
|
+
|
210
|
+
int touchX = (int) (event.getX() * gSrc.width() / gDst.width());
|
211
|
+
int touchY = (int) (event.getY() * gSrc.height() / gDst.height());
|
212
|
+
|
213
|
+
if (event.getAction() == MotionEvent.ACTION_DOWN) {
|
214
|
+
switch(scene) {
|
215
|
+
case TITLE:
|
216
|
+
initScene(Scene.PLAY);
|
217
|
+
break;
|
218
|
+
|
219
|
+
case PLAY:
|
220
|
+
int px = (W-imgPic.getWidth())/2;
|
221
|
+
int py = (H - imgPic.getHeight())/2;
|
222
|
+
|
223
|
+
if (px < touchX && touchX < px + imgPic.getWidth() &&
|
224
|
+
py < touchY && touchY < py + imgPic.getHeight()) {
|
225
|
+
int tx = (touchX - px) / PIECE_PIXEL;
|
226
|
+
int ty = (touchY - py) / PIECE_PIXEL;
|
227
|
+
|
228
|
+
movePiece(new Cell(ty, tx));
|
229
|
+
|
230
|
+
clearCheck();
|
231
|
+
|
232
|
+
}
|
233
|
+
break;
|
234
|
+
|
235
|
+
case CLEAR:
|
236
|
+
initScene(Scene.TITLE);
|
237
|
+
break;
|
238
|
+
}
|
239
|
+
|
240
|
+
return true;
|
241
|
+
}
|
242
|
+
|
243
|
+
return super.onTouchEvent(event);
|
244
|
+
}
|
245
|
+
|
246
|
+
private boolean movePiece(Cell target) {
|
247
|
+
Cell empty = getEmptyCell();
|
248
|
+
if (target.equals(empty)) {
|
249
|
+
return false;
|
250
|
+
}
|
251
|
+
|
252
|
+
if ( (target.row == empty.row && Math.abs(target.col-empty.col) <= 1) ||
|
253
|
+
(target.col == empty.col && Math.abs(target.row - empty.row) <=1)) { //隣り合わせの場合
|
254
|
+
data[empty.row][empty.col] = data[target.row][target.col];
|
255
|
+
data[target.row][target.col] = EMPTY_NO;
|
256
|
+
|
257
|
+
|
258
|
+
return true;
|
259
|
+
} else {
|
260
|
+
return false;
|
261
|
+
}
|
262
|
+
}
|
263
|
+
|
264
|
+
private enum Scene {
|
265
|
+
TITLE,PLAY,CLEAR
|
266
|
+
}
|
267
|
+
|
268
|
+
private Cell getEmptyCell() {
|
269
|
+
for(int i=0; i< SIZE; i++) {
|
270
|
+
for (int j=0; j<SIZE; j++) {
|
271
|
+
if (data[i][j] == EMPTY_NO) {
|
272
|
+
return new Cell(i, j);
|
273
|
+
}
|
274
|
+
}
|
275
|
+
}
|
276
|
+
return null;
|
277
|
+
}
|
278
|
+
|
279
|
+
@EqualsAndHashCode
|
280
|
+
class Cell {
|
281
|
+
Cell(int row, int col) {
|
282
|
+
this.row = row;
|
283
|
+
this.col = col;
|
284
|
+
}
|
285
|
+
int row = 0;
|
286
|
+
int col = 0;
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
private boolean clearCheck() {
|
291
|
+
if (shufle > 0) {
|
292
|
+
return true;
|
293
|
+
}
|
294
|
+
|
295
|
+
int count = 0;
|
296
|
+
for(int i=0; i< SIZE; i++) {
|
297
|
+
for (int j=0; j<SIZE; j++) {
|
298
|
+
if (data[i][j] == i*SIZE + j) {
|
299
|
+
count++;
|
300
|
+
}
|
301
|
+
}
|
302
|
+
}
|
303
|
+
|
304
|
+
if (count == SIZE*SIZE ) {
|
305
|
+
scene = Scene.CLEAR;
|
306
|
+
invalidate();
|
307
|
+
return true;
|
308
|
+
} else {
|
309
|
+
invalidate();
|
310
|
+
return false;
|
311
|
+
}
|
312
|
+
|
313
|
+
}
|
314
|
+
}
|
315
|
+
```
|
316
|
+
|
317
|
+
```xml:AndroidManifest.xml
|
318
|
+
<?xml version="1.0" encoding="utf-8"?>
|
319
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
320
|
+
package="com.example.yuji3.puzzlegame">
|
321
|
+
|
322
|
+
<application
|
323
|
+
android:allowBackup="true"
|
324
|
+
android:icon="@mipmap/ic_launcher"
|
325
|
+
android:label="@string/app_name"
|
326
|
+
android:roundIcon="@mipmap/ic_launcher_round"
|
327
|
+
android:supportsRtl="true"
|
328
|
+
android:theme="@style/AppTheme">
|
329
|
+
<activity android:name=".PuzzleGame"
|
330
|
+
android:label="@string/app_name"
|
331
|
+
android:screenOrientation="portrait">
|
332
|
+
<intent-filter>
|
333
|
+
<action android:name="android.intent.action.MAIN" />
|
334
|
+
|
335
|
+
<category android:name="android.intent.category.LAUNCHER" />
|
336
|
+
</intent-filter>
|
337
|
+
</activity>
|
338
|
+
</application>
|
339
|
+
|
340
|
+
</manifest>
|
341
|
+
```
|
1
開発環境を追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,6 +3,12 @@
|
|
3
3
|
エミュレータで実行すると、下図のように、表示部分がずれています。
|
4
4
|
また、表示部分がずれているため、タッチしたときの座標が正しく取得できません。
|
5
5
|
|
6
|
-

|
7
7
|
|
8
|
-
どこを修正すれば、正しく表示されるでしょうか?
|
8
|
+
どこを修正すれば、正しく表示されるでしょうか?
|
9
|
+
|
10
|
+
### 開発環境
|
11
|
+
* Android Studio2.3.1
|
12
|
+
* AVD Manager Galaxy Nexus API 25
|
13
|
+
|
14
|
+

|