我自己想通了......所描述的行为似乎是一个已知问题。我想这是因为每条路径在内部都被视为一个单独的“轮廓”,不能与另一个路径合并。
但是,我能够通过简单的“hack”获得所需的行为。不过,这只适用于基于行的路径。这是代码(最小示例),如果有人感兴趣的话......
public class MyPath extends Path {
private ArrayList<Float> points = new ArrayList<Float>();
public void addPath(MyPath src) {
this.points.addAll(src.getPoints());
super.rewind();
super.moveTo(this.points.get(0), this.points.get(1));
for (int i = 2; i < this.points.size(); i += 2) {
super.lineTo(this.points.get(i), this.points.get(i+1));
}
}
@Override
public void moveTo(float x, float y) {
this.points.add(x);
this.points.add(y);
super.moveTo(x, y);
}
@Override
public void lineTo(float x, float y) {
this.points.add(x);
this.points.add(y);
super.lineTo(x, y);
}
public ArrayList<Float> getPoints() {
return this.points;
}
}
这个用法很简单:
MyPath p1 = new MyPath();
MyPath p2 = new MyPath();
p1.moveTo(0,0); p1.lineTo(200,200);
p2.moveTo(200,200); p2.lineTo(400,0);
p1.addPath(p2); //merge
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawColor(Color.WHITE);
canvas.drawTextOnPath("Lorem ipsum dolor sit amet", p1, 0, 0, paint);