Android 三轴绘制实时加速度曲线
public class GsensorActivity extends Activity implements SensorEventListener { private static final String TAG = "GsensorActivity"; private SensorManager mSensorManager; private Sensor mSensor; private TextView textviewX; private TextView textviewY; private TextView textviewZ; private TextView textviewF; private float mX, mY, mZ; private long lasttimestamp = 0; Calendar mCalendar; private GraphicalView chart; /** 曲线数量 */ private static final int SERIES_NR = 1; private TimeSeries series1; private XYMultipleSeriesDataset dataset1; private float addY = -1; private long addX; /** 时间数据 */ Date[] xcache = new Date[20]; /** 数据 */ float[] ycache = new float[20]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gsensor); textviewX = (TextView) findViewById(R.id.textView1); textviewY = (TextView) findViewById(R.id.textView3); textviewZ = (TextView) findViewById(R.id.textView4); textviewF = (TextView) findViewById(R.id.textView2); lasttimestamp = System.currentTimeMillis(); LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout); // 生成图表 chart = ChartFactory.getTimeChartView(this, getDateDemoDataset(), getDemoRenderer(), "hh:mm:ss"); layout.addView(chart, new LayoutParams(LayoutParams.WRAP_CONTENT, 380)); mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);// TYPE_GRAVITY if (null == mSensorManager) { Log.d(TAG, "deveice not support SensorManager"); } // 参数三,检测的精准度 mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);// SENSOR_DELAY_GAME } private void updateChart(float a) { // 设定长度为20 int length = series1.getItemCount(); if (length >= 20) length = 20; addY = a; addX = new Date().getTime(); // 将前面的点放入缓存 for (int i = 0; i < length; i++) { xcache[i] = new Date((long) series1.getX(i)); ycache[i] = (float) series1.getY(i); } series1.clear(); series1.add(new Date(addX), addY); for (int k = 0; k < length; k++) { series1.add(xcache[k], ycache[k]); } // 在数据集中添加新的点集 dataset1.removeSeries(series1); dataset1.addSeries(series1); // 曲线更新 chart.invalidate(); } /** * 设定如表样式 * * @return */ private XYMultipleSeriesRenderer getDemoRenderer() { XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); renderer.setChartTitle("实时曲线");// 标题 renderer.setChartTitleTextSize(20); renderer.setXTitle("时间"); // x轴说明 renderer.setAxisTitleTextSize(16); renderer.setAxesColor(Color.BLACK); renderer.setLabelsTextSize(15); // 数轴刻度字体大小 renderer.setLabelsColor(Color.BLACK); renderer.setLegendTextSize(15); // 曲线说明 renderer.setXLabelsColor(Color.BLACK); renderer.setYLabelsColor(0, Color.BLACK); renderer.setShowLegend(false); renderer.setMargins(new int[] { 20, 30, 100, 0 }); XYSeriesRenderer r = new XYSeriesRenderer(); r.setColor(Color.BLUE); r.setChartValuesTextSize(15); r.setChartValuesSpacing(3); r.setPointStyle(PointStyle.CIRCLE); r.setFillBelowLine(true); r.setFillBelowLineColor(Color.WHITE); r.setFillPoints(true); renderer.addSeriesRenderer(r); renderer.setMarginsColor(Color.WHITE); renderer.setPanEnabled(false, false); renderer.setShowGrid(true); renderer.setYAxisMax(50); renderer.setYAxisMin(-30); renderer.setInScroll(true); // 调整大小 return renderer; } /** * 数据对象 * * @return */ private XYMultipleSeriesDataset getDateDemoDataset() { dataset1 = new XYMultipleSeriesDataset(); final int nr = 10; long value = new Date().getTime(); Random r = new Random(); for (int i = 0; i < SERIES_NR; i++) { series1 = new TimeSeries("Demo series " + (i + 1)); for (int k = 0; k < nr; k++) { series1.add(new Date(value + k * 1000), 0); } dataset1.addSeries(series1); } Log.i(TAG, dataset1.toString()); return dataset1; } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public void onSensorChanged(SensorEvent event) { if (event.sensor == null) { return; } if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; mCalendar = Calendar.getInstance(); long stamp = mCalendar.getTimeInMillis();// 1393844912 textviewX.setText(String.valueOf(x)); textviewY.setText(String.valueOf(y)); textviewZ.setText(String.valueOf(z)); int second = mCalendar.get(Calendar.SECOND);// 53 float px = Math.abs(mX - x); float py = Math.abs(mY - y); float pz = Math.abs(mZ - z); Log.d(TAG, "pX:" + px + " pY:" + py + " pZ:" + pz + " stamp:" + stamp + " second:" + second); float maxvalue = getMaxValue(px, py, pz); if (maxvalue > 2 && (stamp - lasttimestamp) > 200) { lasttimestamp = stamp; updateChart(x+y+z); textviewF.setText("检测手机在移动.."); } mX = x; mY = y; mZ = z; } } /** * 获取一个最大值 * * @param px * @param py * @param pz * @return */ public float getMaxValue(float px, float py, float pz) { float max = 0; if (px > py && px > pz) { max = px; } else if (py > px && py > pz) { max = py; } else if (pz > px && pz > py) { max = pz; } return max; } }