我正在尝试从加速度计添加到 LinkedList 值。
private LinkedList<Float> rawValues = new LinkedList<Float>();
public void onSensorChanged(SensorEvent event)
{
int eventType = event.sensor.getType();
switch(eventType)
{
case Sensor.TYPE_ACCELEROMETER:
float accelerometer_z = event.values[2];
rawValues.add(accelerometer_z);
/**
* This printout gives me positions and event values
*/
System.out.println("getAccelerometer size: " +
rawValues.size() + " entry: " + rawValues.getLast());
break;
}
}
到目前为止一切顺利,我得到了预期的读数和数字位置。但是,当我尝试用其他方法对列表进行处理时,它似乎是空的!
public String[] getVelocity()
{
String[] velocityString = {"42","42"};
String values = "";
String theInteger = "";
/**
* Returns a 0 size...
*/
int theSize = rawValues.size();
System.out.println("getVelocity size: " + theSize);
LinkedList<Float> slopeValues = calculateSlopeValues( rawValues);
for (int i = 0; i < theSize; i++)
{
values += "Raw:"+i+ " " + String.valueOf(rawValues.get(i)) + "\n";
values += "\tSlope:"+i+ " " + String.valueOf(slopeValues.get(i)) + "\n";
}
theInteger = String.valueOf(Math.round(Collections.max(slopeValues)*100));
/**
* After adding everything to local variables, the list is cleared.
*/
slopeValues.clear();
velocityString[0] = theInteger;
velocityString[1] = values;
return velocityString;
}
getVelocity 方法是从主活动中的 onTouch 调用的。请给点光好吗?
问候/米
重写的 onTouch 方法:
@Override
public boolean onTouch(View view, MotionEvent event)
{
MySensorEventListener eventListener = new MySensorEventListener();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
String[] strings = eventListener.getVelocity();
String velocity_int = strings[0];
String velocity_string = strings[1];
velocityView.setText(velocity_int);
listView.setText(velocity_string);
break;
}
return false;
}