在android操作系統中創建一個噴漆效果

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


My application requires me to create spray like effect when user touches the screen. The user has the option of choosing a color. I need to create a spray like effect with the color user chooses. I am not sure if its possible or not. If it possible please suggest me a link or a guide to go through.

我的應用程序要求我在用戶觸摸屏幕時創建噴霧效果。用戶可以選擇一種顏色。我需要用用戶選擇的顏色創建一個噴霧效果。我不確定它是否可能。如果可能的話,請給我推薦一個鏈接或指南。

4 个解决方案

#1


11  

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.

你只需要在畫布上使用普通的繪制部分……然后指定要繪制的半徑。然后使用“隨機”函數,在你用半徑定義的圓的范圍內畫出(x)個點,只要用戶按下。如果你需要更確切的幫助,請告訴我。

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

[編輯2]如果您要使用這個,我將考慮采用Iain_b的方法。請考慮他的職位。

[Edit 3] Here's an image... maybe this will help you to understand...

[編輯3]這是一張圖片……也許這能幫助你理解……

enter image description here

[Edit 4]

[4]編輯

Here's my code updated with lain_b's added part to help simplify it.

下面是我用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
        ...
    }
}

#2


5  

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

    開始在噴霧罐半徑范圍內繪制/着色一定數量的隨機像素(用戶在相同區域/相同半徑內停留的時間越長,就越有可能像真正的噴霧罐一樣完全填充該點)。

#3


3  

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)

其中TouchedX / Y是觸摸事件的位置(x, Y是要繪制的像素的坐標)。我只是認為它會給出一個更自然的分布。(隨機- > java.util.Random)

#4


0  

If performance is an issue, you can lazily cache the generated random values the first time then always reuse them. This has the added benefit of being able to undo/redo the same pseudo-random pattern, which might be useful in your app.

如果性能有問題,您可以第一次延遲緩存生成的隨機值,然后總是重用它們。這還有一個額外的好處,即可以撤消/重做相同的偽隨機模式,這在您的應用程序中可能是有用的。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/08/13/3e6ea8c4bc737603b90957a0176d0e26.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com