I'm working on a drawing app. It's fairly simple, uses onTouchEvent, paths, and canvas.drawPath. Whenever the user does a quick swipe and releases without stopping, the last segment of the path blinks continuously. In the ACTION_UP condition, I update the path and set the boolean resetPath to true, and in the onDraw, I draw the path, and if resetPath is true, I call path.reset() and then set resetPath to false. What am I doing wrong?
class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
private DrawingThread _thread;
private Path path;
private Boolean resetPath = false;
public DrawingPanel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new DrawingThread(getHolder(), this);
path = new Path();
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 25;
public boolean onTouchEvent(MotionEvent event) {
synchronized (_thread.getSurfaceHolder()) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
path.reset();
path = new Path();
path.moveTo(event.getX(), event.getY());
mX = event.getX();
mY = event.getY();
}else if(event.getAction() == MotionEvent.ACTION_MOVE){
float dx = Math.abs(event.getX() - mX);
float dy = Math.abs(event.getY() - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
path.quadTo(mX, mY, (event.getX() + mX)/2, (event.getY() + mY)/2);
mX = event.getX();
mY = event.getY();
}
}else if(event.getAction() == MotionEvent.ACTION_UP){
resetPath = true;
}
return true;
}
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawPath(path, mPaint);
if (resetPath) {
path.reset();
resetPath = false;
}
}