質問編集履歴
2
ソース用コードに修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,8 +3,11 @@
|
|
3
3
|
###試したこと
|
4
4
|
GridLayoutでテキストを並べてみたけどなんか微調整がうまくいかず。。。
|
5
5
|
(Java勉強のためJavaの方でタイルを並べたい)
|
6
|
+
|
6
7
|
###該当のソースコード
|
8
|
+
```package com.a2048example.a2048;
|
7
9
|
|
10
|
+
|
8
11
|
import android.support.v7.app.AppCompatActivity;
|
9
12
|
import android.os.Bundle;
|
10
13
|
import android.view.View;
|
@@ -73,7 +76,7 @@
|
|
73
76
|
paint.setColor(Color.RED);
|
74
77
|
paint.setAntiAlias(true);
|
75
78
|
canvas.drawRect(160, 300, 100, 400, paint);
|
76
|
-
canvas.drawText("2", 50,
|
79
|
+
canvas.drawText("2", 50, 60, paint);
|
77
80
|
|
78
81
|
case 3:
|
79
82
|
// 数字パネル
|
@@ -107,7 +110,7 @@
|
|
107
110
|
paint.setAntiAlias(true);
|
108
111
|
canvas.drawRect(80, 300, 1000, 1280, paint);
|
109
112
|
|
110
|
-
// 縦線(縦)
|
113
|
+
// 縦線(縦ドセン)
|
111
114
|
paint.setStrokeWidth(StrokeWidth1);
|
112
115
|
paint.setColor(Color.GRAY);
|
113
116
|
// (x1,y1,x2,y2,paint) 始点の座標(x1,y1), 終点の座標(x2,y2)
|
@@ -199,4 +202,6 @@
|
|
199
202
|
return false;
|
200
203
|
}
|
201
204
|
|
202
|
-
}
|
205
|
+
}
|
206
|
+
|
207
|
+
```
|
1
ソースの追加と
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,6 +2,201 @@
|
|
2
2
|
AndroidStudioで2048のようなゲームを勉強がてらに作っているのですがマス目の中にどうやってタイルを生成させたらいいか方法が思いつかず質問させていただきました。
|
3
3
|
###試したこと
|
4
4
|
GridLayoutでテキストを並べてみたけどなんか微調整がうまくいかず。。。
|
5
|
+
(Java勉強のためJavaの方でタイルを並べたい)
|
6
|
+
###該当のソースコード
|
5
7
|
|
8
|
+
import android.support.v7.app.AppCompatActivity;
|
9
|
+
import android.os.Bundle;
|
10
|
+
import android.view.View;
|
6
|
-
|
11
|
+
import android.view.GestureDetector;
|
12
|
+
import android.view.MotionEvent;
|
13
|
+
import android.graphics.Canvas;
|
14
|
+
import android.graphics.Color;
|
15
|
+
import android.content.Context;
|
7
|
-
|
16
|
+
import android.util.Log;
|
17
|
+
import android.widget.TextView;
|
18
|
+
import android.graphics.Paint;
|
19
|
+
import java.util.Random;
|
20
|
+
|
21
|
+
|
22
|
+
public class NaitiveActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
|
23
|
+
|
24
|
+
GestureDetector gestureDetector;
|
25
|
+
TextView tv;
|
26
|
+
private final int THRESHOLD = 40;
|
27
|
+
|
28
|
+
|
29
|
+
private static final int StrokeWidth1 = 20;
|
30
|
+
private static final int StrokeWidth2 = 30;
|
31
|
+
//Canvas中心点
|
32
|
+
private float xc = 0.0f;
|
33
|
+
private float yc = 0.0f;
|
34
|
+
|
35
|
+
@Override
|
36
|
+
protected void onCreate(Bundle savedInstanceState) {
|
37
|
+
super.onCreate(savedInstanceState);
|
38
|
+
setContentView(R.layout.activity_naitive);
|
39
|
+
//インスタンスの生成
|
40
|
+
DrawView view = new DrawView(this);
|
41
|
+
setContentView(view);
|
42
|
+
gestureDetector = new GestureDetector(this, this);
|
43
|
+
|
44
|
+
}
|
45
|
+
|
46
|
+
class DrawView extends View {
|
47
|
+
Paint paint;
|
48
|
+
Random randInit1 = new Random();
|
49
|
+
//Random randInit2 = new Random();
|
50
|
+
public DrawView(Context context){
|
51
|
+
super(context);
|
52
|
+
paint = new Paint();
|
53
|
+
}
|
54
|
+
|
55
|
+
@Override
|
56
|
+
protected void onDraw(Canvas canvas){
|
57
|
+
|
58
|
+
// Canvas 中心点
|
59
|
+
//xc = canvas.getWidth()/2;
|
60
|
+
//yc = canvas.getHeight()/2;
|
61
|
+
//初期2パネル生成用ランダム変数(まだ同じ数字が出る)
|
62
|
+
for(int i = 0;i < 1; i++) {
|
63
|
+
int initRand1 = randInit1.nextInt(15);
|
64
|
+
Log.d("randInit1", "RANDOM:" + initRand1);
|
65
|
+
switch(2) {
|
66
|
+
case 1:
|
67
|
+
// 数字パネル
|
68
|
+
paint.setColor(Color.RED);
|
69
|
+
canvas.drawText("書きたい文字", 500, 400, paint);
|
70
|
+
|
71
|
+
case 2:
|
72
|
+
// 数字パネル
|
73
|
+
paint.setColor(Color.RED);
|
74
|
+
paint.setAntiAlias(true);
|
75
|
+
canvas.drawRect(160, 300, 100, 400, paint);
|
76
|
+
canvas.drawText("2", 50, 50, paint);
|
77
|
+
|
78
|
+
case 3:
|
79
|
+
// 数字パネル
|
80
|
+
paint.setColor(Color.RED);
|
81
|
+
paint.setAntiAlias(true);
|
82
|
+
canvas.drawRect(240, 300, 100, 400, paint);
|
83
|
+
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
// 数字パネル
|
89
|
+
paint.setColor(Color.RED);
|
90
|
+
paint.setStyle(Paint.Style.STROKE);
|
91
|
+
paint.setStrokeWidth(StrokeWidth2);
|
92
|
+
paint.setAntiAlias(true);
|
93
|
+
canvas.drawRect(80, 300, 1000, 1280, paint);
|
94
|
+
|
95
|
+
// 内側塗り
|
96
|
+
paint.setColor(Color.argb(255, 192, 192, 192));
|
97
|
+
paint.setStyle(Paint.Style.FILL_AND_STROKE);
|
98
|
+
paint.setStrokeWidth(StrokeWidth2);
|
99
|
+
paint.setAntiAlias(true);
|
100
|
+
// (x1,y1,x2,y2,paint) 左上の座標(x1,y1), 右下の座標(x2,y2)
|
101
|
+
canvas.drawRect(80, 300, 1000, 1280, paint);
|
102
|
+
|
103
|
+
// 枠
|
104
|
+
paint.setColor(Color.GRAY);
|
105
|
+
paint.setStyle(Paint.Style.STROKE);
|
106
|
+
paint.setStrokeWidth(StrokeWidth2);
|
107
|
+
paint.setAntiAlias(true);
|
108
|
+
canvas.drawRect(80, 300, 1000, 1280, paint);
|
109
|
+
|
110
|
+
// 縦線(縦)
|
111
|
+
paint.setStrokeWidth(StrokeWidth1);
|
112
|
+
paint.setColor(Color.GRAY);
|
113
|
+
// (x1,y1,x2,y2,paint) 始点の座標(x1,y1), 終点の座標(x2,y2)
|
114
|
+
canvas.drawLine(545, 300, 545, 1280, paint);
|
115
|
+
|
116
|
+
// 縦線(右)
|
117
|
+
paint.setStrokeWidth(StrokeWidth1);
|
118
|
+
paint.setColor(Color.GRAY);
|
119
|
+
// (x1,y1,x2,y2,paint) 始点の座標(x1,y1), 終点の座標(x2,y2)
|
120
|
+
canvas.drawLine(780, 300, 780, 1280, paint);
|
121
|
+
|
122
|
+
// 縦線(左)
|
123
|
+
paint.setStrokeWidth(StrokeWidth1);
|
124
|
+
paint.setColor(Color.GRAY);
|
125
|
+
// (x1,y1,x2,y2,paint) 始点の座標(x1,y1), 終点の座標(x2,y2)
|
126
|
+
canvas.drawLine(320, 300, 320, 1280, paint);
|
127
|
+
|
128
|
+
// 線(横真ん中)
|
129
|
+
paint.setStrokeWidth(StrokeWidth1);
|
130
|
+
paint.setColor(Color.GRAY);
|
131
|
+
// (x1,y1,x2,y2,paint) 始点の座標(x1,y1), 終点の座標(x2,y2)
|
132
|
+
canvas.drawLine(80, 800, 1000, 800, paint);
|
133
|
+
|
134
|
+
// 線(横上部)
|
135
|
+
paint.setStrokeWidth(StrokeWidth1);
|
136
|
+
paint.setColor(Color.GRAY);
|
137
|
+
// (x1,y1,x2,y2,paint) 始点の座標(x1,y1), 終点の座標(x2,y2)
|
138
|
+
canvas.drawLine(80, 551, 1000, 551, paint);
|
139
|
+
|
140
|
+
// 線(横下部)
|
141
|
+
paint.setStrokeWidth(StrokeWidth1);
|
142
|
+
paint.setColor(Color.GRAY);
|
143
|
+
// (x1,y1,x2,y2,paint) 始点の座標(x1,y1), 終点の座標(x2,y2)
|
144
|
+
canvas.drawLine(80, 1035, 1000, 1035, paint);
|
145
|
+
|
146
|
+
}
|
147
|
+
}
|
148
|
+
public boolean onTouchEvent(MotionEvent event) {
|
149
|
+
gestureDetector.onTouchEvent(event);
|
150
|
+
return true;
|
151
|
+
}
|
152
|
+
|
153
|
+
@Override
|
154
|
+
public boolean onDown(MotionEvent e) {
|
155
|
+
// TODO 自動生成されたメソッド・スタブ
|
156
|
+
return false;
|
157
|
+
}
|
158
|
+
|
159
|
+
@Override
|
160
|
+
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
161
|
+
Log.d("onFling", "start");
|
162
|
+
|
163
|
+
if (e2.getX() - e1.getX() > THRESHOLD) {
|
164
|
+
Log.d("onFling", "右");
|
165
|
+
} else if (e2.getX() - e1.getX() < -THRESHOLD) {
|
166
|
+
Log.d("onFling", "左");
|
167
|
+
} else if (e2.getY() - e1.getY() > THRESHOLD) {
|
168
|
+
Log.d("onFling", "下");
|
169
|
+
} else if (e2.getY() - e1.getY() < -THRESHOLD) {
|
170
|
+
Log.d("onFling", "上");
|
171
|
+
}
|
172
|
+
|
173
|
+
return false;
|
174
|
+
}
|
175
|
+
|
176
|
+
|
177
|
+
@Override
|
178
|
+
public void onLongPress(MotionEvent e) {
|
179
|
+
// TODO 自動生成されたメソッド・スタブ
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
@Override
|
184
|
+
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
|
185
|
+
// TODO 自動生成されたメソッド・スタブ
|
186
|
+
return false;
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
@Override
|
191
|
+
public void onShowPress(MotionEvent e) {
|
192
|
+
// TODO 自動生成されたメソッド・スタブ
|
193
|
+
|
194
|
+
}
|
195
|
+
|
196
|
+
@Override
|
197
|
+
public boolean onSingleTapUp(MotionEvent e) {
|
198
|
+
// TODO 自動生成されたメソッド・スタブ
|
199
|
+
return false;
|
200
|
+
}
|
201
|
+
|
202
|
+
}
|