gpt4 book ai didi

安卓绘图填充工具

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:10:36 25 4
gpt4 key购买 nike

我正在为 Android 制作绘图应用程序,我需要一些帮助来添加填充工具。

我希望该工具能够进行洪水填充,并像在 Microsoft Paint 中一样运行,但在手机上。

我有一个在 Canvas 上绘制路径的自定义 View 。我用不同的笔和画笔绘制不同的路径,我允许用户选择线条粗细和颜色。

当我这样做时:

paint.setStyle(Paint.Style.FILL);

然后我画了,我没有得到我想要的填充。

我得到了一些使用“洪水填充算法”的建议,但我不知道如何在我的代码中实现它。

我在哪里可以看到我正在尝试做的事情的例子?有没有人有示例代码来告诉我如何使该工具与我的 android View 一起工作?

编辑:

卡通 View .java:

    public class CartoonView extends View {
ArrayList<Paint> paints = new ArrayList<Paint>();
ArrayList<Path> paths = new ArrayList<Path>();
int color;
int thickness;
boolean pencilSelected;

public boolean isPencilSelected() {
return pencilSelected;
}

public void setPencilSelected(boolean pencilSelected) {
this.pencilSelected = pencilSelected;
}

public ArrayList<Paint> getPaints() {
return paints;
}

public void setPaints(ArrayList<Paint> paints) {
this.paints = paints;
}

public ArrayList<Path> getPaths() {
return paths;
}

public void setPaths(ArrayList<Path> 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();
}
}

最佳答案

我完全同意一些评论员对 Flood Fill 算法的看法。

以下是您想要的功能。试试吧:

private void FloodFill(Bitmap bmp, Point pt, int targetColor, int replacementColor){
Queue<Point> q = new LinkedList<Point>();
q.add(pt);
while (q.size() > 0) {
Point n = q.poll();
if (bmp.getPixel(n.x, n.y) != targetColor)
continue;

Point w = n, e = new Point(n.x + 1, n.y);
while ((w.x > 0) && (bmp.getPixel(w.x, w.y) == targetColor)) {
bmp.setPixel(w.x, w.y, replacementColor);
if ((w.y > 0) && (bmp.getPixel(w.x, w.y - 1) == targetColor))
q.add(new Point(w.x, w.y - 1));
if ((w.y < bmp.getHeight() - 1)
&& (bmp.getPixel(w.x, w.y + 1) == targetColor))
q.add(new Point(w.x, w.y + 1));
w.x--;
}
while ((e.x < bmp.getWidth() - 1)
&& (bmp.getPixel(e.x, e.y) == targetColor)) {
bmp.setPixel(e.x, e.y, replacementColor);

if ((e.y > 0) && (bmp.getPixel(e.x, e.y - 1) == targetColor))
q.add(new Point(e.x, e.y - 1));
if ((e.y < bmp.getHeight() - 1)
&& (bmp.getPixel(e.x, e.y + 1) == targetColor))
q.add(new Point(e.x, e.y + 1));
e.x++;
}
}}

您还可以在链接上阅读有关洪水填充的一些信息:Link1 , Link2Link3

希望您的问题得到解答。请让我知道我可以用其他方式帮助您,或者您对上述答案有疑问。

享受编码......:)

更新了更多信息:

以上功能将在 FloodFill 算法的基础上运行。它真正要做的是,无论您触摸哪里,它都会检测该像素上的像素点和颜色。然后它将根据所选颜色逐个像素地填充颜色,直到任何像素的颜色都与当前颜色不同为止。

希望对您有所帮助。

关于安卓绘图填充工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24786923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com