2014-09-01 51 views
0

enter image description here如何获取空洞上的绘图元素大小?

我正在做一个“带面具的绘制”应用程序。当用户在屏幕上拖动时,它将清除部分遮罩。

我实现它通过cavans与setXfermode清除

// Specify that painting will be with fat strokes: 
drawPaint.setStyle(Paint.Style.STROKE); 
drawPaint.setStrokeWidth(canvas.getWidth()/15); 

// Specify that painting will clear the pixels instead of paining new ones: 
drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 

cv.drawPath(path, drawPaint); 

的问题是,我怎么能得到的空间清理百分比?它没有必要是准确的,只是粗略地检测出更多的时屏幕尺寸的一半以上都很干净。感谢您的帮助

回答

1

你需要做的是转换你canvas和计数在它black pixels数量。使用简单的数学算法,您可以将黑色像素的数量除以画布中的像素数量,这会给出黑色像素的百分比。

样品taken from this post

public float percentTransparent(Bitmap bm) { //pass the converted bitmap of canvas 
    final int width = bm.getWidth(); 
    final int height = bm.getHeight(); 

    int totalBlackPixels = 0; 
    for(int x = 0; x < width; x++) { 
     for(int y = 0; y < height; y++) { 
      if (bm.getPixel(x, y) == Color.BLACK) { 
       totalBlackPixels ++; 
      } 
     } 
    } 
    return ((float)totalBlackPixels)/(width * height); //returns the percentage of black pixel on screen 

} 
+0

真是令人印象深刻。这项工作很棒。最后一个问题是,是否可以进行估算,而不是1像素乘1像素?对于我的情况,它不需要准确的 – user782104 2014-09-01 10:18:14

+0

@ user782104如果它有助于感谢你,我就不会想到有一个来自后称为“蒙特卡罗方法”的文章,它是快速的,而不是逐个像素地计数 – 2014-09-01 18:53:23

相关问题