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,我想知道是否有什麼我失蹤。我已更新了我的問題。 –

相關問題