2014-12-23 4 views
8

잠긴 세로 방향 모드에서 카메라 화면 방향을 찾고 싶습니다. 조각 클래스에서 카메라를 사용하고 있으며 이미 세로 방향으로 화면 방향을 설정했지만 카메라가 켜지면 문제가 발생합니다. 가로 방향이 바뀌고 카메라가 세로 모드 인 경우에만 캡처 단추를 표시해야합니다. 세로 모드에서 방향 변경을 도와 줄 사람이 있습니까?안드로이드의 세로 잠긴 화면에서 내 화면 방향을 감지하는 방법은 무엇입니까?

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 


    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     //do your stuff with the button 
    } 
} 

당신이 당신의 활동이 다음 경우

android:configChanges="orientation|keyboardHidden|screenSize"

를 사용 오리엔테이션 재 작성하지 않도록하려면 다음

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

sensorManager.registerListener(new SensorEventListener() { 

    int orientation=-1;; 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     if (event.values[1] < 6.5 && event.values[1] > -6.5) { 
      if (orientation!=1) { 
       Log.d("Sensor", "Landscape"); 
      } 
      orientation = 1; 
     } else { 
      if (orientation!=0) { 
       Log.d("Sensor", "Portrait"); 
      } 
      orientation = 0; 
     } 
    } 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
    } 
}, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME); 

if (orientation == 0) { 
    // capture button visisble 
} else { 
    // invisible 
} 
+0

http://stackoverflow.com/a/5726776/2247689 –

+0

이에 봐 전화 a/41104983/2267723이 솔루션은 SensorManager를 사용합니다. –

답변

29

OrientationEventListener를 사용할 수 있습니다. 이 클래스를 사용자 정의하는 클래스입니다. http://stackoverflow.com/ : 당신이 방향을 감지 할 위치

public abstract class SimpleOrientationListener extends OrientationEventListener { 

     public static final int CONFIGURATION_ORIENTATION_UNDEFINED = Configuration.ORIENTATION_UNDEFINED; 
     private volatile int defaultScreenOrientation = CONFIGURATION_ORIENTATION_UNDEFINED; 
     public int prevOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; 
     private Context ctx; 
     private ReentrantLock lock = new ReentrantLock(true); 

     public SimpleOrientationListener(Context context) { 
      super(context); 
      ctx = context; 
     } 

     public SimpleOrientationListener(Context context, int rate) { 
      super(context, rate); 
      ctx = context; 
     } 

     @Override 
     public void onOrientationChanged(final int orientation) { 
      int currentOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; 
      if (orientation >= 330 || orientation < 30) { 
       currentOrientation = Surface.ROTATION_0; 
      } else if (orientation >= 60 && orientation < 120) { 
       currentOrientation = Surface.ROTATION_90; 
      } else if (orientation >= 150 && orientation < 210) { 
       currentOrientation = Surface.ROTATION_180; 
      } else if (orientation >= 240 && orientation < 300) { 
       currentOrientation = Surface.ROTATION_270; 
      } 

      if (prevOrientation != currentOrientation && orientation != OrientationEventListener.ORIENTATION_UNKNOWN) { 
       prevOrientation = currentOrientation; 
       if (currentOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) 
        reportOrientationChanged(currentOrientation); 
      } 

     } 

     private void reportOrientationChanged(final int currentOrientation) { 

      int defaultOrientation = getDeviceDefaultOrientation(); 
      int orthogonalOrientation = defaultOrientation == Configuration.ORIENTATION_LANDSCAPE ? Configuration.ORIENTATION_PORTRAIT 
        : Configuration.ORIENTATION_LANDSCAPE; 

      int toReportOrientation; 

      if (currentOrientation == Surface.ROTATION_0 || currentOrientation == Surface.ROTATION_180) 
       toReportOrientation = defaultOrientation; 
      else 
       toReportOrientation = orthogonalOrientation; 

      onSimpleOrientationChanged(toReportOrientation); 
     } 

     /** 
     * Must determine what is default device orientation (some tablets can have default landscape). Must be initialized when device orientation is defined. 
     * 
     * @return value of {@link Configuration#ORIENTATION_LANDSCAPE} or {@link Configuration#ORIENTATION_PORTRAIT} 
     */ 
     private int getDeviceDefaultOrientation() { 
      if (defaultScreenOrientation == CONFIGURATION_ORIENTATION_UNDEFINED) { 
       lock.lock(); 
       defaultScreenOrientation = initDeviceDefaultOrientation(ctx); 
       lock.unlock(); 
      } 
      return defaultScreenOrientation; 
     } 

     /** 
     * Provides device default orientation 
     * 
     * @return value of {@link Configuration#ORIENTATION_LANDSCAPE} or {@link Configuration#ORIENTATION_PORTRAIT} 
     */ 
     private int initDeviceDefaultOrientation(Context context) { 

      WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
      Configuration config = context.getResources().getConfiguration(); 
      int rotation = windowManager.getDefaultDisplay().getRotation(); 

      boolean isLand = config.orientation == Configuration.ORIENTATION_LANDSCAPE; 
      boolean isDefaultAxis = rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180; 

      int result = CONFIGURATION_ORIENTATION_UNDEFINED; 
      if ((isDefaultAxis && isLand) || (!isDefaultAxis && !isLand)) { 
       result = Configuration.ORIENTATION_LANDSCAPE; 
      } else { 
       result = Configuration.ORIENTATION_PORTRAIT; 
      } 
      return result; 
     } 

     /** 
     * Fires when orientation changes from landscape to portrait and vice versa. 
     * 
     * @param orientation value of {@link Configuration#ORIENTATION_LANDSCAPE} or {@link Configuration#ORIENTATION_PORTRAIT} 
     */ 
     public abstract void onSimpleOrientationChanged(int orientation); 

    } 

그럼 그냥

SimpleOrientationListener mOrientationListener = new SimpleOrientationListener(
       context) { 

      @Override 
      public void onSimpleOrientationChanged(int orientation) { 
       if(orientation == Configuration.ORIENTATION_LANDSCAPE){ 

       }else if(orientation == Configuration.ORIENTATION_PORTRAIT){ 

       } 
      } 
     }; 
     mOrientationListener.enable(); 
+0

onSimpleOrientatioChanged 메서드 내에서 무엇을 선언해야합니까? – Vicky

+0

onCreate()가 수행합니다. 이 방법으로 코드를 작성해야합니다. 오리엔테이션 변경 –

+0

세로에서 가로 방향으로 화면이 변경되면 화면이 세로 방향 인 경우에만 보이지 않아야합니다. – Vicky

0

당신 거 사용해야합니다 : 다음은 내 코드입니다 당신의 활동이 초상화를 유지하도록 강요 당하면서 사용해야합니다.

을 방금 시도하려는 활동의 매니페스트 파일에 넣으십시오. android:screenOrientation="portrait"

희망이 있습니다!

+0

nullpointer 예외가 발생하는 경우 @if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) – Vicky

+0

그 풍경이 if 문을 제거 할 때 아무 것도하지 않으면 ... –

+0

글쎄, 화면을 회전시킬 때 오른쪽 하나의 이벤트가 발생하지 않습니다. 가로 방향으로 보면 세로로 감지됩니다. 내 코드 위의 – Vicky

관련 문제