在Android中创建触摸绘制的喷雾效果

时间:2012-08-13 16:43:32

标签: java android

我的应用程序要求我在用户触摸屏幕时创建类似喷雾的效果。用户可以选择颜色。我需要用用户选择的颜色来创建类似喷雾的效果。我不确定它是否可能。如果可能,请建议我链接或指南。

4 个答案:

答案 0 :(得分:11)

您只需使用画布上的常用绘图部分...然后指定要绘制的半径。然后使用“随机”功能,只要用户按下,就可以使用半径在您定义的圆形区域内绘制(x)点数。如果您需要更准确的帮助,请告诉我们。

[编辑] 这将是非常伪代码。但是你应该可以很容易地使代码工作。

// This needs to happen in the down press on the canvas
if(currentBrush == Brush.SPRAY_CAN){
    int dotsToDrawAtATime = 20;
    double brushRadius = 1.0; // This is however large they set the brush size, could be (1), could be whatever the max size of your brush is, e.g., (50), but set it based on what they choose

    for (int i = 0; i < dotsToDrawAtATime; i++){
        // Pick a random color for single dot to draw
        // Get the circumference of the circle (2*pi*brushRadius), based on the X/Y that the user input when they pressed down. Pick a random spot inside that area, and draw a single dot. As this is a for loop, it will happen 20 different times for this one occurrence.
    }
}

[编辑2] 如果你打算使用它,我会高度考虑采用Iain_b这样做的方式。请考虑他的职位。

[编辑3] 这是一张图片......也许这会帮助你理解......

enter image description here

[编辑4]

这是我的代码更新了lain_b添加的部分,以帮助简化它。

// This needs to happen in the down press on the canvas
if(currentBrush == Brush.SPRAY_CAN){
    int dotsToDrawAtATime = 20;
    double brushRadius = 1.0; // This is however large they set the brush size, could be (1), could be whatever the max size of your brush is, e.g., (50), but set it based on what they choose

    for (int i = 0; i < dotsToDrawAtATime; i++){
        // Pick a random color for single dot to draw
        ...

        // Get the location to draw to
        int x = touchedX + Random.nextGaussian()*brushRadius;
        int y = touchedY + Random.nextGaussian()*brushRadius;

        // Draw the point, using the random color, and the X/Y value
        ...
    }
}

答案 1 :(得分:5)

嗯IMO逻辑上我会:

  • 有一个喷雾罐尺寸/面积/半径,用户可以选择,当他们触摸屏幕时获得触摸的坐标。

  • 然后使用触点作为圆心来计算喷雾罐半径。

  • 开始在喷雾罐半径范围内绘制/着色一定数量的随机像素(用户在同一区域/相同半径内保持的时间越长,完全填充斑点的机会就越大真正的喷雾剂。)

答案 2 :(得分:3)

这是对答案的补充(DavidKroukamp,RyanInBinary)以上我无法评论,因为我没有足够的回购。我会使用高斯分布进行像素分布。它比听起来容易得多:

int x = touchedX + Random.nextGaussian()*radius;
int y = touchedY + Random.nextGaussian()*radius;

其中TouchedX / Y是触摸事件的位置(而x,y是要绘制的像素的坐标)。我只是认为它会给出更自然的分布。 (Random - &gt; java.util.Random

答案 3 :(得分:0)

如果性能有问题,您可以在第一次懒惰地缓存生成的随机值,然后始终重复使用它们。这具有能够撤消/重做相同的伪随机模式的额外好处,这可能对您的应用程序有用。