定位纸屑

Positioning confetti

我正在按照下面的教程添加五彩纸屑。

final KonfettiView konfettiView = (KonfettiView)findViewById(viewKonfetti);
            konfettiView.build()
                    .addColors(Color.RED, Color.GREEN)                
                    .setSpeed(1f, 5f)
                    .setFadeOutEnabled(true)
                    .setTimeToLive(2000L)
                    .addShapes(Shape.RECT, Shape.CIRCLE)            
                    .setPosition(0f, -359f, -359f, 0f)
                    .stream(200, 5000L);

我希望五彩纸屑出现在右上角而不是左上角。我应该在 setPosition 方法中输入什么值?这种方法是如何工作的?

我正在关注这个 https://github.com/DanielMartinus/Konfetti 但是关于方法及其参数的信息不多。

这里是编辑代码,它会产生你想要的输出:

fun setPosition(x: Float, y: Float): ParticleSystem {
    location.setX(x)
    location.setY(y)
    return this
}

/**
 * Set position range to emit particles from
 * A random position on the x-axis between [minX] and [maxX] and y-axis between [minY] and [maxY]
 * will be picked for each confetti.
 * @param [maxX] leave this null to only emit from [minX]
 * @param [maxY] leave this null to only emit from [minY]
 */
fun setPosition(minX: Float, maxX: Float? = null, minY: Float, maxY: Float? = null): ParticleSystem {
    location.betweenX(maxX, maxX)
    location.betweenY(minY, maxY)
    return this
}

参考this answer assemble/adjust方向和相应坐标。

还有;如果你只想放置坐标;正如您所发现的,有一个 class MotionEventgetX()getY() 方法,其中 return 相对于视图的坐标。

希望对您有所帮助....