有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java传感器。键入旋转向量没有值?

程序员们

我试图在Android Studio中创建一个应用程序,每当屏幕倾斜45度或更大时,屏幕就会改变颜色。问题是什么都没发生

Listener成功注册,但我在onSensorChanged方法中准备的toast从未被调用,这告诉我没有收到任何读数。有什么是我忽略的吗

致以最良好的问候和感谢

编辑:我按照安迪的建议做了祝酒词(谢谢)。现在我可以看到我得到了什么读数,因为应用程序没有按预期工作

EDIT2:在能够观看读数后,我让应用程序开始工作!转换成学位的过程确实如期进行,我得到了持续的读数。必须把祝酒词改成文本视图才能让它有意义问题已解决

public class RotationVectorActivity extends AppCompatActivity implements SensorEventListener {
    private SensorManager sensorManager;
    private Sensor rotationVector;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rotation_vector_layout);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        rotationVector = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);

        if(rotationVector == null) {
            MainActivity.showToast(this, "Proximity sensor not available.");
            finish();
        } else {
            registerListener();
        }
    }

    public void launchMain(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        Toast.makeText(getApplicationContext(), "We got this far?", Toast.LENGTH_SHORT);
        float[] rotationMatrix = new float[16];
        SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);

        //Remap coordinate system
        float[] remappedRotationMatrix = new float[16];
        SensorManager.remapCoordinateSystem(rotationMatrix,
                SensorManager.AXIS_X,
                SensorManager.AXIS_Z,
                remappedRotationMatrix);

        //Convert to orientations
        float[] orientations = new float[3];
        SensorManager.getOrientation(remappedRotationMatrix, orientations);

        //Convert radians to degrees
        for(int i = 0; i < 3; i++) {
            orientations[i] = (float)(Math.toDegrees(orientations[i]));
        }

        if(orientations[2] > 45) {
            getWindow().getDecorView().setBackgroundColor(Color.YELLOW);
        } else if (orientations[2] < -45) {
            getWindow().getDecorView().setBackgroundColor(Color.BLUE);
        } else if (Math.abs(orientations[2]) < 10) {
            getWindow().getDecorView().setBackgroundColor(Color.WHITE);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        registerListener();
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterListener();
    }

    public void registerListener() {
        sensorManager.registerListener(this, rotationVector, SensorManager.SENSOR_DELAY_NORMAL);
        MainActivity.showToast(this, "Rotation vector listener registered.");

    }

    public void unregisterListener() {
        sensorManager.unregisterListener(this);
        MainActivity.showToast(this, "Rotation vector listener unregistered.");
    }
}

共 (0) 个答案