在绘制路径时更改部分路径的颜色

时间:2016-02-08 10:47:45

标签: android canvas

我设法创建了自己的自定义路径绘图应用程序,如下所示

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 = Math.abs(yTouch - locToPath.get(loc).lastPoint.y);
    if (dx >= 5 || dy >= 5) {
        locToPath.get(loc).path.quadTo(locToPath.get(loc).lastPoint.x, locToPath.get(loc).lastPoint.y, (xTouch + locToPath.get(loc).lastPoint.x) / 2, (yTouch + locToPath.get(loc).lastPoint.y) / 2);
        locToPath.get(loc).lastPoint = new Point(xTouch, yTouch);
        if(loc==myLocation){
            screenRuntime.getRewriteMachine().addActionMove(xTouch, yTouch);
        }
    }

}
public void respondActionUp(final Integer loc,int x,int y){
    locToPath.get(loc).path.lineTo(locToPath.get(loc).lastPoint.x, locToPath.get(loc).lastPoint.y);

    if(loc==myLocation){
        screenRuntime.getRewriteMachine().addActionUp(x, y);
    }
    activePaths.remove(locToPath.get(loc));
    locToPath.get(loc).path.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    if(inited) {
        int xTouch;
        int yTouch;
        xTouch = (int) event.getX(0);
        yTouch = (int) event.getY(0);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                respondActionDown(myLocation,xTouch,yTouch);
                break;
            case MotionEvent.ACTION_MOVE:
                respondActionMove(myLocation, xTouch,yTouch);
                break;
            case MotionEvent.ACTION_UP:
                respondActionUp(myLocation, xTouch,yTouch);
                break;
        }
        return true;
    }
    return false;
}

此代码适用于我的应用程序(忽略位置内容和运行时以及重写机器人的东西)。

我的问题是,我希望路径的某些部分有不同的颜色,最终的目标是我只希望路径的最后几个像素可见,其余部分的Alpha应为0,例如当用户绘制时,他只看到路径的最后几个像素然后慢慢变为不可见。这可能吗?如果是的话,我该怎么做?

感谢。

1 个答案:

答案 0 :(得分:2)

不是向路径添加点,而是创建路径列表,每次向列表添加新路径,该路径只有一个从上一个路径的终点开始的小块,并且只有一个其他点(终点)。然后,您可以使用不同的颜色绘制每个路径:

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);

请注意,path1与path2不同,更重要的是,您为每种颜色创建一个新的mPaint。如果您只是在先前创建和使用的油漆上调用mPaint.setColor(Color.BLUE),我不确定它是否会起作用。