PixelMap下填色功能的实现
Tiechui.Wang 2021-04-14 10:00:00 发布3300 浏览 10 点赞 19 收藏
本文利用FloodFill算法实现PixelMap的填色(油漆桶)效果。
关于FloodFill算法的介绍,请查看这篇文章:https://blog.csdn.net/lion19930924/article/details/54293661
下面介绍鸿蒙系统下填色功能的实现:
定义一个算法类,用于实现FloodFill算法:
public class FloodFillAlgorithm {
private PixelMap fillBitmap;
private int width;
private int height;
private boolean isFillColor = true;
private int maxStackSize = 500;
private int[] xstack = new int[maxStackSize];
private int[] ystack = new int[maxStackSize];
private int stackSize;
public FloodFillAlgorithm(PixelMap fillBitmap) {
this.fillBitmap = fillBitmap;
width = fillBitmap.getImageInfo().size.width;
height = fillBitmap.getImageInfo().size.height;
}
public int getColor(int x, int y) {
return fillBitmap.readPixel(new Position(x, y));
}
public void setColor(int x, int y, int newColor) {
fillBitmap.writePixel(new Position(x, y), newColor);
}
public boolean isFill() {
return isFillColor;
}
public void floodFillScanLineWithStack(int x, int y, int newColor, int oldColor) {
if (oldColor == newColor) {
System.out.println("do nothing !!!, filled area!!");
return;
}
emptyStack();
int y1;
boolean spanLeft, spanRight;
push(x, y);
while (true) {
x = popx();
if (x == -1) return;
y = popy();
y1 = y;
while (y1 >= 0 && getColor(x, y1) == oldColor) y1--; // go to line top/bottom
y1++; // start from line starting point pixel
spanLeft = spanRight = false;
while (y1 < height && getColor(x, y1) == oldColor) {
setColor(x, y1, newColor);
if (!spanLeft && x > 0 && getColor(x - 1, y1) == oldColor)// just keep left line once in the stack
{
push(x - 1, y1);
spanLeft = true;
} else if (spanLeft && x > 0 && getColor(x - 1, y1) != oldColor) {
spanLeft = false;
}
if (!spanRight && x < width - 1 && getColor(x + 1, y1) == oldColor) // just keep right line once in the stack
{
push(x + 1, y1);
spanRight = true;
} else if (spanRight && x < width - 1 && getColor(x + 1, y1) != oldColor) {
spanRight = false;
}
y1++;
}
}
}
private void emptyStack() {
while (popx() != -1) {
popy();
}
stackSize = 0;
}
final void push(int x, int y) {
stackSize++;
if (stackSize == maxStackSize) {
int[] newXStack = new int[maxStackSize * 2];
int[] newYStack = new int[maxStackSize * 2];
System.arraycopy(xstack, 0, newXStack, 0, maxStackSize);
System.arraycopy(ystack, 0, newYStack, 0, maxStackSize);
xstack = newXStack;
ystack = newYStack;
maxStackSize *= 2;
}
xstack[stackSize - 1] = x;
ystack[stackSize - 1] = y;
}
final int popx() {
if (stackSize == 0)
return -1;
else
return xstack[stackSize - 1];
}
final int popy() {
int value = ystack[stackSize - 1];
stackSize--;
return value;
调用方式如下:
FloodFillAlgorithm floodFillAlgorithm = new FloodFillAlgorithm(pixelBitmap);
floodFillAlgorithm.floodFillScanLineWithStack(1, 1, Color.RED.getValue(), Color.GREEN.getValue());
效果如下:

至此,填色(油漆桶)效果已实现
©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
其它
标签
鸿蒙
相关推荐
鸿蒙实战项目案例_从零构建完整应用的完整复盘
周正
1
0鸿蒙NFC开发实战:从读卡器模式到卡模拟的完整实现
周正
1
0鸿蒙加密存储开发:数据安全保护的完整方案
周正
1
0鸿蒙应用签名与上架完整流程:从开发到发布的实战指南
周正
2
0鸿蒙版本兼容处理实战:让你的应用完美适配各版本系统
周正
1
0
Tiechui.Wang
我还没有写个人简介......
1091
帖子
0
提问
893
粉丝
最新发布
华为应用市场增长优化师火热认证中
2025-03-10 10:16:02 发布华为应用市场斩获Pocket Gamer2023年移动游戏奖,全球第三大移动应用生态获行业认可
2024-04-27 11:06:07 发布热门推荐