从扩展View的类中获取屏幕旋转

时间:2011-03-23 18:31:47

标签: android

我有一个班级

class myView extends SurfaceView

如何从那里获得屏幕旋转?

1 个答案:

答案 0 :(得分:0)

您可以实例化OrientationEventListener。只是确保在不使用它时禁用它,因为SensorManager会耗尽电池。

public class OELActivity extends Activity{

  OrientationEventListener mOrientationEventListener;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mOrientationEventListener = (new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
      @Override
      public void onOrientationChanged(int aangle) {
          //Do your thing here. angle is in degrees
      }});
  }

  @Override
  public void onResume() {

    if (myOrientationEventListener.canDetectOrientation()){
      myOrientationEventListener.enable();
    } else {
      //handle the fact that you can't detect the orientation
    }
  }

  @Override
  protected void onPause() {
    super.onPause();
    mOrientationEventListener.disable();
  }
}