質問編集履歴

1

書式の改善、足りない情報の付け足し

2017/04/11 07:11

投稿

jigokunoiruka
jigokunoiruka

スコア8

test CHANGED
File without changes
test CHANGED
@@ -1,7 +1,283 @@
1
+ <4/11に編集しました>
2
+
3
+
4
+
5
+ ```java
6
+
7
+ //interfaceを作成
8
+
9
+ public interface TouchupListener extends EventListener{
10
+
11
+ public void strokeWasFinished();
12
+
13
+ }
14
+
15
+ ```
16
+
17
+
18
+
19
+ ```java
20
+
21
+ //イベントが発生したことを通知するクラス
22
+
23
+ public class TouchupNotify {
24
+
25
+
26
+
27
+ private TouchupListener listener;
28
+
29
+
30
+
31
+ // 画像が表示された事を通知
32
+
33
+ public void informToActivity() {
34
+
35
+ listener.strokeWasFinished();
36
+
37
+ }
38
+
39
+
40
+
41
+ // リスナーをセットする
42
+
43
+ public void setListener(TouchupListener listener){
44
+
45
+ this.listener = listener;
46
+
47
+ }
48
+
49
+
50
+
51
+ }
52
+
53
+ ```
54
+
55
+ ```java
56
+
57
+ public class DrawingView extends View {
58
+
59
+
60
+
61
+ private Paint paint;
62
+
63
+ private Path path;
64
+
65
+ private TouchupNotify touchupNotify;
66
+
67
+
68
+
69
+ public DrawingView(Context context, AttributeSet attrs) {
70
+
71
+ super(context, attrs);
72
+
73
+
74
+
75
+ this.path = new Path();
76
+
77
+
78
+
79
+ this.paint = new Paint();
80
+
81
+ paint.setColor(Color.RED);
82
+
83
+ this.paint.setStyle(Paint.Style.STROKE);
84
+
85
+ this.paint.setAntiAlias(true);
86
+
87
+ this.paint.setStrokeWidth(10);
88
+
89
+ this.path.reset();
90
+
91
+ invalidate();
92
+
93
+ }
94
+
95
+
96
+
97
+ @Override
98
+
99
+ protected void onDraw(Canvas canvas) {
100
+
101
+ super.onDraw(canvas);
102
+
103
+ canvas.drawPath(path, paint);
104
+
105
+ }
106
+
107
+
108
+
109
+ @Override
110
+
111
+ public boolean onTouchEvent(MotionEvent event) {
112
+
113
+ float x = event.getX();
114
+
115
+ float y = event.getY();
116
+
117
+
118
+
119
+ switch (event.getAction()) {
120
+
121
+ case MotionEvent.ACTION_DOWN:
122
+
123
+ this.path.moveTo(x, y);
124
+
125
+ break;
126
+
127
+ case MotionEvent.ACTION_MOVE:
128
+
129
+ this.path.lineTo(x, y);
130
+
131
+ break;
132
+
133
+ case MotionEvent.ACTION_UP:
134
+
135
+ this.path.lineTo(x, y);
136
+
137
+ //ここで呼び出すがエラーが出る↓
138
+
139
+ touchupNotify.informToActivity();
140
+
141
+ break;
142
+
143
+ }
144
+
145
+ invalidate();
146
+
147
+ return true;
148
+
149
+ }
150
+
151
+
152
+
153
+ public void delete() {
154
+
155
+ this.path.reset();
156
+
157
+ invalidate();
158
+
159
+ }
160
+
161
+ }
162
+
163
+
164
+
165
+ ```
166
+
167
+ ```java
168
+
169
+ public class DrawingActivity extends AppCompatActivity implements TouchupListener{
170
+
171
+
172
+
173
+ private TouchupNotify touchupNotify;
174
+
175
+ int count=1;
176
+
177
+
178
+
179
+ @Override
180
+
181
+ protected void onCreate(Bundle savedInstanceState) {
182
+
183
+ super.onCreate(savedInstanceState);
184
+
185
+ setContentView(R.layout.activity_drawing);
186
+
187
+
188
+
189
+ touchupNotify=new TouchupNotify();
190
+
191
+ touchupNotify.setListener(this);
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+ public void checkPlayerDialog(String message, String btnstr, final int WorH){
202
+
203
+ // 確認ダイアログの生成
204
+
205
+ if(count==player_num-1){
206
+
207
+ return;
208
+
209
+ }
210
+
211
+ AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
212
+
213
+ alertDlg.setMessage(message);
214
+
215
+ alertDlg.setPositiveButton(
216
+
217
+ btnstr,
218
+
219
+ new DialogInterface.OnClickListener() {
220
+
221
+ public void onClick(DialogInterface dialog, int which) {
222
+
223
+ // OK ボタンクリック処理
224
+
225
+ }
226
+
227
+ }
228
+
229
+ });
230
+
231
+
232
+
233
+ // 表示
234
+
235
+ alertDlg.setCancelable(false);
236
+
237
+ alertDlg.create().show();
238
+
239
+ count++;
240
+
241
+
242
+
243
+ }
244
+
245
+
246
+
247
+ @Override
248
+
249
+ public void strokeWasFinished() {
250
+
251
+ checkPlayerDialog(String.valueOf(count)+"画目です", "OK",count);
252
+
253
+ }
254
+
255
+ }
256
+
257
+ ```
258
+
259
+
260
+
1
261
  http://qiita.com/1plus4/items/45773e997f56276aac68
2
262
 
3
-
4
-
5
263
  このサイトを参考にして、androidでお絵かきアプリを作成しています。
6
264
 
7
- DrawingViewで、一画書く度にアクティビティにてメッセージダイアログを出すようにしたいのですが、どうすれば出来ますか??
265
+ DrawingViewで、一画書く度にアクティビティにてメッセージダイアログを出すようにしたいのですが、
266
+
267
+ 上記のように独自のリスナーを作成し、アクティビティに実装し、drawingViewで通知してみたところ
268
+
269
+
270
+
271
+ > 04-11 15:55:30.255 2610-2610/com.artwolf.jigokunoiruka.artwolf E/AndroidRuntime: FATAL EXCEPTION: main
272
+
273
+ Process: com.artwolf.jigokunoiruka.artwolf, PID: 2610
274
+
275
+ java.lang.NullPointerException: Attempt to invoke virtual method 'void com.artwolf.jigokunoiruka.aaaaa.TouchupNotify.informToActivity()' on a null object reference
276
+
277
+ at com.artwolf.jigokunoiruka.artwolf.DrawingView.onTouchEvent(DrawingView.java:54)
278
+
279
+
280
+
281
+
282
+
283
+ というエラーが表示され困っています。