質問編集履歴
2
ソースコードの追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -15,73 +15,68 @@
|
|
15
15
|
アプリを実行して画面を指でなぞってみても線が描けません。
|
16
16
|
|
17
17
|
なぞっているのに、画面が飯能しないことについて、どんな原因が考えられるでしょうか・・??
|
18
|
-
|
18
|
+
↓MainActivity.java
|
19
19
|
```java
|
20
20
|
package com.example.paintview;
|
21
21
|
|
22
|
-
import android.
|
22
|
+
import android.os.Bundle;
|
23
|
+
import android.support.design.widget.FloatingActionButton;
|
23
|
-
import android.
|
24
|
+
import android.support.design.widget.Snackbar;
|
24
|
-
import android.
|
25
|
+
import android.support.v7.app.AppCompatActivity;
|
25
|
-
import android.graphics.Path;
|
26
|
-
import android.
|
26
|
+
import android.support.v7.widget.Toolbar;
|
27
|
-
import android.view.MotionEvent;
|
28
27
|
import android.view.View;
|
28
|
+
import android.view.Menu;
|
29
|
+
import android.view.MenuItem;
|
29
30
|
|
30
|
-
public class
|
31
|
+
public class MainActivity extends AppCompatActivity {
|
31
32
|
|
33
|
+
@Override
|
34
|
+
protected void onCreate(Bundle savedInstanceState) {
|
32
|
-
|
35
|
+
super.onCreate(savedInstanceState);
|
33
|
-
|
36
|
+
setContentView(R.layout.activity_main);
|
34
37
|
|
35
|
-
public PaintView(Context context) {
|
36
|
-
this(context, null);
|
37
|
-
}
|
38
38
|
|
39
|
-
|
39
|
+
Toolbar toolbar = findViewById(R.id.toolbar);
|
40
|
-
|
40
|
+
setSupportActionBar(toolbar);
|
41
|
+
|
42
|
+
FloatingActionButton fab = findViewById(R.id.fab);
|
43
|
+
fab.setOnClickListener(new View.OnClickListener() {
|
44
|
+
@Override
|
41
|
-
|
45
|
+
public void onClick(View view) {
|
46
|
+
//Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
|
47
|
+
PaintView paintView = (PaintView)findViewById(R.id.view);
|
42
|
-
|
48
|
+
paintView.clear();
|
43
|
-
|
49
|
+
}
|
44
|
-
paint.setStyle(Paint.Style.STROKE);
|
45
|
-
paint.setStrokeJoin(Paint.Join.ROUND);
|
46
|
-
paint.setStrokeCap(Paint.Cap.ROUND);
|
47
|
-
|
50
|
+
});
|
48
51
|
}
|
49
52
|
|
50
53
|
@Override
|
51
|
-
|
54
|
+
public boolean onCreateOptionsMenu(Menu menu) {
|
55
|
+
// Inflate the menu; this adds items to the action bar if it is present.
|
52
|
-
|
56
|
+
getMenuInflater().inflate(R.menu.menu_main, menu);
|
57
|
+
return true;
|
53
58
|
}
|
54
59
|
|
55
60
|
@Override
|
56
|
-
public boolean
|
61
|
+
public boolean onOptionsItemSelected(MenuItem item) {
|
62
|
+
// Handle action bar item clicks here. The action bar will
|
63
|
+
// automatically handle clicks on the Home/Up button, so long
|
64
|
+
// as you specify a parent activity in AndroidManifest.xml.
|
57
|
-
|
65
|
+
int id = item.getItemId();
|
58
|
-
float y = event.getY();
|
59
66
|
|
67
|
+
//noinspection SimplifiableIfStatement
|
60
|
-
|
68
|
+
if (id == R.id.action_settings) {
|
61
|
-
case MotionEvent.ACTION_DOWN:
|
62
|
-
path.moveTo(x, y);
|
63
|
-
|
69
|
+
return true;
|
64
|
-
break;
|
65
|
-
case MotionEvent.ACTION_MOVE:
|
66
|
-
path.lineTo(x, y);
|
67
|
-
invalidate();
|
68
|
-
break;
|
69
|
-
case MotionEvent.ACTION_UP:
|
70
|
-
path.lineTo(x, y);
|
71
|
-
invalidate();
|
72
|
-
break;
|
73
70
|
}
|
74
|
-
return true;
|
75
|
-
}
|
76
71
|
|
77
|
-
|
72
|
+
return super.onOptionsItemSelected(item);
|
78
|
-
path.reset();
|
79
|
-
invalidate();
|
80
73
|
}
|
81
|
-
|
82
74
|
}
|
83
75
|
|
76
|
+
```
|
77
|
+
↓PaintView.java
|
84
78
|
```java
|
79
|
+
|
85
80
|
package com.example.paintview;
|
86
81
|
|
87
82
|
import android.content.Context;
|
@@ -149,9 +144,9 @@
|
|
149
144
|
}
|
150
145
|
|
151
146
|
}
|
152
|
-
|
153
147
|
```
|
154
148
|
|
149
|
+
↓activity_main.xml
|
155
150
|
```xml
|
156
151
|
<?xml version="1.0" encoding="utf-8"?>
|
157
152
|
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
@@ -190,6 +185,7 @@
|
|
190
185
|
</android.support.design.widget.CoordinatorLayout>
|
191
186
|
```
|
192
187
|
|
188
|
+
↓content_main/xml
|
193
189
|
```xml
|
194
190
|
<?xml version="1.0" encoding="utf-8"?>
|
195
191
|
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
1
ソースコードの追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -81,4 +81,133 @@
|
|
81
81
|
|
82
82
|
}
|
83
83
|
|
84
|
+
```java
|
85
|
+
package com.example.paintview;
|
86
|
+
|
87
|
+
import android.content.Context;
|
88
|
+
import android.graphics.Canvas;
|
89
|
+
import android.graphics.Paint;
|
90
|
+
import android.graphics.Path;
|
91
|
+
import android.util.AttributeSet;
|
92
|
+
import android.view.MotionEvent;
|
93
|
+
import android.view.View;
|
94
|
+
|
95
|
+
public class PaintView extends View {
|
96
|
+
|
97
|
+
private Paint paint;
|
98
|
+
private Path path;
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
public PaintView(Context context) {
|
103
|
+
this(context, null);
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
public PaintView(Context context, AttributeSet attrs) {
|
108
|
+
|
109
|
+
super(context, attrs);
|
110
|
+
path = new Path();
|
111
|
+
paint = new Paint();
|
112
|
+
paint.setColor(0xFF000000);
|
113
|
+
paint.setStyle(Paint.Style.STROKE);
|
114
|
+
paint.setStrokeJoin(Paint.Join.ROUND);
|
115
|
+
paint.setStrokeCap(Paint.Cap.ROUND);
|
116
|
+
paint.setStrokeWidth(10);
|
117
|
+
}
|
118
|
+
|
119
|
+
@Override
|
120
|
+
protected void onDraw(Canvas canvas) {
|
121
|
+
canvas.drawPath(path, paint);
|
122
|
+
}
|
123
|
+
|
124
|
+
@Override
|
125
|
+
public boolean onTouchEvent(MotionEvent event) {
|
126
|
+
float x = event.getX();
|
127
|
+
float y = event.getY();
|
128
|
+
|
129
|
+
switch (event.getAction()) {
|
130
|
+
case MotionEvent.ACTION_DOWN:
|
131
|
+
path.moveTo(x, y);
|
132
|
+
invalidate();
|
133
|
+
break;
|
134
|
+
case MotionEvent.ACTION_MOVE:
|
135
|
+
path.lineTo(x, y);
|
136
|
+
invalidate();
|
137
|
+
break;
|
138
|
+
case MotionEvent.ACTION_UP:
|
139
|
+
path.lineTo(x, y);
|
140
|
+
invalidate();
|
141
|
+
break;
|
142
|
+
}
|
143
|
+
return true;
|
144
|
+
}
|
145
|
+
|
146
|
+
public void clear(){
|
147
|
+
path.reset();
|
148
|
+
invalidate();
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
```xml
|
156
|
+
<?xml version="1.0" encoding="utf-8"?>
|
157
|
+
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
158
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
159
|
+
xmlns:tools="http://schemas.android.com/tools"
|
160
|
+
android:layout_width="match_parent"
|
161
|
+
android:layout_height="match_parent"
|
162
|
+
tools:context=".MainActivity">
|
163
|
+
|
164
|
+
<android.support.design.widget.AppBarLayout
|
165
|
+
android:layout_width="match_parent"
|
166
|
+
android:layout_height="wrap_content"
|
167
|
+
android:theme="@style/AppTheme.AppBarOverlay">
|
168
|
+
|
169
|
+
<android.support.v7.widget.Toolbar
|
170
|
+
android:id="@+id/toolbar"
|
171
|
+
android:layout_width="match_parent"
|
172
|
+
android:layout_height="?attr/actionBarSize"
|
173
|
+
android:background="?attr/colorPrimary"
|
174
|
+
app:popupTheme="@style/AppTheme.PopupOverlay" />
|
175
|
+
|
176
|
+
</android.support.design.widget.AppBarLayout>
|
177
|
+
|
178
|
+
<include layout="@layout/content_main" />
|
179
|
+
|
180
|
+
<android.support.design.widget.FloatingActionButton
|
181
|
+
android:id="@+id/fab"
|
182
|
+
android:layout_width="wrap_content"
|
183
|
+
android:layout_height="wrap_content"
|
184
|
+
android:layout_gravity="bottom|end"
|
185
|
+
android:layout_margin="@dimen/fab_margin"
|
186
|
+
app:srcCompat="@android:drawable/ic_dialog_email" />
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
</android.support.design.widget.CoordinatorLayout>
|
191
|
+
```
|
192
|
+
|
193
|
+
```xml
|
194
|
+
<?xml version="1.0" encoding="utf-8"?>
|
195
|
+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
196
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
197
|
+
xmlns:tools="http://schemas.android.com/tools"
|
198
|
+
android:layout_width="match_parent"
|
199
|
+
android:layout_height="match_parent"
|
200
|
+
app:layout_behavior="@string/appbar_scrolling_view_behavior"
|
201
|
+
tools:context=".MainActivity"
|
202
|
+
tools:showIn="@layout/activity_main">
|
203
|
+
|
204
|
+
|
205
|
+
<view
|
206
|
+
class="com.example.paintview.PaintView"
|
207
|
+
id="@+id/view"
|
208
|
+
android:layout_width="match_parent"
|
209
|
+
android:layout_height="match_parent"
|
210
|
+
tools:layout_editor_absoluteX="45dp"
|
211
|
+
tools:layout_editor_absoluteY="421dp" />
|
212
|
+
</android.support.constraint.ConstraintLayout>
|
84
213
|
```
|