简体   繁体   English

Android - 仅顺时针旋转视图180度

[英]Android - rotate view 180 degress only clockwise

I want to rotate a button only in one direction 180 degrees depending on a boolean flag. 我想根据布尔标志仅在180度的一个方向上旋转按钮。 So when the flag is active the button should have a rotation of 180 and otherwise 0. This works perfectly: 因此,当标志处于活动状态时,按钮的旋转应为180,否则为0.这非常有效:

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

But the button is not rotating always in the same direction cause for example if the target rotation is 0 he will rotate counterclockwise. 但是按钮不是始终在相同方向上旋转,例如,如果目标旋转为0,则它​​将逆时针旋转。

So my question is how can I achieve the 180 rotation animation always be clockwise? 所以我的问题是如何才能实现180度旋转动画始终顺时针?

Edit: 编辑:

So one important note is that the code could be called multiple times. 所以一个重要的注意事项是代码可以被多次调用。 So if I use rotateBy (from the answers below) it would rotate althought if it was active before. 因此,如果我使用rotateBy(来自下面的答案),如果它之前是活动的,它会旋转。 Sorry I forgot to mention that. 对不起,我忘记提及了。 So if the flag is active it must be rotation % 180 == 0 otherwise rotation % 360 == 0 or rotation == 0 因此,如果该标志处于活动状态,则它必须是rotation % 180 == 0否则rotation % 360 == 0 or rotation == 0

I believe it's because the view will turn clockwise when your end degree is larger than your starting degree and counterclockwise when it's reversed. 我相信这是因为当你的结束度大于你的起始度时,视图将顺时针转动,而当它的反转时,逆时针转动。 You could try getting your current rotationX value and adding 180 to it. 您可以尝试获取当前的rotationX值并向其添加180。

EDIT: 编辑:

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

EDIT 2: 编辑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()
}

For simplicity I'm still using an onclick event to fire off the behaviorSubject.onNext event but any action/event would work. 为简单起见,我仍然使用onclick事件来触发behaviorSubject.onNext事件,但任何动作/事件都可以。

而不是旋转(float)使用rotationBy(float)方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM