Android irregular closed area fill color example code

1、 Overview

In the previous description, we completed the filling of image color through layers (please stamp: Android irregular image filling color game for details), but in the coloring game, we still fill the image based on the boundary. This blog will describe in detail.

There are two classical algorithms for image filling.

One is the seed filling method.

In theory, seed filling method can fill any region and graph, but this algorithm has a large number of repeated stacking and large-scale recursion, which reduces the filling efficiency.

The other is the scanning line filling method.

Note: in fact, there are still many image filling algorithms. If you are interested, you can search Google academic.

OK, let's take a look at today's renderings:

OK, you can see that the color filling is much easier than the layer based material preparation in the previous article~~~

2、 Principle analysis

First, let's briefly describe the principle. When we click, we get the "color" of the click point, and then fill in the color according to the algorithm we choose.

Algorithm 1: seed filling method, four Unicom / eight Unicom

Algorithm Introduction: suppose you want to fill an area with red.

Starting from the pixel of the user's click point, judge the color up, down, left and right (eight Unicom also has upper left, lower left, upper right and lower right). If the color in the four directions is consistent with the pixel of the current click point, change the color to the target color. Then continue the above process.

OK, you can see that this is a recursive process, from 1 point to 4 points, and from 4 to 16 points. If you follow this algorithm, you will write code like this:

The code is very simple, but if you run it, a stackoverflowexception will occur, which is mainly caused by a large number of recursion. Although simple, using this method on mobile devices does not work.

Therefore, I thought that this method is not too deep recursion, so I can use a stack to save pixels to reduce the depth and number of recursion, so I changed the code to the following way:

The idea of the method is also relatively simple. Put the current pixel into the stack, and then color it out of the stack. Next, judge the pixels in four directions respectively. If they meet the conditions, put them into the stack (as long as the stack is not empty and runs continuously). OK, I also tried to run this method. Well, I won't report mistakes this time, but the speed is very slow ~ ~ ~ it's too slow for me to accept. (if you are interested, you can try. Remember to click wait if ANR).

From this point of view, the first algorithm is not considered and cannot be used. The main reason is that it is assumed that rectangular areas of the same color need to be filled, and algorithm 1 is still a variety of stacking. So consider the second algorithm

Scan line filling method

Algorithm idea [4]:

The above reference is from reference [4], and some modifications have been made. The algorithm described in article [4] has some problems in the test, so it has been modified

It can be seen that the algorithm is basically colored line by line. In this way, the efficiency of coloring areas in large blocks is much higher than that of algorithm 1.

OK, the steps of the algorithm are vague. You can refer to our code later. After the algorithm is selected, the next step is coding.

3、 Coding implementation

A boundary color is introduced into our code. If it is set, the colored boundary reference is the boundary color. Otherwise, it will be the boundary as long as it is inconsistent with the seed color.

(1) Construction method and measurement

You can see that we chose to inherit ImageView, so we just need to set the image to Src. Obtain our custom boundary color in the construction method. Of course, it can not be set ~ ~ the purpose of rewriting the measurement is to obtain a bitmap of the same size as view for our operation.

The next step is to click~

(2) Ontouchevent

As you can see, we get (x, y) in ontouchevent, and then get the change point coordinates:

The point is to change the color in the array through fillcolor

You can see that I have clearly identified the four steps of the algorithm into the method. Well, finally, there are some dependent details:

OK, this is the end of the code introduction~~~

Finally, paste the layout file~~

Reference link

Analysis of scan line seed filling algorithm

Flood fill algorithm for image processing

Recursive seed filling algorithm

Scan line seed filling algorithm

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>