1 回答
TA贡献2039条经验 获得超7个赞
只需存储 (x,y) 点并在必要时稍后“重绘”它们。
对于 (x,y),我指的是event getX&getY值。将所有值存储在 中ArrayList,然后您可以使用存储的值重绘形状。基本上,我建议你按照以下几行做一些事情,
创建一个点数组
ArrayList<Pair<Float,Float>> points = new ArrayList<>();
将触摸点存储在数组中
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
//respond to down, move and up events
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawPath.moveTo(touchX, touchY);
points.add(new Pair<Float, Float>(touchX, touchY));
break;
case MotionEvent.ACTION_MOVE:
drawPath.lineTo(touchX, touchY);
points.add(new Pair<Float, Float>(touchX, touchY));
break;
case MotionEvent.ACTION_UP:
drawPath.lineTo(touchX, touchY);
System.out.println(touchX +", " +touchY);
points.add(new Pair<Float, Float>(touchX, touchY));
//At this point you might want to
//store this array somewhere
//so you can use it to redraw later if needed
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
break;
default:
return false;
}
//redraw
invalidate();
return true;
}
您可以在需要时使用数组重绘形状
public void drawFromArrayList(ArrayList<Pair<Float,Float>> points) {
int pointCount = points.size();
if (pointCount < 2) {
return;
}
for (int i=0;i<pointCount;i++) {
float touchX = points.get(i).first, touchY = points.get(i).second;
if(i==0) {
drawPath.moveTo(touchX, touchY);
}
drawPath.lineTo(touchX, touchY);
if(i==pointCount-1) {
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
}
}
}
添加回答
举报