我为基于矢量的绘图程序制作了一个简单的橡皮擦工具。
我将List
中的路径保存为android.graphics.Path
,然后将它们循环绘制到带有canvas.drawPath(path, paint)
的画布上。在此示例中,红线是橡皮擦,绿线应该被擦除。
我尝试将Paths
转换为Regions
并调用redRegion.op(greenRegion, Region.OP.INTERSECT)
。我认为问题是Paths没有表面区域,因为Paint还没有应用,它们只是简单的线条。
我正在为API lvl 15构建。
答案 0 :(得分:0)
如果你的背景总是白色,你可以制作"橡皮擦"一条白色的道路。然后它会让它看起来像其他线被删除。
编辑:
也许您可以查询用于在触摸事件中绘制路径的所有点,并创建一个自定义类来封装它。然后有一个方法,如:
//other code....
MyCustomClass class = new MyCustomClass();
if(class.contains(float x, float y)) {
//...erase line
}
//other code
MyCustomClass
public class MyCustomClass {
private List<PathPoints> pathPoints;
public MyCustomClass(List<PathPoints> points) {
this.pathPoints = points;
}
public boolen contains(float x, float y) {
for(PathPoint point: pathPoints) {
if(x == point.getX() && y == point.getY()) {
return true;
}
return false;
}
}
}
和路径指向类
public class PathPoint {
private float x;
private float y;
private int movementType;
public void setX(float x) {
this.x = x;
}
//..all the other getters and setters.
}
然后在onTouch事件中将新的PathPoint添加到每个绘制路径的路径ponit列表中。只在Action.UP中创建一个新的路径点(用户已停止绘图)。
编辑2:好的,这是另一个想法。为什么不创建自己的自定义路径类,然后将所有这些逻辑添加到该类中。因此,当您绘制路径时,只需将路径点添加到路径即可。然后添加一个方法&#34; contains()&#34;到你的自定义路径类。这样它将包含路径上的每个点,无论它是移动到,行到,四边形还是其他什么。所以像这样:
public class MyPath extends Path {
private List<PathPoint> points;
public MyPath(List<PathPoints> pathPoints) {
this.points = pathPoints;
}
public void addPathPoint(PathPoint pathPoint) {
if(points != null) {
points.add(pathPoint);
}
}
public boolean contains(float x, float y) {
for(PathPoint pathPoint: points) {
if(pathPoint.getX() == x && pathPoint.getY() == y) {
return true;
}
}
return false;
}
}
它的绘图部分应该可以正常工作,因为你不接触它。让我知道这个是否奏效。