一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android不規(guī)則封閉區(qū)域填充色彩的實例代碼

Android不規(guī)則封閉區(qū)域填充色彩的實例代碼

2022-02-15 17:48鴻洋_ Android

這篇文章主要介紹了Android不規(guī)則封閉區(qū)域填充色彩的實例代碼, 具有很好的參考價值,希望對大家有所幫助,一起跟隨小編過來看看吧

一、概述

在上一篇的敘述中,我們通過圖層的方式完成了圖片顏色的填充(詳情請戳:Android不規(guī)則圖像填充顏色小游戲),不過在著色游戲中更多的還是基于邊界的圖像的填充。本篇博客將詳細描述。

圖像的填充有2種經典算法。

一種是種子填充法。

種子填充法理論上能夠填充任意區(qū)域和圖形,但是這種算法存在大量的反復入棧和大規(guī)模的遞歸,降低了填充效率。

另一種是掃描線填充法。

注意:實際上圖像填充的算法還是很多的,有興趣可以去Google學術上去搜一搜。

ok,下面先看看今天的效果圖:

Android不規(guī)則封閉區(qū)域填充色彩的實例代碼

ok,可以看到這樣的顏色填充比上一篇的基于層的在素材的準備上要easy 很多~~~

二、原理分析

首先我們簡述下原理,我們在點擊的時候拿到點擊點的”顏色”,然后按照我們選擇的算法進行填色即可。

算法1:種子填充法,四聯通/八聯通

算法簡介:假設要將某個區(qū)域填充成紅色。

從用戶點擊點的像素開始,上下左右(八聯通還有左上,左下,右上,右下)去判斷顏色,如果四個方向上的顏色與當前點擊點的像素一致,則改變顏色至目標色。然后繼續(xù)上述這個過程。

ok,可以看到這是一個遞歸的過程,1個點到4個,4個到16個不斷的去延伸。如果按照這種算法,你會寫出類似這樣的代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * @param pixels 像素數組
 * @param w 寬度
 * @param h 高度
 * @param pixel 當前點的顏色
 * @param newColor 填充色
 * @param i 橫坐標
 * @param j 縱坐標
 */
 private void fillColor01(int[] pixels, int w, int h, int pixel, int newColor, int i, int j)
 {
 int index = j * w + i;
 if (pixels[index] != pixel || i >= w || i < 0 || j < 0 || j >= h)
 return;
 pixels[index] = newColor;
 //上
 fillColor01(pixels, w, h, pixel, newColor, i, j - 1);
 //右
 fillColor01(pixels, w, h, pixel, newColor, i + 1, j);
 //下
 fillColor01(pixels, w, h, pixel, newColor, i, j + 1);
 //左
 fillColor01(pixels, w, h, pixel, newColor, i - 1, j);
 }

代碼很簡單,但是如果你去運行,會發(fā)生StackOverflowException異常,這個異常主要是因為大量的遞歸造成的。雖然簡單,但是在移動設備上使用該方法不行。

于是,我就想,這個方法不是遞歸深度過多么,那么我可以使用一個Stack去存像素點,減少遞歸的深度和次數,于是我把代碼改成如下的方式:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * @param pixels 像素數組
 * @param w 寬度
 * @param h 高度
 * @param pixel 當前點的顏色
 * @param newColor 填充色
 * @param i 橫坐標
 * @param j 縱坐標
 */
 private void fillColor(int[] pixels, int w, int h, int pixel, int newColor, int i, int j)
 {
 mStacks.push(new Point(i, j));
 
 while (!mStacks.isEmpty())
 {
 Point seed = mStacks.pop();
 Log.e("TAG", "seed = " + seed.x + " , seed = " + seed.y);
 
 int index = seed.y * w + seed.x;
 
 pixels[index] = newColor;
 if (seed.y > 0)
 {
 int top = index - w;
 if (pixels[top] == pixel)
 {
 
 mStacks.push(new Point(seed.x, seed.y - 1));
 }
 }
 
 if (seed.y < h - 1)
 {
 int bottom = index + w;
 if (pixels[bottom] == pixel)
 {
 mStacks.push(new Point(seed.x, seed.y + 1));
 }
 }
 
 if (seed.x > 0)
 {
 int left = index - 1;
 if (pixels[left] == pixel)
 {
 mStacks.push(new Point(seed.x - 1, seed.y));
 }
 }
 
 if (seed.x < w - 1)
 {
 int right = index + 1;
 if (pixels[right] == pixel)
 {
 mStacks.push(new Point(seed.x + 1, seed.y));
 }
 }
 
 }
 }

