回答編集履歴

2

前回編集不足

2019/02/14 05:22

投稿

jimbe
jimbe

スコア12545

test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  1つの View 内でドットを 9 つ書いて, 線を引いています.
4
4
 
5
- PatternLockView.OnTouchDotListener を登録すると, ドットに接触する毎にその番号が通知されます.
5
+ PatternLockView.OnDotTouchListener を登録すると, ドットに接触する毎にその番号が通知されます.
6
6
 
7
7
  View を 3x3 に置いてその上に透明な View を置いた場合もこのような処理は必要になると思われますので, 3x3 に置いた View の役割が無いと思います.
8
8
 

1

メソッド名変更, PatternLockView 機能追加

2019/02/14 05:22

投稿

jimbe
jimbe

スコア12545

test CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  import android.view.*;
26
26
 
27
- import java.util.ArrayList;
27
+ import java.util.*;
28
28
 
29
29
 
30
30
 
@@ -70,7 +70,7 @@
70
70
 
71
71
 
72
72
 
73
- interface OnTouchDotListener {
73
+ interface OnDotTouchListener {
74
74
 
75
75
  /**
76
76
 
@@ -88,13 +88,13 @@
88
88
 
89
89
  */
90
90
 
91
- void onTouchDot(View view, int action, int index);
91
+ void onDotTouch(View view, int action, int index);
92
-
92
+
93
- }
93
+ }
94
-
94
+
95
- private OnTouchDotListener listener;
95
+ private OnDotTouchListener listener;
96
-
96
+
97
- void setTouchDotListener(OnTouchDotListener listener) {
97
+ void setOnDotTouchListener(OnDotTouchListener listener) {
98
98
 
99
99
  this.listener = listener;
100
100
 
@@ -120,7 +120,13 @@
120
120
 
121
121
  }
122
122
 
123
+ /** 各ドット */
124
+
123
- private ArrayList<Dot> dots; //3x3
125
+ private ArrayList<Dot> dots;
126
+
127
+ /** タッチ済みのドットの dots index (0~8) */
128
+
129
+ private Set<Integer> touched = new HashSet<Integer>();
124
130
 
125
131
 
126
132
 
@@ -128,6 +134,12 @@
128
134
 
129
135
  private Path path;
130
136
 
137
+ private Point lastDotPoint;
138
+
139
+ private int currentTouchX;
140
+
141
+ private int currentTouchY;
142
+
131
143
 
132
144
 
133
145
  @Override
@@ -168,10 +180,14 @@
168
180
 
169
181
  Paint paint = new Paint();
170
182
 
183
+ paint.setAntiAlias(true);
184
+
171
185
 
172
186
 
173
187
  paint.setColor(Color.RED);
174
188
 
189
+ paint.setStyle(Paint.Style.FILL);
190
+
175
191
  for (Dot dot : dots) canvas.drawRect(dot.rect, paint);
176
192
 
177
193
 
@@ -182,14 +198,30 @@
182
198
 
183
199
  paint.setStyle(Paint.Style.STROKE);
184
200
 
185
- paint.setAntiAlias(true);
186
-
187
201
  paint.setStrokeWidth(10);
188
202
 
189
203
  canvas.drawPath(path, paint);
190
204
 
191
205
  }
192
206
 
207
+ if(lastDotPoint != null) {
208
+
209
+ paint.setColor(Color.BLUE);
210
+
211
+ paint.setStyle(Paint.Style.STROKE);
212
+
213
+ paint.setStrokeWidth(10);
214
+
215
+ Path currentPath = new Path();
216
+
217
+ currentPath.moveTo(lastDotPoint.x, lastDotPoint.y);
218
+
219
+ currentPath.lineTo(currentTouchX, currentTouchY);
220
+
221
+ canvas.drawPath(currentPath, paint);
222
+
223
+ }
224
+
193
225
  }
194
226
 
195
227
 
@@ -202,13 +234,21 @@
202
234
 
203
235
  if(action == MotionEvent.ACTION_DOWN || (tracking && action == MotionEvent.ACTION_MOVE)) {
204
236
 
237
+ currentTouchX = (int)event.getX();
238
+
239
+ currentTouchY = (int)event.getY();
240
+
205
241
  int index = searchDotIndex((int)event.getX(), (int)event.getY());
206
242
 
243
+ if(index != -1 && !touched.contains(index)) { //未タッチのドット
244
+
207
- if(index != -1) {
245
+ touched.add(index);
208
-
246
+
209
- if(listener != null) {
247
+ if (listener != null) {
248
+
210
-
249
+ lastDotPoint = dots.get(index).point;
250
+
211
- if(action == MotionEvent.ACTION_DOWN) {
251
+ if (action == MotionEvent.ACTION_DOWN) {
212
252
 
213
253
  path = new Path();
214
254
 
@@ -220,9 +260,7 @@
220
260
 
221
261
  }
222
262
 
223
- invalidate();
224
-
225
- listener.onTouchDot(this, event.getAction(), index);
263
+ listener.onDotTouch(this, event.getAction(), index);
226
264
 
227
265
  tracking = true;
228
266
 
@@ -230,16 +268,22 @@
230
268
 
231
269
  }
232
270
 
271
+ invalidate();
272
+
233
273
  return true;
234
274
 
235
275
  } else if(tracking && action == MotionEvent.ACTION_UP) {
236
276
 
237
277
  if(listener != null) {
238
278
 
239
- listener.onTouchDot(this, event.getAction(), -1);
279
+ listener.onDotTouch(this, event.getAction(), -1);
240
280
 
241
281
  tracking = false;
242
282
 
283
+ touched.clear();
284
+
285
+ lastDotPoint = null;
286
+
243
287
  }
244
288
 
245
289
  return true;
@@ -298,13 +342,13 @@
298
342
 
299
343
  PatternLockView patternLockView = findViewById(R.id.patternLockView);
300
344
 
301
- patternLockView.setTouchDotListener(new PatternLockView.OnTouchDotListener() {
345
+ patternLockView.setOnDotTouchListener(new PatternLockView.OnDotTouchListener() {
302
346
 
303
347
  @Override
304
348
 
305
- public void onTouchDot(View view, int action, int index) {
349
+ public void onDotTouch(View view, int action, int index) {
306
-
350
+
307
- Log.d("onTouchDot","action="+action+", index="+index);
351
+ Log.d("onDotTouch","action="+action+", index="+index);
308
352
 
309
353
  }
310
354