• 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;
        }
    }
  • 相关阅读:
    BZOJ 1412: [ZJOI2009]狼和羊的故事
    Bzoj 2443: [Usaco2011 Open]奇数度数
    Bzoj 1101: [POI2007]Zap
    BZOJ 2186: [Sdoi2008]沙拉公主的困惑
    BZOJ 4804: 欧拉心算 欧拉函数
    Luogu P3121 [USACO15FEB]审查(黄金)Censoring (Gold)
    Luogu P3000 [USACO10DEC]牛的健美操Cow Calisthenics
    BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
    BZOJ 3297: [USACO2011 Open]forgot
    BZOJ 2456: mode
  • 原文地址:https://www.cnblogs.com/chenlong-50954265/p/5579774.html
Copyright © 2020-2023  润新知