我的应用程序要求我在用户触摸屏幕时创建类似喷雾的效果.用户可以选择颜色.我需要用用户选择的颜色来创建类似喷雾的效果.我不确定它是否可能.如果可能,请建议我链接或指南.
Rya*_*ary 12
您只需使用画布上的常用绘图部分...然后指定要绘制的半径.然后使用"随机"功能,只要用户按下,就可以使用半径在您定义的圆形区域内绘制(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.
}
}
Run Code Online (Sandbox Code Playgroud)
[编辑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
...
}
}
Run Code Online (Sandbox Code Playgroud)
嗯IMO逻辑上我会:
有一个喷雾罐尺寸/面积/半径,用户可以选择,当他们触摸屏幕获得触摸的坐标.
然后使用触摸点作为圆心来计算喷雾罐半径.
开始绘制/彩色一定量的随机像素的喷雾罐半径内,(越长越用户保持在相同的区域/相同半径内,填充斑点完全像一个真正的喷雾罐的机会就越大).