【问题标题】:Creating a spray effect on touch draw in android [closed]在android中创建触摸绘图的喷雾效果[关闭]
【发布时间】:2020-10-16 23:48:38
【问题描述】:

我正在尝试在用户触摸屏幕时创建类似喷雾器的效果。

用户可以选择颜色,所以效果应该与用户选择的颜色有关。

如何实现那种喷雾效果?

【问题讨论】:

    标签: java android


    【解决方案1】:

    您只需在画布上使用公共绘图部分...然后指定要绘制的半径。然后使用“随机”功能,只要用户按下,就在您使用半径定义的圆区域内绘制 (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
            ...
        }
    }
    

    【讨论】:

    • private void touch_start(float x, float y) { mDrawingManager.mDrawingUtilities.mPath.reset(); mDrawingManager.mDrawingUtilities.mPath.moveTo(x, y); mX = x; mY = y;
    • 半径=mDrawingManager.mDrawingUtilities.mPaint.getStrokeWidth(); usertouch 上的 X,Y 触摸点。那么如何计算使用 X,Y 作为参考点绘制点的区域??
    • 我会使用lain_b的方法,你不必担心形状的周长或面积,它会处理的
    • 我面临的问题是使用 x 和 y 作为参考点来获取红色区域。我知道半径和周长。
    • 对。使用 lain_b 的方法,面积和周长不再重要……它为你做腿部工作。您所要做的就是将您正在使用的半径变量插入到他的公式中。我再次更新了我的原始帖子,希望能添加更多帮助
    【解决方案2】:

    嗯,IMO 逻辑上我会:

    • 有一个喷雾罐大小/面积/半径可供用户选择,当他们触摸屏幕时获取触摸坐标。

    • 然后以接触点为圆心计算喷雾罐半径。

    • 开始在喷雾罐半径内绘制/着色一定数量的随机像素,(用户在同一区域/同一半径内握住的时间越长,将斑点完全填满的机会就越大,就像真正的喷雾罐)。

    【讨论】:

      【解决方案3】:

      这是对上面答案(DavidKroukamp,RyanInBinary)的补充,因为我没有足够的回购,所以我无法发表评论。我会使用高斯分布来进行像素分布。它比听起来容易得多:

      int x = touchedX + Random.nextGaussian()*radius;
      int y = touchedY + Random.nextGaussian()*radius;
      

      其中TouchedX / Y 是触摸事件的位置(x ,y 是要绘制的像素的坐标)。我只是认为它会提供更自然的分布。 (Random -> java.util.Random)

      【讨论】:

        【解决方案4】:

        如果性能是一个问题,您可以在第一次延迟缓存生成的随机值,然后始终重复使用它们。这具有额外的好处,即能够撤消/重做相同的伪随机模式,这可能对您的应用很有用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-10-13
          • 2011-01-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-23
          • 1970-01-01
          相关资源
          最近更新 更多