質問編集履歴

2

線を点にしようとしているがうまくいかない部分を追記

2018/09/27 10:13

投稿

tokutoku453
tokutoku453

スコア13

test CHANGED
File without changes
test CHANGED
@@ -8,6 +8,12 @@
8
8
 
9
9
  何かいい方法などはあるのでしょうか?
10
10
 
11
+ 9/27 19:00追記
12
+
13
+ drawingviewのDrawLineの部分をDrawPointに変えて点を打てるようにしたいのですがやっぱり線で描画されてしまいます。
14
+
15
+ Pointに変換する以外で線を引くのではなくタッチ時点で点を打てるようにする方法はあるのでしょうか
16
+
11
17
  > drawingbiew.java
12
18
 
13
19
  ```ここに言語を入力

1

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

2018/09/27 10:13

投稿

tokutoku453
tokutoku453

スコア13

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- 今、ドット絵を打てるアプリを開発してます。
1
+ 今、androidstudioでドット絵を打てるアプリを開発してます。
2
2
 
3
3
  canvas上に線を引く・消しゴムで消す・線の太さを変えるが実装されています。
4
4
 
@@ -7,3 +7,453 @@
7
7
  ですが、線の太さを変更したり、続けて描画するを変更しているのですがなぜかうまくいきません。
8
8
 
9
9
  何かいい方法などはあるのでしょうか?
10
+
11
+ > drawingbiew.java
12
+
13
+ ```ここに言語を入力
14
+
15
+ package e.toku.myapplication;
16
+
17
+
18
+
19
+ import android.content.Context;
20
+
21
+ import android.graphics.Canvas;
22
+
23
+ import android.graphics.Color;
24
+
25
+ import android.graphics.Paint;
26
+
27
+ import android.graphics.Path;
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
+ // 履歴
46
+
47
+ private List<DrawLine> lines;
48
+
49
+ // 現在、描いている線の情報
50
+
51
+ private Paint paint;
52
+
53
+ private Path path;
54
+
55
+
56
+
57
+ // 線の履歴(座標+色)
58
+
59
+ class DrawLine {
60
+
61
+ private Paint paint;
62
+
63
+ private Path path;
64
+
65
+
66
+
67
+ DrawLine(Path path, Paint paint) {
68
+
69
+ this.paint = new Paint(paint);
70
+
71
+ this.path = new Path(path);
72
+
73
+ }
74
+
75
+
76
+
77
+ void draw(Canvas canvas) {
78
+
79
+ canvas.drawPath(this.path, this.paint);
80
+
81
+ }
82
+
83
+ }
84
+
85
+
86
+
87
+ public DrawingView(Context context) {
88
+
89
+ super(context);
90
+
91
+ }
92
+
93
+
94
+
95
+ public DrawingView(Context context, AttributeSet attrs) {
96
+
97
+ super(context, attrs);
98
+
99
+
100
+
101
+ this.path = new Path();
102
+
103
+
104
+
105
+ this.paint = new Paint();
106
+
107
+ this.paint.setStyle(Paint.Style.STROKE);
108
+
109
+ this.paint.setAntiAlias(true);
110
+
111
+ //this.paint.setStrokeWidth(15);
112
+
113
+
114
+
115
+ this.lines = new ArrayList<DrawLine>();
116
+
117
+ }
118
+
119
+
120
+
121
+ @Override
122
+
123
+ protected void onDraw(Canvas canvas) {
124
+
125
+ super.onDraw(canvas);
126
+
127
+
128
+
129
+ // キャンバスをクリア
130
+
131
+ canvas.drawColor(Color.WHITE);
132
+
133
+ // 履歴から線を描画
134
+
135
+ for(DrawLine line : this.lines) {
136
+
137
+ line.draw(canvas);
138
+
139
+ }
140
+
141
+ // 現在、描いている線を描画
142
+
143
+ canvas.drawPath(this.path, this.paint);
144
+
145
+
146
+
147
+ }
148
+
149
+
150
+
151
+ @Override
152
+
153
+ public boolean onTouchEvent(MotionEvent event) {
154
+
155
+ float x = event.getX();
156
+
157
+ float y = event.getY();
158
+
159
+
160
+
161
+ switch (event.getAction()) {
162
+
163
+ case MotionEvent.ACTION_DOWN:
164
+
165
+ this.path.moveTo(x, y);
166
+
167
+ break;
168
+
169
+ case MotionEvent.ACTION_MOVE:
170
+
171
+ this.path.lineTo(x, y);
172
+
173
+ break;
174
+
175
+ case MotionEvent.ACTION_UP:
176
+
177
+ this.path.lineTo(x, y);
178
+
179
+ // 指を離したので、履歴に追加する
180
+
181
+ this.lines.add(new DrawLine(this.path, this.paint));
182
+
183
+ // パスをリセットする
184
+
185
+ // これを忘れると、全ての線の色が変わってしまう
186
+
187
+ this.path.reset();
188
+
189
+ break;
190
+
191
+ }
192
+
193
+ invalidate();
194
+
195
+ return true;
196
+
197
+ }
198
+
199
+
200
+
201
+ public void delete() {
202
+
203
+ // 履歴をクリア
204
+
205
+ this.lines.clear();
206
+
207
+ // 現在の線をクリア
208
+
209
+ this.path.reset();
210
+
211
+ invalidate();
212
+
213
+ }
214
+
215
+
216
+
217
+ public void setPen(int color){
218
+
219
+ this.paint.setColor(color);
220
+
221
+ }
222
+
223
+
224
+
225
+ public void setStrokeWidth(float width){
226
+
227
+ this.paint.setStrokeWidth(width);
228
+
229
+ }
230
+
231
+ }
232
+
233
+ ```
234
+
235
+ > mainactivty
236
+
237
+ ```ここに言語を入力
238
+
239
+ package e.toku.myapplication;
240
+
241
+
242
+
243
+ import android.app.Activity;
244
+
245
+ import android.graphics.Color;
246
+
247
+ import android.os.Bundle;
248
+
249
+ import android.os.CpuUsageInfo;
250
+
251
+ import android.support.v7.app.AppCompatActivity;
252
+
253
+ import android.view.View;
254
+
255
+ import android.view.Window;
256
+
257
+ import android.widget.Button;
258
+
259
+ import android.widget.Toast;
260
+
261
+ import android.widget.SeekBar;
262
+
263
+
264
+
265
+
266
+
267
+ public class MainActivity extends AppCompatActivity implements View.OnClickListener {
268
+
269
+ private DrawingView drawingView;
270
+
271
+ Button red_button,blue_button,yellow_button,black_button,kesigomu_button;
272
+
273
+ Button hutosa1_button,hutosa2_button;
274
+
275
+
276
+
277
+
278
+
279
+
280
+
281
+ @Override
282
+
283
+ protected void onCreate(Bundle savedInstanceState) {
284
+
285
+ super.onCreate(savedInstanceState);
286
+
287
+ supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
288
+
289
+ setContentView(R.layout.activity_main);
290
+
291
+
292
+
293
+ //線の色指定
294
+
295
+ blue_button= findViewById(R.id.blue_button);
296
+
297
+ red_button= findViewById(R.id.red_button);
298
+
299
+ yellow_button= findViewById(R.id.yellow_button) ;
300
+
301
+ black_button= findViewById(R.id.black_button);
302
+
303
+ kesigomu_button= findViewById(R.id.kesigomu_button);
304
+
305
+
306
+
307
+ //線の太さ指定
308
+
309
+ hutosa1_button= findViewById(R.id.hutosa1_button);
310
+
311
+ hutosa2_button= findViewById(R.id.hutosa2_button);
312
+
313
+
314
+
315
+
316
+
317
+ blue_button.setOnClickListener(this);
318
+
319
+ red_button.setOnClickListener(this);
320
+
321
+ yellow_button.setOnClickListener(this);
322
+
323
+ black_button.setOnClickListener(this);
324
+
325
+ kesigomu_button.setOnClickListener(this);
326
+
327
+
328
+
329
+ hutosa1_button.setOnClickListener(this);
330
+
331
+ hutosa2_button.setOnClickListener(this);
332
+
333
+
334
+
335
+
336
+
337
+
338
+
339
+
340
+
341
+
342
+
343
+ this.drawingView = findViewById(R.id.drawing_view);
344
+
345
+
346
+
347
+ findViewById(R.id.deletebutton).setOnClickListener(deleteDrawing);
348
+
349
+
350
+
351
+ }
352
+
353
+ View.OnClickListener deleteDrawing = new View.OnClickListener(){
354
+
355
+ @Override
356
+
357
+ public void onClick(View view){
358
+
359
+ drawingView.delete();
360
+
361
+ }
362
+
363
+ };
364
+
365
+
366
+
367
+
368
+
369
+ public void onClick(View v){
370
+
371
+ switch(v.getId()){
372
+
373
+ //線の色指定
374
+
375
+ case R.id.blue_button:
376
+
377
+ drawingView.setPen(Color.BLUE);
378
+
379
+ Toast.makeText(this,"blue",Toast.LENGTH_SHORT).show();
380
+
381
+ break;
382
+
383
+ case R.id.red_button:
384
+
385
+ drawingView.setPen(Color.RED);
386
+
387
+ Toast.makeText(this,"red",Toast.LENGTH_SHORT).show();
388
+
389
+ break;
390
+
391
+ case R.id.yellow_button:
392
+
393
+ drawingView.setPen(Color.YELLOW);
394
+
395
+ Toast.makeText(this,"yellow",Toast.LENGTH_SHORT).show();
396
+
397
+ break;
398
+
399
+ case R.id.black_button:
400
+
401
+ drawingView.setPen(Color.BLACK);
402
+
403
+ Toast.makeText(this,"black",Toast.LENGTH_SHORT).show();
404
+
405
+ break;
406
+
407
+ case R.id.kesigomu_button:
408
+
409
+ drawingView.setPen(Color.WHITE);
410
+
411
+ drawingView.setStrokeWidth(45);
412
+
413
+ Toast.makeText(this,"消しゴム",Toast.LENGTH_SHORT).show();
414
+
415
+ break;
416
+
417
+ //線の太さ指定
418
+
419
+ case R.id.hutosa1_button:
420
+
421
+ drawingView.setStrokeWidth(15);
422
+
423
+ Toast.makeText(this,"太さ中",Toast.LENGTH_SHORT).show();
424
+
425
+ break;
426
+
427
+ case R.id.hutosa2_button:
428
+
429
+ drawingView.setStrokeWidth(30);
430
+
431
+ Toast.makeText(this,"太さ大",Toast.LENGTH_SHORT).show();
432
+
433
+ break;
434
+
435
+
436
+
437
+ }
438
+
439
+ }
440
+
441
+
442
+
443
+ @Override
444
+
445
+ public void onPointerCaptureChanged(boolean hasCapture) {
446
+
447
+
448
+
449
+ }
450
+
451
+
452
+
453
+
454
+
455
+ }
456
+
457
+
458
+
459
+ ```