<repo_name>ylzz1997/graduateDesign<file_sep>/app/src/main/java/com/lhy/ggraduatedesign/DateProcessor.java
package com.lhy.ggraduatedesign;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.widget.ProgressBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DateProcessor {
    public final static double PER_TIME = 0.015;
    private List<float[]> angal;
    private List<float[]> accelerations;
    private List<float[]> gyroscopes;
    private List<Float> z = new ArrayList<>();
    private List<Float> zx = new ArrayList<>();

    private float gyroscopesAngel;
    private float finalAngal;
    private float finalSpeed;
    public void setAngal(List<float[]> angal) {
        this.angal = angal;
    }

    public void setAccelerations(List<float[]> accelerations) {
        this.accelerations = accelerations;
    }


    public DateProcessor(List<float[]> angal, List<float[]> accelerations, List<float[]> gyroscopes) {
        this.angal = angal;
        this.accelerations = accelerations;
        this.gyroscopes = gyroscopes;

        gyroscopessum();
        ProcessAngle();
        speed();
    }

    private float PerProcessAngle(float[] i){
        float z = i[2] - 90;
        if(z<0){
            return 0;
        }else if(z>180){
            return 180;
        }else {
            return z;
        }
    }

    public float getFinalAngal() {
        return finalAngal;
    }

    public float getFinalSpeed() {
        return finalSpeed;
    }

    private void ProcessAngle(){
        for(float[] i : angal){
            z.add(PerProcessAngle(i));
            zx.add(i[2]);
        }
        if(z.size()-1>=0){
            int halfsize = (int)Math.ceil(zx.size()/2);
            List list = zx.subList(0,halfsize+1);
            float max = (float)Collections.max(list);
            float n = max+gyroscopesAngel-90;
            finalAngal = n;
        }
        else
            finalAngal =0;
    }

    private void speed(){
        float sum = 0;
        for(int i =0;i<accelerations.size();i++){
            double n = (accelerations.get(i))[0];
            if(n<0){
                n=-n;
            }
            sum+=n*PER_TIME;
        }
        finalSpeed = sum;
    }

    private void gyroscopessum(){
        float sum = 0;
        for(float[] n: gyroscopes){
            sum+=Math.abs(n[1])*0.18;
        }
        gyroscopesAngel=sum;
    }
}
<file_sep>/app/src/main/java/com/lhy/ggraduatedesign/DateBean.java
package com.lhy.ggraduatedesign;

public class DateBean {
    private double vx;
    private double vy;
    private float angle;
    private float speed;
    private double distant;
    private double height;
    private float bodyHeight;
    private double time;
    final static float G = 9.8f;
    final static double PI = Math.PI;

    //h=Vyt-1/2gt^2+height;
    //h=Vyt-1/2gt^2+height;


    public DateBean(float angle, float speed, float bodyHeight) {
        this.angle = angle;
        this.speed = speed;
        this.bodyHeight = bodyHeight;

        vx = speed * Math.sin(angle*Math.PI/180);
        vy = speed * Math.cos(angle*Math.PI/180);

        height = bodyHeight + (vy*vy)/(2*G);
        time = vy/G+Math.sqrt((height*2)/G);
        distant = vx*time;
    }

    public double getVx() {
        return vx;
    }

    public double getVy() {
        return vy;
    }

    public double getDistant() {
        return distant;
    }

    public double getHeight() {
        return height;
    }

    public double getTime() {
        return time;
    }

    public float getBodyHeight() {
        return bodyHeight;
    }
}
<file_sep>/app/src/main/java/com/lhy/ggraduatedesign/SensorUtil.java
package com.lhy.ggraduatedesign;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.SENSOR_SERVICE;

public class SensorUtil implements SensorEventListener {
    private float[] r = new float[9];
    private float[] values = new float[3];
    private float[] gravity = null;
    private float[] geomagnetic = null;
    private float[] linerAcceleration = null;
    private float[] gyroscope = null;
    private SensorManager mSensorManager;
    private Context context;
    private float[] result= new float[3];

    private List<float[]> angal = new ArrayList<>();
    private List<float[]> accelerations = new ArrayList<>();
    private List<float[]> gyroscopes = new ArrayList<>();

    private Sensor acceleSensor;
    private Sensor LacceleSensor;
    private Sensor magSensor;
    private Sensor gyroscrope;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0){
                if(gravity!=null && geomagnetic!=null) {
                    accelerations.add(new float[]{linerAcceleration[0],linerAcceleration[1],linerAcceleration[2]});
                    gyroscopes.add(new float[]{gyroscope[0],gyroscope[1],gyroscope[2]});
                        if(SensorManager.getRotationMatrix(r, null, gravity, geomagnetic)) {
                        SensorManager.getOrientation(r, values);
                        result[0] = (float) ((360f+values[0]*180f/Math.PI)%360);
                        result[1] = (float) ((360f+values[1]*180f/Math.PI)%360);
                        result[2] = (float) ((360f+values[2]*180f/Math.PI)%360);
                        angal.add(new float[]{result[0],result[1],result[2]});
                    }
            }
                /*
                result[0] = (float) ((360f+geomagnetic[0]*180f/Math.PI)%360);
                result[1] = (float) ((360f+geomagnetic[1]*180f/Math.PI)%360);
                result[2] = (float) ((360f+geomagnetic[2]*180f/Math.PI)%360);
                angal.add(new float[]{result[0],result[1],result[2]});
                TextView tv = ((Activity)context).findViewById(R.id.main_text);
                tv.setText("x="+result[0]+"y="+result[1]+"z="+result[2]+"  \n x="+gravity[0]+"y="+gravity[1]+"z="+gravity[2]);
                   */
            }
        }
    };


    public SensorUtil(Context context) {
        this.context = context;
        mSensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
        LacceleSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
        acceleSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        gyroscrope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    }

    public void Initialization(){
        mSensorManager.registerListener(this, LacceleSensor, SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this, acceleSensor, SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this, magSensor, SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this, gyroscrope, SensorManager.SENSOR_DELAY_GAME);
    }

    public void Pause(){
        mSensorManager.unregisterListener(this);
        handler.sendEmptyMessage(1);
    }



    @Override
    public void onSensorChanged(SensorEvent event) {
        System.out.print("执行中");
        switch (event.sensor.getType()) {
            case Sensor.TYPE_LINEAR_ACCELERATION:
                linerAcceleration = event.values;
                handler.sendEmptyMessage(0);
                break;
            case Sensor.TYPE_ACCELEROMETER:
                gravity = event.values;
                handler.sendEmptyMessage(0);
                break;
            case Sensor.TYPE_MAGNETIC_FIELD:
                geomagnetic = event.values;
                handler.sendEmptyMessage(0);
                break;
            case Sensor.TYPE_GYROSCOPE:
                gyroscope = event.values;
                handler.sendEmptyMessage(0);
                break;
        }
    }

    public void Reset(){
        angal = new ArrayList<>();
        accelerations = new ArrayList<>();
        gyroscopes = new ArrayList<>();
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    public List<float[]> getAngal() {
        return angal;
    }

    public void setAngal(List<float[]> angal) {
        this.angal = angal;
    }

    public List<float[]> getAccelerations() {
        return accelerations;
    }

    public Sensor getLacceleSensor() {
        return LacceleSensor;
    }

    public void setAccelerations(List<float[]> accelerations) {
        this.accelerations = accelerations;
    }

    public List<float[]> getGyroscopes() {
        return gyroscopes;
    }
}
