2013-04-04 36 views
3

我在運行時連續獲取加速度計值(即)X和Y值。我的問題是,當加速度計的值在移動時發生變化時,相應的值應根據變化進行累加。Android Accelerometer Accumulation

這是我的代碼:

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); 
    } 

} 
+0

沒有得到你的問題。你究竟想達到什麼目的? – Nezam 2013-04-04 06:20:30

+0

他的應用程序可能被迫關閉。 while循環的條件總是如此。 – 2013-04-04 06:23:51

+0

我無法在X,Y,Z中刷新值。 – user2243468 2013-04-04 06:26:50

回答

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))); 
    } 

} 
相關問題