如何获得一个cavans抽奖单元尺寸? [英] How to get the draw element size on a cavans?

查看:258
本文介绍了如何获得一个cavans抽奖单元尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是工作在一个绘制面具的应用程序。当屏幕上的用户拖动,它清除掩模的一部分。

I was working on a "draw with mask" app. When the user drag on the screen , it cleans part of the mask.

我实现它通过cavans和setXfermode清除

I implemented it through cavans and with setXfermode Clear

// 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);

问题是,我怎么能得到的空间清理百分比?它确实是准确的没有必要,只是粗略地当超过屏幕大小的一半是干净的检测。感谢您​​的帮助

推荐答案

您需要做的是你的画布转换中的位图和计数黑色像素在它的数量。使用简单的数学,你可以把黑的像素数在画布像素的数量,这将给你的黑色像素的百分比。

What you need to do is to convert your canvas in to bitmap and count the number of black pixels in it. Using simple math you can divide the number of black pixels to the number of pixels in the canvas which will give you the percentage of black pixels.

样<一个href=\"http://stackoverflow.com/questions/14357246/how-to-get-total-area-covered-while-drawing-path-on-canvas-android\">taken从这个帖子:

 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

}

这篇关于如何获得一个cavans抽奖单元尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