2015-01-08 2 views
3

Android Wear 기기의 경우 기기 방향에 따라 화면을 자동으로 회전 할 수 있습니까? 예를 들어, 내 손을 내 앞에서 공중에 똑바로 들고 손을 뻗으면 손목 시계면이 옆으로 보이고 모든 텍스트를 읽기가 어려울 것입니다.웨어러블 회전 화면 (오리엔테이션 기반)

화면 방향이 중력과 일치하는 핸드 헬드 Android 기기와 같은 자동 회전 기능이 있나요?

내 앱에서이 기능을 사용하고 싶습니다.

답변

0

지금까지 나는 이에 대한 만족스러운 대답을 찾을 수 없었습니다. 매니페스트의 활동 정의에 android:screenOrientation="fullSensor"을 설정하면 이는 완전히 무시됩니다. 주제를 바꾸면 효과가없는 것처럼 보입니다.

그렇다면 필요한 경우 방향을 수동으로 변경할 수 있습니다. 개인적으로이 솔루션이 마음에 들지 않으므로 누군가가 더 나은 솔루션을 제공한다면 기부 해주십시오. (사이드 노트 :웨어 응용 프로그램이 자동 방향을 지정하지 않는 것이 당연하다고 생각하지만, 클라이언트 측에서는이를 요청했기 때문에 어떤 방식 으로든 구현해야합니다.)

이 코드는 완벽하지는 않지만 필요한 경우 시작했습니다.

@Override 
protected void onResume() { 
    super.onResume(); 
    // .. other code .. 
    startRotationListening(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    // .. other code .. 
    stopRotationListening(); 
} 

protected void startRotationListening() { 
    try { 
     SensorManager sensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE); 
     sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR), SENSOR_DELAY); 
     listeningToRotation = true; 
    } catch (Exception e) { 
     Log.e(TAG, "Rotation hardware? What hardware?", e); 
    } 
} 

protected void stopRotationListening() { 
    if (listeningToRotation) { 
     try { 
      SensorManager sensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE); 
      sensorManager.unregisterListener(this); 
      listeningToRotation = false; 
     } catch (Exception e) { 
      Log.e(TAG, "Rotation hardware? What hardware?", e); 
     } 
    } 
} 

@Override 
public void onSensorChanged(SensorEvent event) { 
    //if (event.sensor == rotationSensor) { 
     if (event.values.length > 4) { 
      float[] truncatedRotationVectors = new float[4]; 
      System.arraycopy(event.values, 0, truncatedRotationVectors, 0, 4); 
      onRotationChange(truncatedRotationVectors); 
     } 
    //} 
} 

protected void onRotationChange(float[] vectors) { 
    float[] rotationMatrix = new float[9], adjustedMatrix = new float[9], orientation = new float[3]; 
    SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors); 
    SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, adjustedMatrix); 
    SensorManager.getOrientation(adjustedMatrix, orientation); 

    float pitch = orientation[1] * RADS_TO_DEGS, roll = orientation[2] * RADS_TO_DEGS; 

    String strOrientation = null; 
    int screenOrientation = -1; 
    if (pitch <= -80 && pitch >= -100) { 
     // Too flat (face up), ignore orientation 
     strOrientation = "FLAT_UP"; 
    } else if (pitch >= 80 && pitch <= 100) { 
     // Too flat (face down), ignore orientation 
     strOrientation = "FLAT_DN"; 
    } 

    if (strOrientation == null) { 
     if (roll >= -10 && roll <= 10) { 
      screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; 
      strOrientation = "UPRIGHT"; 
     } else if (roll <= -80 && roll >= -110) { 
      screenOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; 
      strOrientation = "ONRIGHT"; 
     } else if (roll >= 170 || roll <= -170) { 
      screenOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; 
      strOrientation = "TOPSIDE"; 
     } else if (roll >= 80 && roll <= 100) { 
      screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; 
      strOrientation = " ONLEFT"; 
     } else { 
      strOrientation = " ??? "; 
     } 
    } 

    if (screenOrientation != -1) { 
     setRequestedOrientation(screenOrientation); 
    } 
    Log.d(TAG, String.format("%s Pitch: %f, roll: %f", strOrientation, pitch, roll)); 
}