这是 Java类
import java.util.linkedList;import java.util.Queue;import androID.app.Activity;import androID.app.ProgressDialog;import androID.content.Context;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.graphics.BitmapFactory.Options;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Paint;import androID.graphics.Path;import androID.graphics.Point;import androID.os.AsyncTask;import androID.os.Bundle;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.ImageVIEw;import androID.Widget.relativeLayout;public class Main extends Activity {private relativeLayout dashBoard;private MyVIEw myVIEw;public ImageVIEw image;button b_red,b_blue,b_green,b_orange,b_clear;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); myVIEw = new MyVIEw(this); setContentVIEw(R.layout.activity_main); findVIEwByID(R.ID.dashBoard); b_red = (button) findVIEwByID(R.ID.b_red); b_blue = (button) findVIEwByID(R.ID.b_blue); b_green = (button) findVIEwByID(R.ID.b_green); b_orange = (button) findVIEwByID(R.ID.b_orange); b_red.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { myVIEw.changePaintcolor(0xFFFF0000); } }); b_blue.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { myVIEw.changePaintcolor(0xFF0000FF); } }); b_green.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { myVIEw.changePaintcolor(0xFF00FF00); } }); b_orange.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { myVIEw.changePaintcolor(0xFFFF9900); } }); dashBoard = (relativeLayout) findVIEwByID(R.ID.dashBoard); dashBoard.addVIEw(myVIEw);}public class MyVIEw extends VIEw { private Paint paint; private Path path; public Bitmap mBitmap; public ProgressDialog pd; final Point p1 = new Point(); public Canvas canvas; //Bitmap mutableBitmap ; public MyVIEw(Context context) { super(context); 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); mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.forme).copy(Bitmap.Config.ARGB_8888,true); this.path = new Path(); } @OverrIDe protected voID onDraw(Canvas canvas) { this.canvas = canvas; this.paint.setcolor(color.RED); canvas.drawBitmap(mBitmap,paint); } @OverrIDe public boolean ontouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: 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(); } public voID changePaintcolor(int color){ this.paint.setcolor(color); } 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(); } @OverrIDe protected voID onPreExecute() { pd.show(); } @OverrIDe protected voID onProgressUpdate(Integer... values) { } @OverrIDe protected VoID doInBackground(VoID... params) { FloodFill f = new FloodFill(); f.floodFill(bmp,pt,targetcolor,replacementcolor); return null; } @OverrIDe protected voID onPostExecute(VoID result) { pd.dismiss(); invalIDate(); } }}// flood fillpublic class FloodFill { public voID floodFill(Bitmap image,Point node,int targetcolor,int replacementcolor) { int wIDth = image.getWIDth(); int height = image.getHeight(); int target = targetcolor; int 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); } }}}
正如您所看到的,我使用填充泛洪算法来着色图像中的空白区域.
图片如下:
Image to be used to fill the color
现在的问题是如何用下面的图案(或任何其他图案)而不是颜色填充白色空间区域.
Pattern image
请任何帮助,因为我是AndroID的新手.谢谢
解决方法 经过一些小的解决方法后,我能够使用Flood Fill Algorithm填充封闭区域中的模式.以下是我的所作所为.1.介绍一个带有模式的新位图,如下所示.
bitmapPattern = BitmapFactory.decodeResource(getResources(),R.drawable.pattern1).copy(Bitmap.Config.ARGB_8888,true);
>并将此bitmapPattern传递给Flood Fill算法,如下所示.
FloodFillPattern f = new FloodFillPattern(); f.floodFillPattern(bmp,replacementcolor,pattern);
>请注意,您必须将此位图缩放为等于要填充此模式的图像大小.
>然后简单地用泛光填充算法中的图案位图的像素替换原始图像的像素.如下:
image.setPixel(x,pattern.getPixel(x,y));
pattern是您传递的patternBitmap.可以帮助别人:)
总结以上是内存溢出为你收集整理的使用洪水填充algroithim android填充模式而不是颜色全部内容,希望文章能够帮你解决使用洪水填充algroithim android填充模式而不是颜色所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)