-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawingView.java
104 lines (86 loc) · 2.74 KB
/
DrawingView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package ca.uwaterloo.cs349;
import android.content.Context;
import android.graphics.*;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class DrawingView extends View {
private Paint paint = new Paint();
private List<PointF> points = null;
private List<Button> buttons = new ArrayList<>();
public DrawingView(Context context) {
super(context);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(12);
paint.setPathEffect(null);
}
public void addButton(Button button) {
button.setEnabled(false);
buttons.add(button);
}
public List<PointF> getPoints() {
return points;
}
public void clearView() {
points = null;
invalidate();
for(Button button : buttons) {
button.setEnabled(false);
}
}
@Override
public void setOnTouchListener(OnTouchListener l) {
super.setOnTouchListener(l);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
points.add(new PointF(event.getX(), event.getY()));
invalidate();
return true;
case MotionEvent.ACTION_DOWN:
points = new ArrayList<>();
points.add(new PointF(event.getX(), event.getY()));
invalidate();
return true;
case MotionEvent.ACTION_UP:
points.add(new PointF(event.getX(), event.getY()));
invalidate();
for(Button button : buttons) {
button.setEnabled(true);
}
return true;
}
return false;
}
public void setPoints(List<PointF> points) {
this.points = points;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(points == null) {
return;
}
Path path = new Path();
path.moveTo(points.get(0).x, points.get(0).y);
for(int i = 1; i < points.size(); ++i) {
path.lineTo(points.get(i).x, points.get(i).y);
}
canvas.drawPath(path, paint);
/*
Paint pointPaint = new Paint();
pointPaint.setColor(Color.RED);
for(PointF point : points) {
canvas.drawCircle(point.x, point.y, 5, pointPaint);
}*/
}
}