2011-05-26 103 views
1

我在android中實現了一個相機。我一直把這個活動作爲景觀中的表現。 因爲我已經給定了方向,所以我無法通過顯示來獲取方向。它總是以LandScape的形式出現。但是我想知道什麼時候我的設備是以縱向或縱向位置進行拍攝的。我不希望屏幕方向。任何人都可以提出一種檢測設備方向的好方法。在android中檢測屏幕方向

感謝所有

+0

乾淨的解決方案,無需您解析傳感器信息:http://stackoverflow.com/a/9295421/752781 – pandre 2013-04-18 11:41:30

回答

1

要檢測您可以在活動中使用下面的代碼

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

感謝 迪帕克

+0

這將給我我已經設置爲景觀的屏幕方向。所以它總是給出景觀 – WISH 2011-05-26 12:37:35

3
public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 

     // Checks the orientation of the screen 
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 

//   Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 

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

//   Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
     } 
     // Checks whether a hardware keyboard is available 
     if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) { 
//   Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show(); 
     } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) { 
//   Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show(); 
     } 
    } 
+0

這不會工作,因爲在問題中指定該活動是景觀固定的 – 2017-01-19 02:52:02

0

我用這個:

@Override 
    public void onConfigurationChanged(Configuration _newConfig){ 
     super.onConfigurationChanged(_newConfig); 
     int height = getWindowManager().getDefaultDisplay().getHeight(); 
     int width = getWindowManager().getDefaultDisplay().getWidth(); 
     if(width > height){ 
// landscape 
     }else{ 
// portrait 
     } 
    } 

這是原油,但有效。

+0

更簡潔的方式如下:http://stackoverflow.com/a/6138643/540990 – Murphy 2012-06-26 16:25:39

0

試試這個: 首先實現SensorEventListener並獲得RotationSensor

sensorManager = (SensorManager)getActivity().getSystemService(Context.SENSOR_SERVICE); 
rotationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR); 
sensorManager.registerListener(this, rotationSensor, SENSOR_INTERVAL); 
int FROM_RADS_TO_DEGS = -57; 

然後你就可以檢測設備的這樣的角度:

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

private void updateRotation(float[] vectors) { 
    float[] rotationMatrix = new float[9]; 
    SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors); 
    int worldAxisX = SensorManager.AXIS_X; 
    int worldAxisZ = SensorManager.AXIS_Z; 
    float[] adjustedRotationMatrix = new float[9]; 
    SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix); 
    float[] orientation = new float[3]; 
    SensorManager.getOrientation(adjustedRotationMatrix, orientation); 
    float pitch = orientation[1] * FROM_RADS_TO_DEGS; 
    if(pitch < -45 && pitch > -135) { 
     // if device is laid flat on a surface, we don't want to change the orientation 
     return; 
    } 
    float roll = Math.abs(orientation[2] * FROM_RADS_TO_DEGS); 
    if((roll > 45 && roll < 135)) { 
     // The device is closer to landscape orientation. Enable fullscreen 
     if(!player.isFullScreen()) { 
      if(getActivity() != null) { 
       player.setFullScreenOn(); 
      } 
     } 
    } 
    else { 
     // The device is closer to portrait orientation. Disable fullscreen 
     if(player.isFullScreen()) { 
      if(getActivity() != null) { 
       player.setFullScreenOff(); 
      } 
     } 
    } 
} 

@Override 
public void onAccuracyChanged(Sensor sensor, int accuracy) { 
    // Do nothing 
} 

這引起了原代碼的教程,但不久前,所以我不記得教程的位置。該版本根據我的要求進行了大量定製,但如果有人從原始版本中識別出來,請將其放入鏈接中。

我使用此代碼來檢測視頻播放器何時應該全屏內的ViewPage,我不想讓橫向方向上。除了一件事情,它運作良好:

它使用RotationSensor硬件,並非所有的Android設備都有RotationSensor。如果有人知道使用一些包含在所有設備上的硬件來做到這一點的方法(肯定有一種方法是因爲Android知道何時切換方向),請在評論中告訴我,以便我可以更新自己的代碼。