我正在为
Android制作一个绘图应用程序,我需要一些帮助添加填充工具.
我希望该工具能够填充填充,并且在Microsoft Paint中表现得像在手机上一样.
我有一个自定义视图,在画布上绘制路径.我为不同的笔和画笔绘制了不同的路径,我允许用户选择线条粗细和颜色.
当我做:
paint.setStyle(Paint.Style.FILL);
而且我画画,我没有得到我想要的填充.
我已经得到了一些使用“Flood Fill Algorithm”的建议,但我无法弄清楚如何在我的代码中实现它.
我在哪里可以看到我想要做的一个例子?有没有人有示例代码向我展示如何使用我的android视图使该工具工作?
编辑:
CartoonView.java:
public class CartoonView extends View {
ArrayList paints = new ArrayList();
ArrayList paths = new ArrayList();
int color;
int thickness;
boolean pencilSelected;
public boolean isPencilSelected() {
return pencilSelected;
}
public void setPencilSelected(boolean pencilSelected) {
this.pencilSelected = pencilSelected;
}
public ArrayList getPaints() {
return paints;
}
public void setPaints(ArrayList paints) {
this.paints = paints;
}
public ArrayList getPaths() {
return paths;
}
public void setPaths(ArrayList paths) {
this.paths = paths;
}
public int getThickness() {
return thickness;
}
public int getColor() {
return color;
}
public CartoonView(Context context, AttributeSet attrs) {
super(context, attrs);
color = Color.BLACK;
thickness = 3;
pencilSelected = true;
createPaint();
}
@Override
protected void onDraw(Canvas canvas) {
for (Path path : paths) {
canvas.drawPath(path, paints.get(paths.indexOf(path)));
}
}
public void setPaintColor(int newColor) {
color = newColor;
createPaint();
}
public void setPaintThickness(int newThickness) {
thickness = newThickness;
createPaint();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.isEnabled()) {
Path path;
if (paths.size() == 0) {
path = new Path();
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5f);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setColor(color);
paint.setStrokeWidth(thickness);
thickness = (int) paint.getStrokeWidth();
paths.add(path);
paints.add(paint);
} else {
path = paths.get(paths.size() - 1);
}
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
default:
return true;
}
invalidate();
}
return true;
}
public void createPaint() {
Path path = new Path();
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5f);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setColor(color);
paint.setStrokeWidth(thickness);
paths.add(path);
paints.add(paint);
}
public void clearView(){
paths.clear();
paints.clear();
invalidate();
}
}
博主正在开发一个Android绘图应用,遇到问题在于尝试添加填充工具,希望其功能类似于Microsoft Paint中的填充。目前,应用已能绘制不同路径并允许用户自定义线条颜色和粗细。但使用`Paint.Style.FILL`时,填充效果不理想。博主考虑使用FloodFillAlgorithm,但不清楚如何在代码中实现。在CartoonView.java文件中,博主分享了部分代码,并寻求关于如何在自定义视图中实现填充功能的示例代码或指导。
404

被折叠的 条评论
为什么被折叠?