方法的思想也比較簡單,將當前像素點入棧,然后出棧著色,接下來分別判斷四個方向的,如果符合條件也進行入棧(只要棧不為空持續(xù)運行)。ok,這個方法我也嘗試跑了下,恩,這次不會報錯了,但是速度特別的慢~~~~慢得我是不可接受的。(有興趣可以嘗試,記得如果ANR,點擊等待)。

這樣來看,第一種算法,我們是不考慮了,沒有辦法使用,主要原因是假設對于矩形同色區(qū)域,都是需要填充的,而算法一依然是各種入棧。于是考慮第二種算法

掃描線填充法

算法思想[4]:

初始化一個空的棧用于存放種子點,將種子點(x, y)入棧;
判斷棧是否為空,如果棧為空則結束算法,否則取出棧頂元素作為當前掃描線的種子點(x, y),y是當前的掃描線;
從種子點(x, y)出發(fā),沿當前掃描線向左、右兩個方向填充,直到邊界。分別標記區(qū)段的左、右端點坐標為xLeft和xRight;
分別檢查與當前掃描線相鄰的y - 1和y + 1兩條掃描線在區(qū)間[xLeft, xRight]中的像素,從xRight開始向xLeft方向搜索,假設掃描的區(qū)間為AAABAAC(A為種子點顏色),那么將B和C前面的A作為種子點壓入棧中,然后返回第(2)步;

上述參考自參考文獻[4],做了些修改,文章[4]中描述算法,測試有一點問題,所以做了修改.

可以看到該算法,基本上是一行一行著色的,這樣的話在大塊需要著色區(qū)域的效率比算法一要高很多。

ok,關于算法的步驟大家目前覺得模糊,一會可以參照我們的代碼。選定了算法以后,接下來就開始編碼了。

三、編碼實現

我們代碼中引入了一個邊界顏色,如果設置的話,著色的邊界參考為該邊界顏色,否則會只要與種子顏色不一致為邊界。

(一)構造方法與測量

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class ColourImageView extends ImageView
{
 
 private Bitmap mBitmap;
 /**
 * 邊界的顏色
 */
 private int mBorderColor = -1;
 
 private boolean hasBorderColor = false;
 
 private Stack<Point> mStacks = new Stack<Point>();
 
 public ColourImageView(Context context, AttributeSet attrs)
 {
 super(context, attrs);
 
 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ColourImageView);
 mBorderColor = ta.getColor(R.styleable.ColourImageView_border_color, -1);
 hasBorderColor = (mBorderColor != -1);
 
 L.e("hasBorderColor = " + hasBorderColor + " , mBorderColor = " + mBorderColor);
 
 ta.recycle();
 
 }
 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
 {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
 int viewWidth = getMeasuredWidth();
 int viewHeight = getMeasuredHeight();
 
 //以寬度為標準,等比例縮放view的高度
 setMeasuredDimension(viewWidth,
 getDrawable().getIntrinsicHeight() * viewWidth / getDrawable().getIntrinsicWidth());
 L.e("view's width = " + getMeasuredWidth() + " , view's height = " + getMeasuredHeight());
 
 //根據drawable,去得到一個和view一樣大小的bitmap
 BitmapDrawable drawable = (BitmapDrawable) getDrawable();
 Bitmap bm = drawable.getBitmap();
 mBitmap = Bitmap.createScaledBitmap(bm, getMeasuredWidth(), getMeasuredHeight(), false);
 }

