问题描述
|
我在android中实现相机。我将活动保留为清单中的风景。
由于我已将方向指定为固定,因此无法通过显示获得方向。它总是作为LandScape提供。但是我想知道我的设备何时保持在纵向或垂直位置。我不需要屏幕方向。谁能建议一种检测设备方向的好方法。
谢谢大家
解决方法
我认为您将不得不聆听加速度计传感器的更新并分析它们以确定方向何时发生变化。这里有一些监听传感器的示例: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();
}
}
,要检测屏幕方向,您可以在活动中使用以下代码
@Override
public void onConfigurationChanged(Configuration newConfig)
{ super.onConfigurationChanged(newConfig);
}
谢谢
迪帕克
,尝试这个:
首先实现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,truncatedRotationVector,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知道何时切换方向),请在注释中告知我,以便我可以更新自己的代码。
,我用这个:
@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
}
}
这很粗糙,但有效。