我在android中实现了一个摄像头。我把活动作为清单中的景观。 由于我已将方向定为固定,因此我无法通过显示获得方向。它总是作为LandScape。但我想知道我的设备何时处于纵向或垂直位置。我不想要屏幕方向。任何人都可以建议一种检测设备方向的好方法。
全部谢谢
答案 0 :(得分:3)
我认为你必须听取加速计传感器更新并解析它们以确定方向何时发生变化。这里有一些听传感器的示例:http://www.anddev.org/accessing_the_accelerometer-t499.html和http://mobilestrategist.blogspot.com/2010/01/android-accelerometer-and-orientation.html
答案 1 :(得分: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();
}
}
答案 2 :(得分:1)
要检测屏幕方向,您可以在活动中使用以下代码
@Override
public void onConfigurationChanged(Configuration newConfig)
{ super.onConfigurationChanged(newConfig);
}
由于 迪帕克
答案 3 :(得分:1)
试试这个: 首先实现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知道何时切换方向),请在评论中告诉我,以便我可以更新自己的代码。
答案 4 :(得分: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
}
}
原始但有效。