gedo February 2016

Changing the color of part of a path while it is being drawn

I managed to create my own custom path drawing application and it is as follows

public class CanvasView extends View {
Context context;
HashMap<Integer,PathWrapper> locToPath=new HashMap<>();
ArrayList<PathWrapper> activePaths=new ArrayList<>();
CoMingleAndroidRuntime<Screenshare> screenRuntime;
boolean inited=false;
Integer myLocation;
public CanvasView(Context context,AttributeSet attr) {
    super(context, attr);
    setWillNotDraw(false);
    this.context = context;
}

public void init(CoMingleAndroidRuntime<Screenshare> screenRuntime){
    inited=true;
    this.screenRuntime=screenRuntime;
    this.myLocation=screenRuntime.getLocation();
    addPath(myLocation);
    invalidate();
}

public void addPath(int Location){
    Paint mPaint=new Paint();
    mPaint.setColor(Color.BLACK);
    mPaint.setAlpha(195);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeWidth(50f);
    locToPath.put(Location, new PathWrapper(new Path(), mPaint, Location));
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    for(PathWrapper path:activePaths){
        canvas.drawPath(path.path, path.paint);
    }
    invalidate();
}
public void respondActionColorChanged(int R,int G,int B){
    locToPath.get(myLocation).paint.setColor(Color.rgb(R, G, B));
}
public void respondActionColorChanged(int loc,int R,int G,int B){
    locToPath.get(loc).paint.setColor(Color.rgb(R, G, B));
}
public void respondActionDown(final Integer loc, int xTouch,int yTouch){
    activePaths.add(locToPath.get(loc));
    locToPath.get(loc).path.moveTo(xTouch, yTouch);
    locToPath.get(loc).lastPoint = new Point(xTouch, yTouch);
    if(loc==myLocation){
        screenRuntime.getRewriteMachine().addActionDown(xTouch, yTouch);
    }
}
public void respondActionMove(final Integer loc,int xTouch,int yTouch){
    float dx = Math.abs(xTouch - locToPath.get(loc).lastPoint.x);
    float dy =        

Answers


Gavriel February 2016

Instead of adding points to a path, create a list of paths, and every time add a new path to the list that has only a small chunk that starts at the end point of the previous path, and has only one other point (end-point). Then you can draw each path with a different color:

Paint mPaint=new Paint();
mPaint.setColor(Color.BLACK);
//rest of mPaint...
canvas.drawPath(path1, mPaint);

mPaint=new Paint();
mPaint.setColor(Color.BLUE);
//rest of mPaint...
canvas.drawPath(path2, mPaint);

Note that path1 is different from path2, and more importantly you create a new mPaint for each color. I'm not sure if it would work if you just would call mPaint.setColor(Color.BLUE) on the previously created and used paint.

Post Status

Asked in February 2016
Viewed 1,151 times
Voted 14
Answered 1 times

Search




Leave an answer