可以看到我們選擇的是繼承ImageView,這樣只需要將圖片設為src即可。
構造方法中獲取我們的自定義邊界顏色,當然可以不設置~~
重寫測量的目的是為了獲取一個和View一樣大小的Bitmap便于我們操作。

接下來就是點擊啦~

(二)onTouchEvent

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@Override
 public boolean onTouchEvent(MotionEvent event)
 {
 final int x = (int) event.getX();
 final int y = (int) event.getY();
 if (event.getAction() == MotionEvent.ACTION_DOWN)
 {
 //填色
 fillColorToSameArea(x, y);
 }
 
 return super.onTouchEvent(event);
 }
 
 /**
 * 根據x,y獲得改點顏色,進行填充
 *
 * @param x
 * @param y
 */
 private void fillColorToSameArea(int x, int y)
 {
 Bitmap bm = mBitmap;
 
 int pixel = bm.getPixel(x, y);
 if (pixel == Color.TRANSPARENT || (hasBorderColor && mBorderColor == pixel))
 {
 return;
 }
 int newColor = randomColor();
 
 int w = bm.getWidth();
 int h = bm.getHeight();
 //拿到該bitmap的顏色數組
 int[] pixels = new int[w * h];
 bm.getPixels(pixels, 0, w, 0, 0, w, h);
 //填色
 fillColor(pixels, w, h, pixel, newColor, x, y);
 //重新設置bitmap
 bm.setPixels(pixels, 0, w, 0, 0, w, h);
 setImageDrawable(new BitmapDrawable(bm));
 
 }

可以看到,我們在onTouchEvent中獲取(x,y),然后拿到改點坐標:

獲得點擊點顏色,獲得整個bitmap的像素數組

改變這個數組中的顏色

然后重新設置給bitmap,重新設置給ImageView

重點就是通過fillColor去改變數組中的顏色

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
 * @param pixels 像素數組
 * @param w 寬度
 * @param h 高度
 * @param pixel 當前點的顏色
 * @param newColor 填充色
 * @param i 橫坐標
 * @param j 縱坐標
 */
 private void fillColor(int[] pixels, int w, int h, int pixel, int newColor, int i, int j)
 {
 //步驟1:將種子點(x, y)入棧;
 mStacks.push(new Point(i, j));
 
 //步驟2:判斷棧是否為空,
 // 如果棧為空則結束算法,否則取出棧頂元素作為當前掃描線的種子點(x, y),
 // y是當前的掃描線;
 while (!mStacks.isEmpty())
 {
 
 
 /**
 * 步驟3:從種子點(x, y)出發(fā),沿當前掃描線向左、右兩個方向填充,
 * 直到邊界。分別標記區(qū)段的左、右端點坐標為xLeft和xRight;
 */
 Point seed = mStacks.pop();
 //L.e("seed = " + seed.x + " , seed = " + seed.y);
 int count = fillLineLeft(pixels, pixel, w, h, newColor, seed.x, seed.y);
 int left = seed.x - count + 1;
 count = fillLineRight(pixels, pixel, w, h, newColor, seed.x + 1, seed.y);
 int right = seed.x + count;
 
 
 /**
 * 步驟4:
 * 分別檢查與當前掃描線相鄰的y - 1和y + 1兩條掃描線在區(qū)間[xLeft, xRight]中的像素,
 * 從xRight開始向xLeft方向搜索,假設掃描的區(qū)間為AAABAAC(A為種子點顏色),
 * 那么將B和C前面的A作為種子點壓入棧中,然后返回第(2)步;
 */
 //從y-1找種子
 if (seed.y - 1 >= 0)
 findSeedInNewLine(pixels, pixel, w, h, seed.y - 1, left, right);
 //從y+1找種子
 if (seed.y + 1 < h)
 findSeedInNewLine(pixels, pixel, w, h, seed.y + 1, left, right);
 }
 
 }

