Creating a spray effect on touch draw in android
You'd just use the common draw part on the canvas... then specify a radius to be drawn to. Then using the 'random' function, draw (x) number of dots inside the area of the circle you defined using the radius for as long as the user is pressing down. If you need more exact help please let me know.
[Edit] This is going to be very pseudo-code. But you should very easily be able to make your code work from this.
// 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.
}
}
[Edit 2] If you're going to use this, I would highly consider incorporating Iain_b's way of doing this. Please take his post into consideration.
[Edit 3] Here's an image... maybe this will help you to understand...
[Edit 4]
Here's my code updated with lain_b's added part to help simplify it.
// 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
...
}
}
Hmm IMO logically I would:
have a spray can size/area/radius the user can choose from, when they touch the screen get the co-ordinates of the touch.
Then calculate the spray cans radius using the touched-point as the circles center.
Begin to draw/colour a certain amount of random pixels within the spray cans radius, (the longer the user holds over the same area/within the same radius, the greater the chance of filling the spot completely like a real spray can).
This is an addition to the answers (DavidKroukamp , RyanInBinary)above I can't comment as I don't have enough repo. I would use a gaussian distribution for pixel distribution. Its much easier than it sounds :
int x = touchedX + Random.nextGaussian()*radius;
int y = touchedY + Random.nextGaussian()*radius;
Where TouchedX / Y
is the location of the touch event ( and x ,y are the coordinates of a pixel to draw ). I just think it would give a more natural distribution. (Random
-> java.util.Random
)