Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a spray effect on touch draw in android [closed]

Tags:

java

android

I am trying to create a sprayer like effect when user touches the screen.

The user has the option of choosing a color, so the effect should be with the user's color choice.

How to implement that spray effect?

like image 359
Raghunandan Avatar asked Aug 13 '12 16:08

Raghunandan


1 Answers

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...

enter image description here

[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
        ...
    }
}
like image 172
RyanInBinary Avatar answered Sep 30 '22 13:09

RyanInBinary