我正在尝试开发一个简单的“触摸绘图”,并且我想绘制用户在 onTouch 侦听器中放置的不同路径。我有一个简单的问题。当我绘制路径时,我获得了在最后一个 onTouch 条目的点中绘制的单个路径。我认为所有路径都绘制在最后一条路径上,因为我使用具有 150 alpha 的 Paint,并且它在第二个和连续的 onTouch 条目中看起来更可靠。
我怎么能解决这个问题?谢谢!
public class PaintView extends View implements OnTouchListener {
List<List<Point>> paths = new ArrayList<List<Point>>();
List<Point> points = new ArrayList<Point>();
Paint paintLine = new Paint();
Paint paintCircle = new Paint();
public PaintView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(10);
paintLine.setColor(Color.GREEN);
paintLine.setAlpha(150);
}
public void onDraw(Canvas canvas) {
Path path = new Path();
List<Point> pointsList;
List<Path> pathList = new ArrayList<Path>();
Point point = new Point();
for (int i = 0; i < paths.size(); i++) {
pointsList = paths.get(i);
path.reset();
boolean first = true;
for (int j = 0; j < points.size(); j+=2 ) {
point = pointsList.get(j);
if (first) {
first = false;
path.moveTo(point.x, point.y);
} else if (j < pointsList.size() - 1 ) {
Point nextPoint = pointsList.get(j+1);
path.quadTo(point.x, point.y, nextPoint.x, nextPoint.y);
} else {
path.lineTo(point.x, point.y);
}
}
pathList.add(path);
}
for (Path pathDraw : pathList) {
canvas.drawPath(pathDraw, paintLine);
}
}
public boolean onTouch(View view, final MotionEvent event) {
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// DO SOMETHING
points.clear();
points.add(point);
break;
case MotionEvent.ACTION_MOVE:
points.add(point);
break;
case MotionEvent.ACTION_UP:
points.add(point);
paths.add(points);
break;
default:
return false;
}
invalidate();
return true;
}
}