あなたは、開始の間の差を計算する必要がありますが、0から359度の範囲である
OrientationEventListener.onOrientationChanged()
を介してデバイスの回転度を得ることができますポイントと回転角度の変化をあなた自身が知っている。
void onOrientationChanged (int orientation) {
//orientation is an argument which represents rotation in degrees
}
また、あなたは、リスナーのenable()
とdisable()
メソッドを呼び出すことで、このリスナーを有効または無効にすることができます。ここにはGoogleのdeveoper reference docsがあります。
thisは、どのように方向リスナーを使用するかについてのリンクです。
public class SimpleOrientationActivity extends Activity {
OrientationEventListener mOrientationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mOrientationListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
Log.v(DEBUG_TAG, "Orientation changed to " + orientation);
}
};
if (mOrientationListener.canDetectOrientation() == true) {
Log.v(DEBUG_TAG, "Can detect orientation");
mOrientationListener.enable();
} else {
Log.v(DEBUG_TAG, "Cannot detect orientation");
mOrientationListener.disable();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mOrientationListener.disable();
}
}
ゲームの使用などの目的に適した他のレート値があります。既定のレートSENSOR_DELAY_NORMALは、簡単な方向変更に最適です。他の値(SENSOR_DELAY_UIやSENSOR_DELAY_GAMEなど)が役に立つかもしれません。
Thisは、同じコードを実装し、適切な説明を提供する別の便利なリンクです。
ゲームを始めるときに、ユーザーにデバイスをキャリブレーションしてから、デバイスの現在の傾きを取得して開始チルトに保存するようなことができます。その後、デバイスの傾きを取得するたびに、(currentTilt-startingTilt)を実行します。基本的には、ゲームの開始時に、傾きを取得し、その開始傾きに関連する後の入力を管理します。 –