javascript - Android - 仅顺时针旋转 View 180 度

标签 javascript android

我只想根据 bool 标志在一个方向上将按钮旋转 180 度。因此,当标志处于 Activity 状态时,按钮应该旋转 180 度,否则旋转 0 度。这非常有效:

button.animate().rotation(active ? 180 : 0).setDuration(250).start();

但按钮并不总是在同一方向旋转,例如如果目标旋转为 0,他将逆时针旋转。

所以我的问题是如何实现 180 度旋转动画总是顺时针?

编辑:

所以一个重要的注意事项是代码可以被调用多次。因此,如果我使用 rotateBy(来自下面的答案),如果它之前处于 Activity 状态,它会旋转 thought。对不起,我忘了提到这一点。因此,如果标志处于 Activity 状态,则它必须是 rotation % 180 == 0 否则 rotation % 360 == 0 或 rotation == 0

最佳答案

我相信这是因为当您的结束度数大于您的起始度数时, View 将顺时针旋转,而当它反转时, View 将逆时针旋转。您可以尝试获取当前的 rotationX 值并向其添加 180。

编辑:

view.rate_button.animate()
    .rotationBy(view.rate_button.rotationX + rotationValue)
    .setDuration(250)
    .withStartAction { active = true }
    .withEndAction { active = false }
    .start()

编辑 2:

behaviorSubject.subscribe({
    if (active && view.rate_button.rotationX == 0f) {
        // Active state is true but the button is not rotated to 180
        // degrees like we need so set the rotation value to 180 so we 
        // can still increment.
        rotateButton(180f)
    }
    else if (active && view.rate_button.rotationX == 180f) {
        // Active state is true and the button is rotated to 180
        // degrees like we need set the rotation value to -180
        // basically canceling out any rotation.
        rotateButton(-180f)
    }
    else if (!active && view.rate_button.rotationX == 0f) {
        // Active state is false and the button is rotated to 0
        // degrees like we need set the rotation value to 0
        // basically canceling out any rotation.
        rotateButton(0f)
    }
    else if (!active && view.rate_button.rotationX == 180f) {
        // Active state is false but the button is not rotated 
        // to 0 degrees like we need set the rotation value to 180
        // so we can still increment.
        rotateButton(180f)
    }
    else {}
})

private fun rotateButton(rotationValue: Float) {
    button.animate()
        .rotationBy(button.rotationX + rotationValue)
        .setDuration(250)
        .start()
}

为简单起见,我仍然使用 onclick 事件来触发 behaviorSubject.onNext 事件,但任何操作/事件都可以。

关于javascript - Android - 仅顺时针旋转 View 180 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50911644/

相关文章:

javascript - 如何使用 firestore TIMESTAMP 在 JavaScript 中创建时间戳

android - Kotlin 1.4.20 - 构建速度较慢

java - 如何检查复选框是否在android应用程序中被选中或未被选中?

android - 无法将应用发布到 PlayStore - 禁止降级以前使用 M 权限的设备

javascript - 十六进制颜色 - 使其更亮的配方

javascript - 根据选择的单选按钮更改按钮链接

javascript - 在MVC中使用javascript调用 Controller 方法

javascript - 必须列在 web_accessible_resources list 键中,以便由扩展之外的页面加载。

java - 插入数据库时​​字符串转换奇怪

android - linux 中的 UID 是什么?