可以看到我已經很清楚的將該算法的四個步驟標識到該方法中。好了,最后就是一些依賴的細節(jié)上的方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* 在新行找種子節(jié)點
*
* @param pixels
* @param pixel
* @param w
* @param h
* @param i
* @param left
* @param right
*/
private void findSeedInNewLine(int[] pixels, int pixel, int w, int h, int i, int left, int right)
{
/**
* 獲得該行的開始索引
*/
int begin = i * w + left;
/**
* 獲得該行的結束索引
*/
int end = i * w + right;
 
boolean hasSeed = false;
 
int rx = -1, ry = -1;
 
ry = i;
 
/**
* 從end到begin,找到種子節(jié)點入棧(AAABAAAB,則B前的A為種子節(jié)點)
*/
while (end >= begin)
{
if (pixels[end] == pixel)
{
if (!hasSeed)
{
rx = end % w;
mStacks.push(new Point(rx, ry));
hasSeed = true;
}
} else
{
hasSeed = false;
}
end--;
}
}
 
/**
* 往右填色,返回填充的個數
*
* @return
*/
private int fillLineRight(int[] pixels, int pixel, int w, int h, int newColor, int x, int y)
{
int count = 0;
 
while (x < w)
{
//拿到索引
int index = y * w + x;
if (needFillPixel(pixels, pixel, index))
{
pixels[index] = newColor;
count++;
x++;
} else
{
break;
}
 
}
 
return count;
}
 
 
/**
* 往左填色,返回填色的數量值
*
* @return
*/
private int fillLineLeft(int[] pixels, int pixel, int w, int h, int newColor, int x, int y)
{
int count = 0;
while (x >= 0)
{
//計算出索引
int index = y * w + x;
 
if (needFillPixel(pixels, pixel, index))
{
pixels[index] = newColor;
count++;
x--;
} else
{
break;
}
 
}
return count;
}
 
private boolean needFillPixel(int[] pixels, int pixel, int index)
{
if (hasBorderColor)
{
return pixels[index] != mBorderColor;
} else
{
return pixels[index] == pixel;
}
}
 
/**
* 返回一個隨機顏色
*
* @return
*/
private int randomColor()
{
Random random = new Random();
int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
return color;
}

ok,到此,代碼就介紹完畢了~~~

最后貼下布局文件~~

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:zhy="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 android:paddingBottom="@dimen/activity_vertical_margin"
 tools:context=".MainActivity">
 <com.zhy.colour_app_01.ColourImageView
 zhy:border_color="#FF000000"
 android:src="@drawable/image_007"
 android:background="#33ff0000"
 android:layout_width="match_parent"
 android:layout_centerInParent="true"
 android:layout_height="match_parent"/>
 
</RelativeLayout>
 
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="ColourImageView">
 <attr name="border_color" format="color|reference"></attr>
 </declare-styleable>
</resources>

 

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:https://blog.csdn.net/lmj623565791/article/details/45954255

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: www.久久99| 午夜秀场在线观看 | 国产在线精品观看 | 欧美同性gayvidoes| 小仙夜晚慰自催眠mp3护士篇 | 免费国产成人α片 | 青草视频网站 | 成年人视频免费在线播放 | 日本动漫打扑克动画片樱花动漫 | 能播放的欧美同性videos | 欧美亚洲另类在线观看 | 亚洲精品福利一区二区在线观看 | 国产一卡二卡3卡4卡更新 | 国产午夜免费视频 | free service性v极品 | 男人天堂影院 | 国产日韩精品欧美一区 | 互换身体全集免费观看 | 欧美一级片免费看 | ccc在线在线36| 波多野结衣在线看 | 亚洲成人看片 | 亚洲精品一区二区观看 | 日本色女 | 娇小XXXXX第一次出血 | 国产一级视频久久 | 国产精品免费网站 | japonensis中国东北老人 | 国产麻豆剧果冻传媒观看免费视频 | 国产亚洲欧美日韩俺去了 | 国产一级大片免费看 | 91视频国产精品 | 国产成+人+综合+亚洲不卡 | poronovideos极度残酷 | 欧美久久天天综合香蕉伊 | 久久精品嫩草影院免费看 | 884hutv四虎永久7777 | 国产新疆成人a一片在线观看 | 青草久久精品亚洲综合专区 | 果冻传媒在线视频播放观看 | 日本一在线中文字幕天堂 |