无法使用视图类在画布上填充颜色

时间:2013-02-22 11:20:52

标签: android android-canvas

这里我正在使用 * 查看课程 *,当我尝试在图片上应用Floodfill时,它无法正常工作

public class Drawing_View extends View 
{

    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 Drawing_View(Context context )
{
    super(context);         

}

 public DrawingView(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();       


}

在这里我们设置图像和颜色以填充特定区域

@Override
protected void onDraw(Canvas canvas) 
{
    this.canvas=canvas;
    mBitmap= BitmapFactory.decodeResource(getResources(), R.drawable.c);
    this.paint.setColor(Color.GREEN);
    canvas.drawBitmap(mBitmap, 0, 0,paint);

}

**this the touch code **



@Override
    public boolean onTouchEvent(MotionEvent event)
    {

        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;
    }
        /*It will reset the path*/
    public void clear()
    {
        path.reset();
        invalidate();
    }
    public int getCurrentPaintColor() 
    {
        return paint.getColor();
    }

* 此类用于填充颜色*         class TheTask扩展了AsyncTask         {

        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(); /*calling floodfill in asyntask */
            f.floodFill(bmp,pt,targetColor,replacementColor);

            return null;
        }

        @Override
        protected void onPostExecute(Void result)
        {     
            pd.dismiss();
            invalidate();
        }
    }


       public class FloodFill // this class is used to fill the colour...
       {
         public void floodFill(Bitmap image, Point node, int targetColor,
         int replacementColor) 
             {
         int width = image.getWidth();// width of image part
         int height = image.getHeight();// height of image part
         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);
         }
     }
 }

}

0 个答案:

没有答案