質問編集履歴
2
線を点にしようとしているがうまくいかない部分を追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
しかし、線を引くことはできるのですが、ドット絵を打つアプリを開発しているんので線を引くではなく点で打つを実装したいです。
|
4
4
|
ですが、線の太さを変更したり、続けて描画するを変更しているのですがなぜかうまくいきません。
|
5
5
|
何かいい方法などはあるのでしょうか?
|
6
|
+
9/27 19:00追記
|
7
|
+
drawingviewのDrawLineの部分をDrawPointに変えて点を打てるようにしたいのですがやっぱり線で描画されてしまいます。
|
8
|
+
Pointに変換する以外で線を引くのではなくタッチ時点で点を打てるようにする方法はあるのでしょうか
|
6
9
|
> drawingbiew.java
|
7
10
|
```ここに言語を入力
|
8
11
|
package e.toku.myapplication;
|
1
コードの追加 質問内容の訂正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,230 @@
|
|
1
|
-
今、ドット絵を打てるアプリを開発してます。
|
1
|
+
今、androidstudioでドット絵を打てるアプリを開発してます。
|
2
2
|
canvas上に線を引く・消しゴムで消す・線の太さを変えるが実装されています。
|
3
3
|
しかし、線を引くことはできるのですが、ドット絵を打つアプリを開発しているんので線を引くではなく点で打つを実装したいです。
|
4
4
|
ですが、線の太さを変更したり、続けて描画するを変更しているのですがなぜかうまくいきません。
|
5
|
-
何かいい方法などはあるのでしょうか?
|
5
|
+
何かいい方法などはあるのでしょうか?
|
6
|
+
> drawingbiew.java
|
7
|
+
```ここに言語を入力
|
8
|
+
package e.toku.myapplication;
|
9
|
+
|
10
|
+
import android.content.Context;
|
11
|
+
import android.graphics.Canvas;
|
12
|
+
import android.graphics.Color;
|
13
|
+
import android.graphics.Paint;
|
14
|
+
import android.graphics.Path;
|
15
|
+
import android.util.AttributeSet;
|
16
|
+
import android.view.MotionEvent;
|
17
|
+
import android.view.View;
|
18
|
+
import java.util.ArrayList;
|
19
|
+
import java.util.List;
|
20
|
+
|
21
|
+
|
22
|
+
public class DrawingView extends View {
|
23
|
+
// 履歴
|
24
|
+
private List<DrawLine> lines;
|
25
|
+
// 現在、描いている線の情報
|
26
|
+
private Paint paint;
|
27
|
+
private Path path;
|
28
|
+
|
29
|
+
// 線の履歴(座標+色)
|
30
|
+
class DrawLine {
|
31
|
+
private Paint paint;
|
32
|
+
private Path path;
|
33
|
+
|
34
|
+
DrawLine(Path path, Paint paint) {
|
35
|
+
this.paint = new Paint(paint);
|
36
|
+
this.path = new Path(path);
|
37
|
+
}
|
38
|
+
|
39
|
+
void draw(Canvas canvas) {
|
40
|
+
canvas.drawPath(this.path, this.paint);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
public DrawingView(Context context) {
|
45
|
+
super(context);
|
46
|
+
}
|
47
|
+
|
48
|
+
public DrawingView(Context context, AttributeSet attrs) {
|
49
|
+
super(context, attrs);
|
50
|
+
|
51
|
+
this.path = new Path();
|
52
|
+
|
53
|
+
this.paint = new Paint();
|
54
|
+
this.paint.setStyle(Paint.Style.STROKE);
|
55
|
+
this.paint.setAntiAlias(true);
|
56
|
+
//this.paint.setStrokeWidth(15);
|
57
|
+
|
58
|
+
this.lines = new ArrayList<DrawLine>();
|
59
|
+
}
|
60
|
+
|
61
|
+
@Override
|
62
|
+
protected void onDraw(Canvas canvas) {
|
63
|
+
super.onDraw(canvas);
|
64
|
+
|
65
|
+
// キャンバスをクリア
|
66
|
+
canvas.drawColor(Color.WHITE);
|
67
|
+
// 履歴から線を描画
|
68
|
+
for(DrawLine line : this.lines) {
|
69
|
+
line.draw(canvas);
|
70
|
+
}
|
71
|
+
// 現在、描いている線を描画
|
72
|
+
canvas.drawPath(this.path, this.paint);
|
73
|
+
|
74
|
+
}
|
75
|
+
|
76
|
+
@Override
|
77
|
+
public boolean onTouchEvent(MotionEvent event) {
|
78
|
+
float x = event.getX();
|
79
|
+
float y = event.getY();
|
80
|
+
|
81
|
+
switch (event.getAction()) {
|
82
|
+
case MotionEvent.ACTION_DOWN:
|
83
|
+
this.path.moveTo(x, y);
|
84
|
+
break;
|
85
|
+
case MotionEvent.ACTION_MOVE:
|
86
|
+
this.path.lineTo(x, y);
|
87
|
+
break;
|
88
|
+
case MotionEvent.ACTION_UP:
|
89
|
+
this.path.lineTo(x, y);
|
90
|
+
// 指を離したので、履歴に追加する
|
91
|
+
this.lines.add(new DrawLine(this.path, this.paint));
|
92
|
+
// パスをリセットする
|
93
|
+
// これを忘れると、全ての線の色が変わってしまう
|
94
|
+
this.path.reset();
|
95
|
+
break;
|
96
|
+
}
|
97
|
+
invalidate();
|
98
|
+
return true;
|
99
|
+
}
|
100
|
+
|
101
|
+
public void delete() {
|
102
|
+
// 履歴をクリア
|
103
|
+
this.lines.clear();
|
104
|
+
// 現在の線をクリア
|
105
|
+
this.path.reset();
|
106
|
+
invalidate();
|
107
|
+
}
|
108
|
+
|
109
|
+
public void setPen(int color){
|
110
|
+
this.paint.setColor(color);
|
111
|
+
}
|
112
|
+
|
113
|
+
public void setStrokeWidth(float width){
|
114
|
+
this.paint.setStrokeWidth(width);
|
115
|
+
}
|
116
|
+
}
|
117
|
+
```
|
118
|
+
> mainactivty
|
119
|
+
```ここに言語を入力
|
120
|
+
package e.toku.myapplication;
|
121
|
+
|
122
|
+
import android.app.Activity;
|
123
|
+
import android.graphics.Color;
|
124
|
+
import android.os.Bundle;
|
125
|
+
import android.os.CpuUsageInfo;
|
126
|
+
import android.support.v7.app.AppCompatActivity;
|
127
|
+
import android.view.View;
|
128
|
+
import android.view.Window;
|
129
|
+
import android.widget.Button;
|
130
|
+
import android.widget.Toast;
|
131
|
+
import android.widget.SeekBar;
|
132
|
+
|
133
|
+
|
134
|
+
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
|
135
|
+
private DrawingView drawingView;
|
136
|
+
Button red_button,blue_button,yellow_button,black_button,kesigomu_button;
|
137
|
+
Button hutosa1_button,hutosa2_button;
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
@Override
|
142
|
+
protected void onCreate(Bundle savedInstanceState) {
|
143
|
+
super.onCreate(savedInstanceState);
|
144
|
+
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
|
145
|
+
setContentView(R.layout.activity_main);
|
146
|
+
|
147
|
+
//線の色指定
|
148
|
+
blue_button= findViewById(R.id.blue_button);
|
149
|
+
red_button= findViewById(R.id.red_button);
|
150
|
+
yellow_button= findViewById(R.id.yellow_button) ;
|
151
|
+
black_button= findViewById(R.id.black_button);
|
152
|
+
kesigomu_button= findViewById(R.id.kesigomu_button);
|
153
|
+
|
154
|
+
//線の太さ指定
|
155
|
+
hutosa1_button= findViewById(R.id.hutosa1_button);
|
156
|
+
hutosa2_button= findViewById(R.id.hutosa2_button);
|
157
|
+
|
158
|
+
|
159
|
+
blue_button.setOnClickListener(this);
|
160
|
+
red_button.setOnClickListener(this);
|
161
|
+
yellow_button.setOnClickListener(this);
|
162
|
+
black_button.setOnClickListener(this);
|
163
|
+
kesigomu_button.setOnClickListener(this);
|
164
|
+
|
165
|
+
hutosa1_button.setOnClickListener(this);
|
166
|
+
hutosa2_button.setOnClickListener(this);
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
this.drawingView = findViewById(R.id.drawing_view);
|
173
|
+
|
174
|
+
findViewById(R.id.deletebutton).setOnClickListener(deleteDrawing);
|
175
|
+
|
176
|
+
}
|
177
|
+
View.OnClickListener deleteDrawing = new View.OnClickListener(){
|
178
|
+
@Override
|
179
|
+
public void onClick(View view){
|
180
|
+
drawingView.delete();
|
181
|
+
}
|
182
|
+
};
|
183
|
+
|
184
|
+
|
185
|
+
public void onClick(View v){
|
186
|
+
switch(v.getId()){
|
187
|
+
//線の色指定
|
188
|
+
case R.id.blue_button:
|
189
|
+
drawingView.setPen(Color.BLUE);
|
190
|
+
Toast.makeText(this,"blue",Toast.LENGTH_SHORT).show();
|
191
|
+
break;
|
192
|
+
case R.id.red_button:
|
193
|
+
drawingView.setPen(Color.RED);
|
194
|
+
Toast.makeText(this,"red",Toast.LENGTH_SHORT).show();
|
195
|
+
break;
|
196
|
+
case R.id.yellow_button:
|
197
|
+
drawingView.setPen(Color.YELLOW);
|
198
|
+
Toast.makeText(this,"yellow",Toast.LENGTH_SHORT).show();
|
199
|
+
break;
|
200
|
+
case R.id.black_button:
|
201
|
+
drawingView.setPen(Color.BLACK);
|
202
|
+
Toast.makeText(this,"black",Toast.LENGTH_SHORT).show();
|
203
|
+
break;
|
204
|
+
case R.id.kesigomu_button:
|
205
|
+
drawingView.setPen(Color.WHITE);
|
206
|
+
drawingView.setStrokeWidth(45);
|
207
|
+
Toast.makeText(this,"消しゴム",Toast.LENGTH_SHORT).show();
|
208
|
+
break;
|
209
|
+
//線の太さ指定
|
210
|
+
case R.id.hutosa1_button:
|
211
|
+
drawingView.setStrokeWidth(15);
|
212
|
+
Toast.makeText(this,"太さ中",Toast.LENGTH_SHORT).show();
|
213
|
+
break;
|
214
|
+
case R.id.hutosa2_button:
|
215
|
+
drawingView.setStrokeWidth(30);
|
216
|
+
Toast.makeText(this,"太さ大",Toast.LENGTH_SHORT).show();
|
217
|
+
break;
|
218
|
+
|
219
|
+
}
|
220
|
+
}
|
221
|
+
|
222
|
+
@Override
|
223
|
+
public void onPointerCaptureChanged(boolean hasCapture) {
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
}
|
229
|
+
|
230
|
+
```
|