如何拥有自动缩放图?

jyztefdp  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(249)

怎么能有一个能从传感器读取实时数据的图形呢?x轴上的0点总是可见的,所以随着时间的推移,图形越来越紧了?这是我的意思
我试图使一个应用程序与光传感器,将显示现场,但我需要的图表,以规模,使所有的读数都可以在任何时候看到。
这是我到目前为止写的:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Vibrator;
import android.widget.TextView;
import android.widget.Toast;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
 private SensorManager sensorManager;                           //sensor manager is what allows for the access to the sensors
 private float light;
 private TextView currentLight;
 long tStart = System.currentTimeMillis();
 private TextView status;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  status = (TextView) findViewById(R.id.statusT);
  currentLight = (TextView) findViewById(R.id.currentLightVal);
  sensorManager = (SensorManager)
          getSystemService(Context.SENSOR_SERVICE);
 }

 @Override
 protected void onResume() {                                        //when the application is resumed (if the user is interacting with it)
  super.onResume();
  loadLightLevel();
 }
 @Override
 protected void onPause() {                                        //when the application is paused (if the user stops interacting with it)
  super.onPause();
  unregisterAll();
 }
 private void loadLightLevel() {
  Sensor sensor =
          sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);     //this will identify the sensor and calls it "sensor"
  if (sensor != null) {                                                        //if there is a sensor
   sensorManager.registerListener(this, sensor,
           SensorManager.SENSOR_DELAY_FASTEST);                                //set the delay between readings to get them as fast as possible
  } else {
   Toast.makeText(this, "No Ambient Temperature Sensor !",        //toast to say the sensor was not found
           Toast.LENGTH_LONG).show();
  }
 }
 private void unregisterAll() {
  sensorManager.unregisterListener(this);
 }                                                                              //unregisters the listener for the sensor
 @Override
 public void onSensorChanged(SensorEvent sensorEvent) {                         //sensor event holds data about the sensor and what it has read
  long tEnd = System.currentTimeMillis();
  long tDelta = tEnd - tStart;
  double elapsedSeconds = tDelta / 1000.0;
  GraphView graph = (GraphView) findViewById(R.id.graph);
  LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
          new DataPoint(elapsedSeconds, currentLight),
  });
  graph.addSeries(series);
  if (sensorEvent.values.length > 0) {                                          //if the values array is holding anything
   light = sensorEvent.values[0];                                         //the "temperature" float is made equal to the value in the 0th element of the values array of the sensor event
   currentLight.setText(Float.toString(light));                            //float to string for the text view
   getSupportActionBar().setTitle("Sensor 1: Light sensor");                                              //edits the top bar of the application
   if (light > 11) {                                                     //if the temperature is higher than the current highest recording
    //make the current temperature the new highest
    status.setText("Light is on!");                                   //edit text view
   } else {
    status.setText("Light is off!");                                   //edit text view
   }
  }
 }

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题