1

我正在编写类似应用程序的刮刮卡,为此我使用了 SurfaceView。我用某种颜色填充它,并使用 PorterDuff.Mode.CLEAR PorterDuffXfermode 在其上绘制一些路径。我必须确定用户何时完全划伤它(SurfaceView 的画布是完全透明的)。谁能给我一些建议,如何识别它?

我尝试保存路径的坐标,但由于绘图笔画宽度,我无法很好地计算覆盖区域。

我试图从 SurfaceView 的 getDrawingCache 方法获取位图并迭代其像素并使用 getPixel 方法。它不起作用,我认为这不是检查画布的有效方法。

4

1 回答 1

0

假设画布不会很大或不会缩放到任意大小,我认为循环像素是有效的。

给定一个大的或任意大小的画布,我会创建一个画布的数组表示,并在你走的时候标记像素,记录用户至少点击了多少次。然后根据阈值测试该数字,该阈值确定必须刮掉多少票证才能将其视为“刮掉”。传入的伪代码

const int count = size_x * size_y; // pixel count
const int threshhold = 0.8 * count // user must hit 80% of the pixels to uncover
const int finger_radius = 2; // the radias of our finger in pixels
int scratched_pixels = 0;
bit [size_x][size_y] pixels_hit; // array of pixels all initialized to 0

void OnMouseDown(int pos_x, int pos_y)
{
    // calculates the mouse position in the canvas
    int canvas_pos_x, canvas_pos_y = MousePosToCanvasPos(pos_x, pos_y);
    for(int x = canvas_pos_x - finger_rad; x < canvas_pos_x + brush_rad; ++x)
    {
        for(int y = canvas_pos_y - finger_rad; y < canvas_pos_y + brush_rad; ++y)
        {
            int dist_x = x - canvas_pos_x;
            int dist_y = y - canvas_pos_y;
            if((dist_x * dist_x + dist_y * dist_y) <= brush_rad * brush_rad
                && pixels_hit[x][y] == 0)
            {
                ++scratched_pixels;
                pixels_hit[x][y] = 1;
            }
        }
    }
}

bool IsScratched()
{
    if(scratched_pixels > threshhold)
        return true;
    else
        return false;
}
于 2013-06-04T19:04:02.630 回答