当屏幕方向没有时,Display.getRotation()值会发生变化

时间:2014-12-19 05:29:28

标签: android accelerometer android-orientation

我正在测试Android的加速度计,并且遇到显示器的方向值交替出现的问题,即使我的手机处于静态横向方向。

这是活动的代码:

public class AccelerometerTest extends Activity implements SensorEventListener
{
    private final int SENSOR_SAMPLE_RATE = 1000;
    long lastSense;

    int screenRotation;
    static final int ACCELEROMETER_AXIS_SWAP[][] = { 
        {1, -1, 0, 1},      //Rotation 0.
        {-1, -1, 1, 0},     //Rotation 90.
        {-1, 1, 0, 1},      //Rotation 180.
        {1, 1, 1, 0}};      //Rotation 270.

    StringBuilder sbuilder = new StringBuilder();
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        textView = new TextView(this);
        setContentView(textView);
        lastSense = 0;

        SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0)
        {
            textView.setText("No accelerometer installed.");
        }//if
        else
        {
            Sensor accelerometer = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
            if (!sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME))
            {
                textView.setText("Couldn't register sensor listener.");
            }//if
        }//else
    }//onCreate

    @Override
    protected void onResume()
    {
        super.onResume();
        WindowManager wmgr = (WindowManager) getSystemService(Activity.WINDOW_SERVICE);
        screenRotation = wmgr.getDefaultDisplay().getRotation();
    }//onResume

    @Override
    public void onSensorChanged(SensorEvent event) 
    {
        if (lastSense == 0)
        {
            lastSense = Calendar.getInstance().getTimeInMillis();
        }//if

        if (!((Calendar.getInstance().getTimeInMillis() - lastSense) > SENSOR_SAMPLE_RATE))
        {
            return;
        }//if

        final int[] acc_swap = ACCELEROMETER_AXIS_SWAP[screenRotation];
        float screenX = (float)acc_swap[0] * event.values[acc_swap[2]]; 
        float screenY = (float)acc_swap[1] * event.values[acc_swap[3]]; 
        float screenZ = event.values[2];

        sbuilder.setLength(0);
        sbuilder.append("Adjusting : Not Adjusting\n");
        sbuilder.append(screenX +" :X: " + event.values[0] + "\n");
        sbuilder.append(screenY +" :Y: " + event.values[1] + "\n");
        sbuilder.append(screenZ +" :Z: " + event.values[2] + "\n");
        textView.setText(sbuilder.toString());

        lastSense = Calendar.getInstance().getTimeInMillis();
    }//onSensorChanged

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

我在调试时遇到了这个问题。我在这里设置了第一个断点:

screenRotation = wmgr.getDefaultDisplay().getRotation();

这是设置变量screenRotation的唯一位置。活动开始时,它的初始值为0(ROTATION_0)。将屏幕顺时针旋转90度至横向时,该值设置为3(ROTATION_270)。虽然我的手机的横向方向保持不变,但onResume()永远不会被调用,因此screenRotation的值 永远不会改变...对吗?

但是,当我在这里设置第二个断点时:

final int[] acc_swap = ACCELEROMETER_AXIS_SWAP[screenRotation];

当我每次断点命中时检查其值时,screenRotation值在0到3之间交替变换。如上所示,我将代码设置为每秒只执行第二个断点行。有谁知道为什么screenRotation在静态横向方向上交替值,即使我的代码永远不会重新分配它的值?

0 个答案:

没有答案