Android 无法在曲面视图中应用泛洪填充
如何在曲面视图中的图像上实现泛洪填充算法?我已经在活动中成功地使用了这个算法。 //这是我的课程,我尝试在图像的特定区域填充颜色 公共类DrawCanvas扩展了SurfaceView实现回调 {Android 无法在曲面视图中应用泛洪填充,android,android-canvas,Android,Android Canvas,如何在曲面视图中的图像上实现泛洪填充算法?我已经在活动中成功地使用了这个算法。 //这是我的课程,我尝试在图像的特定区域填充颜色 公共类DrawCanvas扩展了SurfaceView实现回调 { Bean; 画d; 字符串标签; 私人油漆; 专用路径; 位图mBitmap; 进展性帕金森病; 终点p1=新点(); 帆布; 专用静态最终浮动接触公差=4; 浮动mX,我的; 公共绘图画布(上下文) { 超级(上下文); } 公共绘图画布(上下文、属性集属性) { 超级(上下文,attrs); th
Bean;
画d;
字符串标签;
私人油漆;
专用路径;
位图mBitmap;
进展性帕金森病;
终点p1=新点();
帆布;
专用静态最终浮动接触公差=4;
浮动mX,我的;
公共绘图画布(上下文)
{
超级(上下文);
}
公共绘图画布(上下文、属性集属性)
{
超级(上下文,attrs);
this.paint=新油漆();
this.paint.setAntiAlias(true);
pd=新进度对话框(上下文);
this.paint.setStyle(paint.Style.STROKE);
绘制.设置行程连接(绘制.连接.圆形);
油漆。设置行程宽度(5f);
this.path=新路径();
getHolder().addCallback(此);
设置聚焦(真);
}
@凌驾
公共空白表面更改(表面文件夹持有者、整型格式、整型宽度、整型高度)
{
}
@凌驾
已创建的公共空白表面(表面持有人)
{
尝试
{
canvas=holder.lockCanvas(空);
同步(保持架)
{
onDraw(帆布);
}
}
捕获(例外e)
{
e、 printStackTrace();
}最后
{
if(canvas!=null)
{
支架。解锁画布和立柱(画布);
}
}
}
@凌驾
公共空间表面覆盖(表面覆盖物持有人)
{
}
@凌驾
公共空白onDraw(画布)
{
//super.onDraw(帆布);
这个.canvas=canvas;
mBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.c);
这个.paint.setColor(Color.GREEN);
画布.drawBitmap(mBitmap,0,0,paint);
}
@凌驾
公共布尔onTouchEvent(运动事件)
{System.out.println(“touch”);
float x=event.getX();
float y=event.getY();
开关(event.getAction())
{
case MotionEvent.ACTION\u DOWN:
//终点p1=新点();
p1.x=(int)x;
p1.y=(int)y;
最终int sourceColor=mBitmap.getPixel((int)x,(int)y);
final int targetColor=paint.getColor();
新建任务(mBitmap、p1、sourceColor、targetColor).execute();
使无效();
}
返回true;
}
公共空间清除(){
path.reset();
使无效();
}
public int getCurrentPaintColor()
{
返回paint.getColor();
}
类任务扩展异步任务{
位图bmp;
点pt;
int replacementColor,targetColor;
公共任务(位图bm、点p、点sc、点tc)
{
这个.bmp=bm;
这个,pt=p;
这个.replacementColor=tc;
这个.targetColor=sc;
pd.setMessage(“填充…”);
pd.show();
}
受保护的void onPreExecute(){
pd.show();
}
受保护的void onProgressUpdate(整型…值){
}
受保护的Void doInBackground(Void…参数)
{
System.out.println(“洪水填充”);
溢流填料f=新溢流填料();
f、 泛光填充(bmp、pt、targetColor、replacementColor);
返回null;
}
受保护的void onPostExecute(void结果){
pd.解散();
使无效();
}
}
公共级填洪
{
公共空白泛光填充(位图图像、点节点、int-targetColor、int-replacementColor)
{System.out.println(“类内洪水填充”);
int width=image.getWidth();
int height=image.getHeight();
int target=targetColor;
int replacement=replacementColor;
System.out.println(“目标颜色”+targetColor+“替换颜色”+replacementColor);
如果(目标!=更换){
Queue Queue=new LinkedList();
做{
int x=node.x;
int y=node.y;
而(x>0&&image.getPixel(x-1,y)=目标){
x--;
}
布尔spanUp=false;
布尔值=假;
而(x0
&&image.getPixel(x,y-1)=目标){
添加(新点(x,y-1));
spanUp=true;
}如果(spanUp&&y>0),则为else
&&image.getPixel(x,y-1)!=目标){
spanUp=false;
}
如果(!spanDown&&y<高度-1
&&image.getPixel(x,y+1)=目标){
添加(新点(x,y+1));
spanDown=true;
}否则,如果(向下和向下<高度-1
&&image.getPixel(x,y+1)!=目标){
Bean bean;
Draw d;
String tag;
private Paint paint;
private Path path;
Bitmap mBitmap;
ProgressDialog pd;
final Point p1 = new Point();
Canvas canvas;
private static final float TOUCH_TOLERANCE = 4;
float mX,mY;
public DrawCanvas(Context context)
{
super(context);
}
public DrawCanvas(Context context, AttributeSet attrs)
{
super(context, attrs);
this.paint = new Paint();
this.paint.setAntiAlias(true);
pd= new ProgressDialog(context);
this.paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(5f);
this.path = new Path();
getHolder().addCallback(this);
setFocusable(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
canvas = holder.lockCanvas(null);
synchronized (holder)
{
onDraw(canvas);
}
}
catch (Exception e)
{
e.printStackTrace();
} finally
{
if (canvas != null)
{
holder.unlockCanvasAndPost(canvas);
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
}
@Override
public void onDraw(Canvas canvas)
{
//super.onDraw(canvas);
this.canvas=canvas;
mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.c);
this.paint.setColor(Color.GREEN);
canvas.drawBitmap(mBitmap, 0, 0,paint);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{ System.out.println("touch");
float x = event.getX();
float y = event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
//final Point p1 = new Point();
p1.x=(int) x;
p1.y=(int) y;
final int sourceColor= mBitmap.getPixel((int)x,(int) y);
final int targetColor = paint.getColor();
new TheTask(mBitmap, p1, sourceColor, targetColor).execute();
invalidate();
}
return true;
}
public void clear() {
path.reset();
invalidate();
}
public int getCurrentPaintColor()
{
return paint.getColor();
}
class TheTask extends AsyncTask<Void, Integer, Void> {
Bitmap bmp;
Point pt;
int replacementColor,targetColor;
public TheTask(Bitmap bm,Point p, int sc, int tc)
{
this.bmp=bm;
this.pt=p;
this.replacementColor=tc;
this.targetColor=sc;
pd.setMessage("Filling....");
pd.show();
}
protected void onPreExecute() {
pd.show();
}
protected void onProgressUpdate(Integer... values) {
}
protected Void doInBackground(Void... params)
{
System.out.println("in flood fill");
FloodFill f= new FloodFill();
f.floodFill(bmp,pt,targetColor,replacementColor);
return null;
}
protected void onPostExecute(Void result) {
pd.dismiss();
invalidate();
}
}
public class FloodFill
{
public void floodFill(Bitmap image, Point node, int targetColor,int replacementColor)
{ System.out.println("in class floodfill");
int width = image.getWidth();
int height = image.getHeight();
int target = targetColor;
int replacement = replacementColor;
System.out.println("target color"+targetColor+"replacement"+replacementColor);
if (target != replacement) {
Queue<Point> queue = new LinkedList<Point>();
do {
int x = node.x;
int y = node.y;
while (x > 0 && image.getPixel(x - 1, y) == target) {
x--;
}
boolean spanUp = false;
boolean spanDown = false;
while (x < width && image.getPixel(x, y) == target) {
image.setPixel(x, y, replacement);
if (!spanUp && y > 0
&& image.getPixel(x, y - 1) == target) {
queue.add(new Point(x, y - 1));
spanUp = true;
} else if (spanUp && y > 0
&& image.getPixel(x, y - 1) != target) {
spanUp = false;
}
if (!spanDown && y < height - 1
&& image.getPixel(x, y + 1) == target) {
queue.add(new Point(x, y + 1));
spanDown = true;
} else if (spanDown && y < height - 1
&& image.getPixel(x, y + 1) != target) {
spanDown = false;
}
x++;
}
} while ((node = queue.poll()) != null);
}
}
}
}