【问题标题】:Identify when the surfaceview is fully transparent识别surfaceview何时完全透明
【发布时间】:2013-06-08 09:20:37
【问题描述】:

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

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

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

【问题讨论】:

    标签: android surfaceview transparent


    【解决方案1】:

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

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

    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;
    }
    

    【讨论】:

    • 感谢您的评论。我可以跟踪手指的位置,但最后没有给出正确的结果,因为我在画布上绘制了一些笔触宽度的路径。
    • 在这种情况下,将手指表示为一个圆圈,并找到该圆圈相交的所有像素。然后对它们进行全部测试,而不是单场演出。如果需要,我可以提供一些伪代码。
    • 我已经完成了,解决方案与您在上一条评论中所说的类似。如果您将其写为答案,我会接受。
    猜你喜欢
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多