如何拥有自动缩放图?

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

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

  1. import androidx.appcompat.app.AppCompatActivity;
  2. import android.os.Bundle;
  3. import android.content.Context;
  4. import android.hardware.Sensor;
  5. import android.hardware.SensorEvent;
  6. import android.hardware.SensorEventListener;
  7. import android.hardware.SensorManager;
  8. import android.os.Vibrator;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11. import com.jjoe64.graphview.GraphView;
  12. import com.jjoe64.graphview.series.DataPoint;
  13. import com.jjoe64.graphview.series.LineGraphSeries;
  14. import java.util.Timer;
  15. import java.util.TimerTask;
  16. public class MainActivity extends AppCompatActivity implements SensorEventListener {
  17. private SensorManager sensorManager; //sensor manager is what allows for the access to the sensors
  18. private float light;
  19. private TextView currentLight;
  20. long tStart = System.currentTimeMillis();
  21. private TextView status;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. status = (TextView) findViewById(R.id.statusT);
  27. currentLight = (TextView) findViewById(R.id.currentLightVal);
  28. sensorManager = (SensorManager)
  29. getSystemService(Context.SENSOR_SERVICE);
  30. }
  31. @Override
  32. protected void onResume() { //when the application is resumed (if the user is interacting with it)
  33. super.onResume();
  34. loadLightLevel();
  35. }
  36. @Override
  37. protected void onPause() { //when the application is paused (if the user stops interacting with it)
  38. super.onPause();
  39. unregisterAll();
  40. }
  41. private void loadLightLevel() {
  42. Sensor sensor =
  43. sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); //this will identify the sensor and calls it "sensor"
  44. if (sensor != null) { //if there is a sensor
  45. sensorManager.registerListener(this, sensor,
  46. SensorManager.SENSOR_DELAY_FASTEST); //set the delay between readings to get them as fast as possible
  47. } else {
  48. Toast.makeText(this, "No Ambient Temperature Sensor !", //toast to say the sensor was not found
  49. Toast.LENGTH_LONG).show();
  50. }
  51. }
  52. private void unregisterAll() {
  53. sensorManager.unregisterListener(this);
  54. } //unregisters the listener for the sensor
  55. @Override
  56. public void onSensorChanged(SensorEvent sensorEvent) { //sensor event holds data about the sensor and what it has read
  57. long tEnd = System.currentTimeMillis();
  58. long tDelta = tEnd - tStart;
  59. double elapsedSeconds = tDelta / 1000.0;
  60. GraphView graph = (GraphView) findViewById(R.id.graph);
  61. LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
  62. new DataPoint(elapsedSeconds, currentLight),
  63. });
  64. graph.addSeries(series);
  65. if (sensorEvent.values.length > 0) { //if the values array is holding anything
  66. 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
  67. currentLight.setText(Float.toString(light)); //float to string for the text view
  68. getSupportActionBar().setTitle("Sensor 1: Light sensor"); //edits the top bar of the application
  69. if (light > 11) { //if the temperature is higher than the current highest recording
  70. //make the current temperature the new highest
  71. status.setText("Light is on!"); //edit text view
  72. } else {
  73. status.setText("Light is off!"); //edit text view
  74. }
  75. }
  76. }
  77. @Override
  78. public void onAccuracyChanged(Sensor sensor, int i) {
  79. }
  80. }

暂无答案!

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

相关问题