我正在尝试为我的应用程序强制"纵向"模式,因为我的应用程序绝对不是为"横向"模式设计的.
在阅读了一些论坛后,我在清单文件中添加了这些行:
<application
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:screenOrientation="portrait">
Run Code Online (Sandbox Code Playgroud)
但它不适用于我的设备(HTC Desire).它从"纵向"lo"横向"切换,忽略清单文件中的行.
在更多论坛阅读之后,我尝试在我的清单文件中添加它:
<application
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:configChanges="orientation"
android:screenOrientation="portrait">
Run Code Online (Sandbox Code Playgroud)
这个函数在我的activity类中:
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Run Code Online (Sandbox Code Playgroud)
但同样,没有运气.
使用TYPE_MAGNETOMETER传感器时,可获得与器件方向相关的磁场强度的X,Y,Z值.我想要得到的是将这些值转换为全局参考框架,澄清:用户接受设备,测量这些值,而不是将设备旋转任何轴周围一定程度并获得相同的值.请在下面找到类似的问题: 在全局坐标中获取磁场值 如何获得磁场矢量,与设备旋转无关? 在这个答案中描述了样本解决方案(它用于线性加速,但我认为没关系):https://stackoverflow.com/a/11614404/2152255 我使用它并且我得到3个值,X总是非常小(不要认为它是正确的),Y和Z都可以,但是当我旋转设备时它们仍然有点变化.怎么可以调整?它可以全部解决吗?我使用简单的卡尔曼滤波器来近似测量值,因为即使设备根本没有移动/旋转,我也会得到不同的值.请在下面找到我的代码:
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import com.test.statistics.filter.kalman.KalmanState;
import com.example.R;
/**
* Activity for gathering magnetic field statistics.
*/
public class MagneticFieldStatisticsGatheringActivity extends Activity implements SensorEventListener {
public static final int KALMAN_STATE_MAX_SIZE = 80;
public static final double MEASUREMENT_NOISE = 5;
/** Sensor manager. */
private SensorManager mSensorManager;
/** Magnetometer spec. */
private TextView vendor;
private TextView resolution; …Run Code Online (Sandbox Code Playgroud) 无论当前的屏幕方向(横向或纵向),我都希望获得当前的磁性方向.
我找到了这个例子,但它不是独立的方向,对吧?而这也帮不了我.我也读过http://android-developers.blogspot.de/2010/09/one-screen-turn-deserves-another.html.
这是我目前采用的方法,我不想使用(短):
mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
private SensorEventListener sensorEventListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
/* Get measured value */
float current_measured_bearing = (float) event.values[0];
/* Compensate device orientation */
switch (((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay().getRotation()) {
case Surface.ROTATION_90:
current_measured_bearing = current_measured_bearing + 90f;
break;
case Surface.ROTATION_180:
current_measured_bearing = current_measured_bearing - 180f;
break;
case Surface.ROTATION_270:
current_measured_bearing = current_measured_bearing - 90f;
break;
}
Run Code Online (Sandbox Code Playgroud)
但最后一部分肯定是错的!getRotationMatrix()在这种情况下,如何正确使用更新的方法?(方向独立)或者我只需要event.values[]根据旋转矩阵使用数组的其他值?或者我需要'重新映射坐标'?那么,是实现这一目标的正确方法是什么?
我正在为具有360°屏幕旋转和API Level 11+的设备开发.
我知道这些问题经常被问到,但我不能将他们的答案转移到我的问题上.
android ×3
bearing ×1
java ×1
landscape ×1
magnetometer ×1
orientation ×1
portrait ×1
screen ×1
sensor ×1