在android中检测屏幕方向

在android中检测屏幕方向

问题描述:

我在android中实现了一个相机。我一直把这个活动作为景观中的表现。 因为我已经给定了方向,所以我无法通过显示来获取方向。它总是以LandScape的形式出现。但是我想知道什么时候我的设备是以纵向或纵向位置进行拍摄的。我不希望屏幕方向。任何人都可以提出一种检测设备方向的好方法。在android中检测屏幕方向

感谢所有

+0

干净的解决方案,无需您解析传感器信息:http://*.com/a/9295421/752781 – pandre 2013-04-18 11:41:30

要检测您可以在活动中使用下面的代码

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

感谢 迪帕克

+0

这将给我我已经设置为景观的屏幕方向。所以它总是给出景观 – WISH 2011-05-26 12:37:35

屏幕方向,我认为你将不得不听的加速度计传感器的更新和解析它们以确定方向何时改变。有听这里的传感器的一些例子:http://www.anddev.org/accessing_the_accelerometer-t499.html这里http://mobilestrategist.blogspot.com/2010/01/android-accelerometer-and-orientation.html

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

我用这个:

@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://*.com/a/6138643/540990 – Murphy 2012-06-26 16:25:39

试试这个: 首先实现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知道何时切换方向),请在评论中告诉我,以便我可以更新自己的代码。