我正在创建一个允许用户在屏幕上绘图的应用程序。我的代码实际上创建了一个路径,然后创建了一行。但是,当用户拿起屏幕的手指并转到屏幕的另一部分时,它会在下一个视频中出现一条线。 http://youtu.be/CBv1wtUC2g4
以下代码是drawview的代码。
public class DrawView extends View implements OnTouchListener {
private static final String TAG = "DrawView";
List<Point> points = new ArrayList<Point>();
Paint paint = new Paint();
int action;
int draw=0;
Boolean finger=true;
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
paint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Path path = new Path();
boolean first = true;
for(int i = 0; i < points.size(); i += 2){
Point point = points.get(i);
if(first){
first = false;
path.moveTo(point.x, point.y);
}
else if(i < points.size() - 1){
Point next = points.get(i + 1);
path.quadTo(point.x, point.y, next.x, next.y);
}
else {
path.lineTo(point.x, point.y);
}
}
canvas.drawPath(path, paint);
}
public boolean onTouch(View view, MotionEvent event) {
action = event.getAction();
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
points.add(point);
invalidate();
Log.d(TAG, "point: " + point);
if (action == MotionEvent.ACTION_MOVE) {
finger = true;
}
if (action == MotionEvent.ACTION_UP) {
finger = false;
}
draw++;
return true;
}
}
class Point {
float x, y;
@Override
public String toString() {
return x + ", " + y;
}
}
另外我认为也许我想要的是一个包含对象(路径和线条)的数组,但我不知道该怎么做。使用这种方法,我认为我可以回去删除最后一个对象,这可能吗?