【发布时间】:2020-10-16 23:48:38
【问题描述】:
我正在尝试在用户触摸屏幕时创建类似喷雾器的效果。
用户可以选择颜色,所以效果应该与用户选择的颜色有关。
如何实现那种喷雾效果?
【问题讨论】:
我正在尝试在用户触摸屏幕时创建类似喷雾器的效果。
用户可以选择颜色,所以效果应该与用户选择的颜色有关。
如何实现那种喷雾效果?
【问题讨论】:
您只需在画布上使用公共绘图部分...然后指定要绘制的半径。然后使用“随机”功能,只要用户按下,就在您使用半径定义的圆区域内绘制 (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] 这是一张图片...也许这会帮助您理解...
[编辑 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
...
}
}
【讨论】:
嗯,IMO 逻辑上我会:
有一个喷雾罐大小/面积/半径可供用户选择,当他们触摸屏幕时获取触摸坐标。
然后以接触点为圆心计算喷雾罐半径。
开始在喷雾罐半径内绘制/着色一定数量的随机像素,(用户在同一区域/同一半径内握住的时间越长,将斑点完全填满的机会就越大,就像真正的喷雾罐)。
【讨论】:
这是对上面答案(DavidKroukamp,RyanInBinary)的补充,因为我没有足够的回购,所以我无法发表评论。我会使用高斯分布来进行像素分布。它比听起来容易得多:
int x = touchedX + Random.nextGaussian()*radius;
int y = touchedY + Random.nextGaussian()*radius;
其中TouchedX / Y 是触摸事件的位置(x ,y 是要绘制的像素的坐标)。我只是认为它会提供更自然的分布。 (Random -> java.util.Random)
【讨论】:
如果性能是一个问题,您可以在第一次延迟缓存生成的随机值,然后始终重复使用它们。这具有额外的好处,即能够撤消/重做相同的伪随机模式,这可能对您的应用很有用。
【讨论】: