3

実行時に加速度計の値 (つまり、X 値と Y 値) を継続的に取得しています。私の質問は、モバイルで加速度計の値が変化した場合、対応する値を変化に応じて累積する必要があるということです。

これは私のコードです:

public class MainActivity extends Activity implements SensorEventListener {
    private SensorManager sensorManager;

    private float accumulation_x = 0;
    private float accumulation_y = 0;
    private float accumulation_z = 0;

    private TextView acessTextview, angleTextview;
    private float value;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById();

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);

    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {





            int count = 1;
                    while(count!=0){

                        float x = (float) 0.9887777;
                        float y = (float) 0.187359372; 
                        float z = (float) 0.0228636;

                float X_axis = (float) (x + (0.02724095));
                float Y_axis = (float) (y + (-0.027792556));
                float Z_axis = (float) (z - (0.105689));

                accumulation_x = accumulation_x + X_axis;
                accumulation_y = accumulation_y + Y_axis;
                accumulation_z = accumulation_z + Z_axis;

                value = (y / z);
                float angle = (float) Math.toDegrees(Math.atan(value));
                angleTextview.setText("Angle:" + angle);

                acessTextview.setText("accumulation_x  :" + X_axis + "\n"
                        + "accumulation_y :" + Y_axis + "\n"
                        + "accumulation_z  :" + Z_axis);

                count++;

                    }

        }

    }

    private void findViewById() {
        // TODO Auto-generated method stub
        acessTextview = (TextView) findViewById(R.id.accessTextview);
        angleTextview = (TextView) findViewById(R.id.angleTextview);
    }

}
4

1 に答える 1

2

あなたのコードは正しいです。マイナーな変更のみが必要です。refreshValues() という追加のメソッドでコードを更新しました。このメソッドは、X、Y の最新の値を TextView に設定します。このメソッドは onSensorChanged() メソッドから呼び出されます。

public class MainActivity extends Activity implements SensorEventListener {
    private SensorManager sensorManager;

    private float accumulation_x = 0;
    private float accumulation_y = 0;
    private float accumulation_z = 0;

    private TextView acessTextview, angleTextview;
    private float value;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById();

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);

    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {   }

    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            int count = 1;
                    while(count!=0){

                        float x = (float) 0.9887777;
                        float y = (float) 0.187359372; 
                        float z = (float) 0.0228636;

                float X_axis = (float) (x + (0.02724095));
                float Y_axis = (float) (y + (-0.027792556));
                float Z_axis = (float) (z - (0.105689));

                accumulation_x = accumulation_x + X_axis;
                accumulation_y = accumulation_y + Y_axis;
                accumulation_z = accumulation_z + Z_axis;

                value = (y / z);
                float angle = (float) Math.toDegrees(Math.atan(value));
                angleTextview.setText("Angle:" + angle);

                acessTextview.setText("accumulation_x  :" + X_axis + "\n"
                        + "accumulation_y :" + Y_axis + "\n"
                        + "accumulation_z  :" + Z_axis);

                count++;

                    }

        }

        refreshValues ( accumulation_x,accumulation_y );
    }

    private void findViewById() {
        // TODO Auto-generated method stub
        acessTextview = (TextView) findViewById(R.id.accessTextview);
        angleTextview = (TextView) findViewById(R.id.angleTextview);
    }

    private void refreshValues ( float x, float y )
    {
        acessTextview.setText ( String.valueOf(x) );
        angleTextview.setText( ( String.valueOf(y)));
    }

}
于 2013-04-04T06:26:49.267 に答える