2

我已经做了一些按钮使用setRotation()相应地旋转到设备方向。但是,我注意到这种变化并不顺畅,我想知道是否有一种简单的方法可以用RotateAnimation替换这种方法。主要问题是这些方向变化不会从相同的角度出现,例如,动画必须处理从0-90和270-90的旋转。我正在使用OrientationEventListener来检测角度方向。有任何想法吗?有没有办法让setRotation动画,还是用RotateAnimation替代它?

UPDATE:

OrientationEventListener orientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) { 

    @Override 
    public void onOrientationChanged(int angle) { 
     float currentAngle = downloadStatus.getRotation(); 
     if(angle > 260 && angle < 280) { 
      downloadStatus.animate().rotationBy(90 - currentAngle).setDuration(100).start(); 
     } else if(angle > 80 && angle < 100) { 
      downloadStatus.animate().rotationBy(-90 - currentAngle).setDuration(100).start(); 
     } else if(angle > 350 || angle < 10){ 
      downloadStatus.animate().rotationBy(-currentAngle).setDuration(100).start(); 
     } else if(angle > 170 && angle < 190){ 
      downloadStatus.animate().rotationBy(180 - currentAngle).setDuration(100).start(); 
     } 
    } 
}; 
orientationEventListener.enable(); 

我明年受审什么是以下两个替换反向纵向角度,如果:

while (MyButtonCurrentAngle==90) { 
    if (ButtonsAngle > 170 && ButtonsAngle < 190) { 
     MyButton.animate().rotationBy(90 - MyButtonCurrentAngle).setDuration(100).start(); 
    } 
} 
while (MyButtonCurrentAngle==270) { 
    if (ButtonsAngle > 170 && ButtonsAngle < 190) { 
     MyButton.animate().rotationBy(-90 - MyButtonCurrentAngle).setDuration(100).start(); 
    } 
} 
+0

'ViewPropertyAnimator'? – tachyonflux

回答

2

尝试更新我以前的代码如下:

OrientationEventListener orientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) { 

     @Override 
     public void onOrientationChanged(int angle) { 
      float currentAngle = downloadStatus.getRotation(); 
      if(angle > 260 && angle < 280) { 
       downloadStatus.animate().rotationBy(90 - currentAngle).setDuration(100).start(); 
      } else if(angle > 80 && angle < 100) { 
       downloadStatus.animate().rotationBy(-90 - currentAngle).setDuration(100).start(); 
      } else if(angle > 350 || angle < 10){ 
       downloadStatus.animate().rotationBy(-currentAngle).setDuration(100).start(); 
      } 
     } 
    }; 
    orientationEventListener.enable(); 
+1

你是最棒的!再次感谢你的帮助。 :) –

+0

嗨,我有一个小小的怀疑。我添加了反向人像和动画,但它旋转了很多。我第一次尝试180,因为没有什么-0,我想知道是否有什么我失踪。我已更新了我的问题。 –

相关问题