0 ratings0% found this document useful (0 votes) 42 views4 pagesAndroid Studio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
File ~C:\Usersvansh\AndroiaStudioProjectsIMicrolappisrcimainjavalcomiexample\micro\MainActivity.java
ONO RANE
11
package com.example.micro;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.hardware.Sensor;
import android. hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android. hardware. SensorManager;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.jj0e64.graphview.GraphView;
import com.jj0e64.graphview. Viewport;
import com.jj0e64.graphview. series .DataPoint;
import com.jj0e64.graphview. series. LineGraphSeries;
public class MainActivity extends AppCompatActivity {
TextView txt_currentAccel, txt_prevAccel,
txt_acceleration;
ProgressBar prog_shakeMeter;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private double accelerationCurrentValue;
private double accelerationPreviousValue;
private int pointsPlotted = 5;
private int graphIntervalcounter = 0;
private Viewport viewport;
LineGraphSeries series = new
LineGraphSeries(new DataPoint[] {
new DataPoint(9, 0),
new DataPoint(6, 0),
new DataPoint(6, 0),
new DataPoint(6, 0),
Page tofFile ~ \Usersvansh'AndroidStudioProjectsIMicrolappisrcimainjavalcomiexample\micro\MainActivity.java
40 new DataPoint(0, 0)
41 vn;
42
43 private final SensorEventListener
sensorEventListener = new SensorEventListener() {
45 @0verride
46 public void onSensorChanged(SensorEvent
sensorEvent) {
47 float x = sensorEvent.values[@];
48 float y = sensorEvent.values[1]
49 float z = sensorEvent.values[2]
50
51 accelerationCurrentValue = Math.sqrt((x
ex tye ytz4*2));
52 double changeInAccelleration = Math.abs(
accelerationCurrentValue - accelerationPreviousValue
di
53 accelerationPreviousValue =
accelerationCurrentValue;
54
55 txt_currentAccel.setText("Current = "+ (
int) accelerationCurrentValue) ;
56 txt_prevAccel.setText("prev = "+ (int)
accelerationPreviousValue) ;
57 txt_acceleration.setText ("Acceleration
Change = " + (int) changeInAccelleration);
58
59 prog_shakeMeter. setProgress((int)
changeInAcceLleration) ;
60
61 if (changeInAccelleration > 10) {
62 txt_acceleration. setBackgroundColor(
Color.RED) ;
63 } else if (changeInAccelleration > 5) {
64 txt_acceleration. setBackgroundColor(
Color.parseColor("#fcad03")) ;
65 } else if (changeInAccelleration > 2) {
66 txt_acceleration.setBackgroundColor(
Color. YELLOW) ;
67 t
Page 20f4File ~ Usersvansh'AndroiaStudioProjects|Microlappisrcimainjavalcomiexample\micro\MainActivity.java
68
69 pointsPlotted++;
70
71 series .appendData(new DataPoint(
pointsPlotted , changeInAccelleration), true
pointsPlotted) ;
72 viewport. setMinX(pointsPlotted) ;
73 viewport.setMinX(pointsPlotted - 200);
74 +
7
76 @Override
77 public void onAccuracyChanged(Sensor sensor
, int accuracy) {
78
79 t
80
81 ty
82
83
84
85 @0verride
86 protected void onCreate (Bundle
savedInstanceState) {
87 super.onCreate(savedInstanceState) ;
88 setContentView(R.layout.activity_main) ;
89
90 txt_acceleration = findViewById(R.id.
txt_accel);
91 txt_currentAccel = findViewById(R.id.
txt_currentAccel);
92 txt_prevAccel = findViewById(R.id.
txt_prevAccel);
93
94 prog_shakeMeter = findViewById(R.id.
prog_shakeMeter) ;
95
96 mSensorManager = (SensorManager)
getSystemService (SENSOR_SERVICE) ;
97 mAccelerometer = mSensorManager.
getDefaultSensor (Sensor. TYPE_ACCELEROMETER) ;
98
Page 3 ofFile - Usersvansh'AndroiaStudioProjects!Microlappisrcmainjavalcomiexample\micro\MainActivity.java
99 GraphView graph = (GraphView) findViewByld(R
id. graph);
100 viewport = graph.getViewport();
101 viewport.setScrollable(true) ;
102 viewport. setXAxisBoundsManual (true) ;
103
104 graph.addSeries(series) ;
105
106 +
107
108 protected void onResume() {
109 super .onResume();
110 mSensorManager .registerListener(
sensorEventListener, mAccelerometer, SensorManager
SENSOR_DELAY_NORMAL) ;
111 t
112
113 protected void onPause() {
114 super. onPause();
115 mSensorManager.unregisterListener(
sensorEventListener) ;
116 +
117
118 }
119
Page 4014