Android绘图填充工具 [英] Android Drawing Fill Tool

查看:68
本文介绍了Android绘图填充工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a drawing app for android and I need some help adding a fill tool.

我希望该工具充满水,并且表现得像在Microsoft Paint中一样,但是在手机上.

I want the tool to flood fill, and to behave like it would in Microsoft Paint, but on a phone.

我有一个自定义视图,可在画布上绘制路径.我为不同的笔和画笔绘制了不同的路径,并且允许用户选择线条的粗细和颜色.

I have a custom view that draws a path on a canvas. I draw different paths for different pens and brushes, and I allow users to pick line thickness and color.

当我这样做时:

paint.setStyle(Paint.Style.FILL);

我画画,我没有得到想要的填充.

and I paint, I don't get a fill how I want.

我已经获得一些使用洪水填充算法"的建议,但是我不知道如何在代码中实现它.

I have gotten some suggestions to use the "Flood Fill Algorithm", but I can't figure out how to implement it in my code.

我在哪里可以看到我想做的事的例子?有人有示例代码向我展示如何使该工具与Android视图配合使用吗?

Where could I go to see an example of what I am trying to do? Does anyone have sample code to show me how I could make the tool work with my android view?

CartoonView.java:

CartoonView.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();
    }
}

推荐答案

我完全同意一些有关洪水填充算法的评论员.

I completely agree with the some of the commentators who said about Flood Fill Algorithm.

下面是可以根据需要工作的功能.试试吧:

Below is the function which works as what you want. Just try it:

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 Link2

You can also read some information regarding Flood fill on link: Link1, Link2 and Link3

希望您能回答问题.请让我知道,我可以通过其他方式帮助您,或者您对上述答案有疑问.

Hope you got answer to your question. Please let me know i can help you in another way or you have doubt in above answer.

享受编码...:)

更新了更多信息:

以上功能将基于FloodFill算法工作.它将要做的是, 无论您触摸到什么位置,它都会检测该像素上的像素点和颜色. 然后它将按选定的颜色逐像素填充颜色,直到任何像素的颜色不同于当前的颜色为止.

Above functiona will work on bases of the FloodFill algorithm. What it will exactly do is, It will detect the pixcel point and color on that pixel whereever you touch. Then it will fill the color by pixel to pixel by the selected color till any pixel dont have different color then it have currently.

希望它能对您有所帮助.

I hope it will help you.

这篇关于Android绘图填充工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