Issue
I am drawing closed paths in android canvas using finger movement. Here is my code for that
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
startPoint = new PointF(event.getX(), event.getY());
endPoint = new PointF();
hotSpot = new HotSpots();
endPoint.x = event.getX();
endPoint.y = event.getY();
wallpath = new Path();
wallpath.moveTo(endPoint.x,endPoint.y);
break;
case MotionEvent.ACTION_MOVE:
PointF point = new PointF(event.getX(),event.getY());
endPoint.x = event.getX();
endPoint.y = event.getY();
invalidate();
break;
case MotionEvent.ACTION_UP:
endPoint.x = startPoint.x;//event.getX();
isFinished = true;
break;
default:
break;
}
@Override
protected void onDraw(Canvas canvas)
{
wallpath.lineTo(endPoint.x, endPoint.y);
canvas.drawPath(wallpath, paint);
if(isFinished)
{
wallpath.lineTo(endPoint.x, endPoint.y);
canvas.drawPath(wallpath, paint);
}
}
On every new touch I am creating a new path. But when a new path is drawn the old path gets erased. How can I draw multiple paths on canvas with different fill colors to each path.
Solution
When ACTION_DOWN is the triggered event, you are resetting wallpath with the wallpath = new Path() statement, abandoning the previous value. This new path is the only thing that is being drawn in your onDraw method, which is called every time the canvas needs to be redrawn.
Consider building a List of paths, adding a new path to the list when ACTION_DOWN is the triggered event, and in your onDraw method, draw every path in the list.
Details:
Elsewhere in the code, add:
pathList = new ArrayList<Path>;
In ACTION_DOWN case, add a line:
wallpath = new Path();
pathList.add(wallpath); // <-- Add this line.
In the ACTION_MOVE case:
endPoint.x = event.getX();
endPoint.y = event.getY();
wallpath.lineTo(endPoint.x, endPoint.y); // <-- Add this line.
Finally, replace the code in your onDraw method with the following:
for (Path path : pathList) {
canvas.drawPath(path, paint);
}
Remove the conditional, it is unnecessary.
Answered By - Wayne McGee
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.