回答編集履歴
4
deleteで全ての線が消えるようにしました
test
CHANGED
@@ -1,134 +1,4 @@
|
|
1
|
-
このコードでsetColor()をするべきなのは、`this.paint`です。
|
2
|
-
|
3
|
-
ところが、まったく使っていないthis.penに対してsetColorを呼んでいるので色が変わりません。
|
4
|
-
|
5
|
-
```java
|
6
|
-
|
7
|
-
public void setPen(int color){
|
8
|
-
|
9
|
-
pen.setColor(color);
|
10
|
-
|
11
|
-
}
|
12
|
-
|
13
|
-
```
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
this.penは使用していないので、削除しましょう。
|
18
|
-
|
19
|
-
penを削除したコードはこうなると思います。
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
```java
|
24
|
-
|
25
|
-
public class DrawingView extends View {
|
26
|
-
|
27
|
-
private Paint paint;
|
28
|
-
|
29
|
-
private Path path;
|
30
|
-
|
31
|
-
Bitmap bmp = null;
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
public DrawingView(Context context, AttributeSet attrs) {
|
36
|
-
|
37
|
-
super(context, attrs);
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
this.path = new Path();
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
this.paint = new Paint();
|
46
|
-
|
47
|
-
this.paint.setStyle(Paint.Style.STROKE);
|
48
|
-
|
49
|
-
this.paint.setAntiAlias(true);
|
50
|
-
|
51
|
-
this.paint.setStrokeWidth(5);
|
52
|
-
|
53
|
-
}
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
@Override
|
58
|
-
|
59
|
-
protected void onDraw(Canvas canvas) {
|
60
|
-
|
61
|
-
super.onDraw(canvas);
|
62
|
-
|
63
|
-
canvas.drawPath(this.path, this.paint);
|
64
|
-
|
65
|
-
}
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
@Override
|
70
|
-
|
71
|
-
public boolean onTouchEvent(MotionEvent event) {
|
72
|
-
|
73
|
-
float x = event.getX();
|
74
|
-
|
75
|
-
float y = event.getY();
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
switch (event.getAction()) {
|
80
|
-
|
81
|
-
case MotionEvent.ACTION_DOWN:
|
82
|
-
|
83
|
-
this.path.moveTo(x, y);
|
84
|
-
|
85
|
-
break;
|
86
|
-
|
87
|
-
case MotionEvent.ACTION_MOVE:
|
88
|
-
|
89
|
-
this.path.lineTo(x, y);
|
90
|
-
|
91
|
-
break;
|
92
|
-
|
93
|
-
case MotionEvent.ACTION_UP:
|
94
|
-
|
95
|
-
this.path.lineTo(x, y);
|
96
|
-
|
97
|
-
break;
|
98
|
-
|
99
|
-
}
|
100
|
-
|
101
|
-
invalidate();
|
102
|
-
|
103
|
-
return true;
|
104
|
-
|
105
|
-
}
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
public void delete() {
|
110
|
-
|
111
|
-
this.path.reset();
|
112
|
-
|
113
|
-
invalidate();
|
114
|
-
|
115
|
-
}
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
public void setPen(int color){
|
120
|
-
|
121
|
-
this.paint.setColor(color);
|
122
|
-
|
123
|
-
}
|
124
|
-
|
125
|
-
}
|
126
|
-
|
127
|
-
```
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
1
|
+
回答を修正し、履歴を管理するバージョンのコードだけを残します。
|
132
2
|
|
133
3
|
|
134
4
|
|
@@ -292,6 +162,12 @@
|
|
292
162
|
|
293
163
|
public void delete() {
|
294
164
|
|
165
|
+
// 履歴をクリア
|
166
|
+
|
167
|
+
this.lines.clear();
|
168
|
+
|
169
|
+
// 現在の線をクリア
|
170
|
+
|
295
171
|
this.path.reset();
|
296
172
|
|
297
173
|
invalidate();
|
3
追記したコードのミスを修正
test
CHANGED
@@ -272,6 +272,12 @@
|
|
272
272
|
|
273
273
|
this.lines.add(new DrawLine(this.path, this.paint));
|
274
274
|
|
275
|
+
// パスをリセットする
|
276
|
+
|
277
|
+
// これを忘れると、全ての線の色が変わってしまう
|
278
|
+
|
279
|
+
this.path.reset();
|
280
|
+
|
275
281
|
break;
|
276
282
|
|
277
283
|
}
|
2
コメントで記載した履歴を保存するやり方を追記しました
test
CHANGED
@@ -125,3 +125,181 @@
|
|
125
125
|
}
|
126
126
|
|
127
127
|
```
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
## 追記: 履歴を元に線を引くバージョン
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
```java
|
136
|
+
|
137
|
+
public class DrawingView extends View {
|
138
|
+
|
139
|
+
// 履歴
|
140
|
+
|
141
|
+
private List<DrawLine> lines;
|
142
|
+
|
143
|
+
// 現在、描いている線の情報
|
144
|
+
|
145
|
+
private Paint paint;
|
146
|
+
|
147
|
+
private Path path;
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
// 線の履歴(座標+色)
|
152
|
+
|
153
|
+
class DrawLine {
|
154
|
+
|
155
|
+
private Paint paint;
|
156
|
+
|
157
|
+
private Path path;
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
DrawLine(Path path, Paint paint) {
|
162
|
+
|
163
|
+
this.paint = new Paint(paint);
|
164
|
+
|
165
|
+
this.path = new Path(path);
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
void draw(Canvas canvas) {
|
172
|
+
|
173
|
+
canvas.drawPath(this.path, this.paint);
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
public DrawingView(Context context) {
|
182
|
+
|
183
|
+
super(context);
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
public DrawingView(Context context, AttributeSet attrs) {
|
190
|
+
|
191
|
+
super(context, attrs);
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
this.path = new Path();
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
this.paint = new Paint();
|
200
|
+
|
201
|
+
this.paint.setStyle(Paint.Style.STROKE);
|
202
|
+
|
203
|
+
this.paint.setAntiAlias(true);
|
204
|
+
|
205
|
+
this.paint.setStrokeWidth(5);
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
this.lines = new ArrayList<DrawLine>();
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
@Override
|
216
|
+
|
217
|
+
protected void onDraw(Canvas canvas) {
|
218
|
+
|
219
|
+
super.onDraw(canvas);
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
// キャンバスをクリア
|
224
|
+
|
225
|
+
canvas.drawColor(Color.WHITE);
|
226
|
+
|
227
|
+
// 履歴から線を描画
|
228
|
+
|
229
|
+
for(DrawLine line : this.lines) {
|
230
|
+
|
231
|
+
line.draw(canvas);
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
// 現在、描いている線を描画
|
236
|
+
|
237
|
+
canvas.drawPath(this.path, this.paint);
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
@Override
|
244
|
+
|
245
|
+
public boolean onTouchEvent(MotionEvent event) {
|
246
|
+
|
247
|
+
float x = event.getX();
|
248
|
+
|
249
|
+
float y = event.getY();
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
switch (event.getAction()) {
|
254
|
+
|
255
|
+
case MotionEvent.ACTION_DOWN:
|
256
|
+
|
257
|
+
this.path.moveTo(x, y);
|
258
|
+
|
259
|
+
break;
|
260
|
+
|
261
|
+
case MotionEvent.ACTION_MOVE:
|
262
|
+
|
263
|
+
this.path.lineTo(x, y);
|
264
|
+
|
265
|
+
break;
|
266
|
+
|
267
|
+
case MotionEvent.ACTION_UP:
|
268
|
+
|
269
|
+
this.path.lineTo(x, y);
|
270
|
+
|
271
|
+
// 指を離したので、履歴に追加する
|
272
|
+
|
273
|
+
this.lines.add(new DrawLine(this.path, this.paint));
|
274
|
+
|
275
|
+
break;
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
invalidate();
|
280
|
+
|
281
|
+
return true;
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
public void delete() {
|
288
|
+
|
289
|
+
this.path.reset();
|
290
|
+
|
291
|
+
invalidate();
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
public void setPen(int color){
|
298
|
+
|
299
|
+
this.paint.setColor(color);
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
```
|
1
追記
test
CHANGED
@@ -13,6 +13,8 @@
|
|
13
13
|
```
|
14
14
|
|
15
15
|
|
16
|
+
|
17
|
+
this.penは使用していないので、削除しましょう。
|
16
18
|
|
17
19
|
penを削除したコードはこうなると思います。
|
18
20
|
|