質問編集履歴

1

コードの追加 質問内容の訂正

2019/01/05 11:33

投稿

tokutoku453
tokutoku453

スコア13

test CHANGED
File without changes
test CHANGED
@@ -3,3 +3,319 @@
3
3
  drawrectを使用してタップした位置に四角形を描画したいのですがサイトや本をみてもよくわかりません
4
4
 
5
5
  分かりやすい解説やサイトなどがあると教えていただけると助かります
6
+
7
+
8
+
9
+ 1月5日20:30分追記
10
+
11
+
12
+
13
+ ```DrawingView
14
+
15
+ import android.content.Context;
16
+
17
+ import android.content.ReceiverCallNotAllowedException;
18
+
19
+ import android.graphics.Canvas;
20
+
21
+ import android.graphics.Color;
22
+
23
+ import android.graphics.Paint;
24
+
25
+ import android.graphics.Path;
26
+
27
+ import android.graphics.Rect;
28
+
29
+ import android.util.AttributeSet;
30
+
31
+ import android.view.MotionEvent;
32
+
33
+ import android.view.View;
34
+
35
+ import java.util.ArrayList;
36
+
37
+ import java.util.List;
38
+
39
+
40
+
41
+
42
+
43
+ public class DrawingView extends View {
44
+
45
+ int touchX; //タッチした際のX
46
+
47
+ int touchY; //タッチした際のY
48
+
49
+
50
+
51
+ // 履歴
52
+
53
+ private List<DrawLine> lines;
54
+
55
+ // 現在、描いている線の情報
56
+
57
+ private Paint paint;
58
+
59
+ private Path path;
60
+
61
+ private Rect rect;
62
+
63
+ private Paint paint1;
64
+
65
+
66
+
67
+ // 線の履歴(座標+色)
68
+
69
+ class DrawLine {
70
+
71
+ private Paint paint;
72
+
73
+ private Path path;
74
+
75
+ private Rect rect;
76
+
77
+ private Paint paint1;
78
+
79
+
80
+
81
+
82
+
83
+ DrawLine(Path path, Paint paint, Rect rect) {
84
+
85
+ this.paint = new Paint(paint);
86
+
87
+ this.path = new Path(path);
88
+
89
+ this.rect = new Rect(rect);
90
+
91
+ //this.paint1 = new Paint(paint);
92
+
93
+ }
94
+
95
+
96
+
97
+
98
+
99
+ void draw(Canvas canvas) {
100
+
101
+ canvas.drawPath(this.path, this.paint);
102
+
103
+ }
104
+
105
+ }
106
+
107
+
108
+
109
+ public DrawingView(Context context) {
110
+
111
+ super(context);
112
+
113
+ }
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+ public DrawingView(Context context, AttributeSet attrs) {
122
+
123
+ super(context, attrs);
124
+
125
+
126
+
127
+ this.path = new Path();
128
+
129
+
130
+
131
+ this.paint = new Paint();
132
+
133
+
134
+
135
+ this.rect = new Rect();
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+ this.paint.setStyle(Paint.Style.STROKE);
144
+
145
+ // this.paint.setStrokeJoin(Paint.Join.MITER);//これがあると角が丸くなる
146
+
147
+ this.paint.setStrokeCap(Paint.Cap.SQUARE);
148
+
149
+ this.paint.setAntiAlias(true);
150
+
151
+ this.paint.setStrokeWidth(30);
152
+
153
+
154
+
155
+
156
+
157
+ this.lines = new ArrayList<DrawLine>();
158
+
159
+
160
+
161
+
162
+
163
+ }
164
+
165
+
166
+
167
+ @Override
168
+
169
+ protected void onDraw(Canvas canvas) {
170
+
171
+ super.onDraw(canvas);
172
+
173
+
174
+
175
+
176
+
177
+ // キャンバスをクリア
178
+
179
+ canvas.drawColor(Color.WHITE);
180
+
181
+ // 履歴から線を描画
182
+
183
+ for (DrawLine line : this.lines) {
184
+
185
+ line.draw(canvas);
186
+
187
+ }
188
+
189
+ // 現在、描いている線を描画
190
+
191
+ canvas.drawPath(this.path, this.paint);
192
+
193
+
194
+
195
+ Paint paint1 = new Paint(Color.WHITE);
196
+
197
+ paint1.setColor(Color.BLACK);
198
+
199
+ paint1.setStyle(Paint.Style.STROKE);//ペン先に合わせて色が一緒に代わってしまうので保留
200
+
201
+
202
+
203
+ for (int i = 0; i < 101; i++) {
204
+
205
+ for (int j = 0; j < 101; j++) {
206
+
207
+ int a = 30;
208
+
209
+ Rect rect = new Rect(a * i, a * j, a * (i + j) - 1, a * (i + j) - 1);
210
+
211
+
212
+
213
+ canvas.drawRect(rect, paint1);
214
+
215
+
216
+
217
+ }
218
+
219
+ }
220
+
221
+ }
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+ @Override
230
+
231
+ public boolean onTouchEvent(MotionEvent event) {
232
+
233
+ float x = event.getX();
234
+
235
+ float y = event.getY();
236
+
237
+
238
+
239
+ switch (event.getAction()) {
240
+
241
+ case MotionEvent.ACTION_DOWN:
242
+
243
+ this.path.moveTo(x, y);
244
+
245
+ this.path.lineTo(x, y);
246
+
247
+ break;
248
+
249
+ case MotionEvent.ACTION_MOVE:
250
+
251
+ this.path.lineTo(x, y);
252
+
253
+ break;
254
+
255
+ case MotionEvent.ACTION_UP:
256
+
257
+ this.path.lineTo(x, y);
258
+
259
+ // 指を離したので、履歴に追加する
260
+
261
+ this.lines.add(new DrawLine(this.path, this.paint, this.rect));
262
+
263
+ // パスをリセットする
264
+
265
+ // これを忘れると、全ての線の色が変わってしまう
266
+
267
+ this.path.reset();
268
+
269
+ break;
270
+
271
+
272
+
273
+ }
274
+
275
+ invalidate();
276
+
277
+ return true;
278
+
279
+ }
280
+
281
+
282
+
283
+ public void delete() {
284
+
285
+ // 履歴をクリア
286
+
287
+ this.lines.clear();
288
+
289
+ // 現在の線をクリア
290
+
291
+ this.path.reset();
292
+
293
+ invalidate();
294
+
295
+ }
296
+
297
+
298
+
299
+ public void setPen(int color){
300
+
301
+ this.paint.setColor(color);
302
+
303
+ }
304
+
305
+
306
+
307
+ public void setStrokeWidth(float width){
308
+
309
+ this.paint.setStrokeWidth(width);
310
+
311
+
312
+
313
+ }
314
+
315
+ }
316
+
317
+
318
+
319
+ ```
320
+
321
+ こちら該当箇所にコードを埋め込んで確認してみましたが、線を引く処理が優先されているのか、アプリケーションエラーでエミュレータが起動しなくなってしまいました。