본문 바로가기

PROJECT

[Android Studio] 가속도 센서 받기 - graph 사용

※ char와 똑같이 설치해 줘야하는 것을 반드시 이행

 

설치법

   -build.gradle(app)

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.2'
    implementation 'com.jjoe64:graphview:4.1.0'
}

   위에 코드에서 맨 아래것을 추가해주면 된다. implementation 'com.jjoe64:graphview:4.1.0'

 

- MainActivity.java

package org.techtown.accelerometersensor;

import android.annotation.SuppressLint;
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.app.Activity;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.data.LineData;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.Viewport;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.jjoe64.graphview.LegendRenderer;

import android.content.Context;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;


/*https://github.com/jit0434/Acellerometer/blob/master/AccelDataPlot/app/src/main/java/com/example/papa/acceldataplot/MainActivity.java*/
public class MainActivity extends AppCompatActivity implements SensorEventListener {


    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    TextView xValue, yValue, zValue;
    private Thread thread;
    private LineGraphSeries<DataPoint> mSeriesAccelX,mSeriesAccelY,mSeriesAccelZ;
    private GraphView mGraphAccel;
    private double graphLastAccelXValue = 10d;
    private GraphView line_graph;

    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //선을 나타낼 수 있는 가속도 센서 선택
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);

        if (mAccelerometer != null) {
            //센서를 속도를 설정하여 읽어온다.
            mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
        }
        mSeriesAccelX = initSeries(Color.BLUE, "X"); //라인 그래프를 그림
        mSeriesAccelY = initSeries(Color.RED, "Y");
        mSeriesAccelZ = initSeries(Color.GREEN, "Z");
        mGraphAccel = initGraph(R.id.graph, "X, Y, Z direction Acceleration");

        //그래프에 x,y,z 추가
        mGraphAccel.addSeries(mSeriesAccelX);
        mGraphAccel.addSeries(mSeriesAccelY);
        mGraphAccel.addSeries(mSeriesAccelZ);



        xValue = (TextView) findViewById(R.id.xValue);
        yValue = (TextView) findViewById(R.id.yValue);
        zValue = (TextView) findViewById(R.id.zValue);

    }
    //그래프 초기화
    public GraphView initGraph(int id, String title) {
        GraphView graph = findViewById(id);
        //데이터가 늘어날때 x축 scroll이 생기도록
        graph.getViewport().setXAxisBoundsManual(true);
        graph.getViewport().setMinX(0);
        graph.getViewport().setMaxX(5);
        graph.getGridLabelRenderer().setLabelVerticalWidth(100);
        graph.setTitle(title);
        graph.getGridLabelRenderer().setHorizontalLabelsVisible(false);
        graph.getLegendRenderer().setVisible(true);
        graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
        return graph;
    }

    public void onSensorChanged(SensorEvent event) {
        float x, y, z;
        x = event.values[0];
        y = event.values[1];
        z = event.values[2];

        xValue.setText("xValue: " + x);
        yValue.setText("yValue: " + y);
        zValue.setText("zValue: " + z);

        graphLastAccelXValue += 0.05d;

        mSeriesAccelX.appendData(new DataPoint(graphLastAccelXValue,x),true,100);
        mSeriesAccelY.appendData(new DataPoint(graphLastAccelXValue,y),true,100);
        mSeriesAccelZ.appendData(new DataPoint(graphLastAccelXValue,z),true,100);
            
            
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        
    }
    //x,y,z 데이터 그래프 추가
    public LineGraphSeries<DataPoint> initSeries(int color, String title){
        LineGraphSeries<DataPoint> series;
        series = new LineGraphSeries<>();
        series.setDrawDataPoints(true);
        series.setDrawBackground(true);
        series.setColor(color);
        series.setTitle(title);
        return series;
    }

    protected void onPause(){
        super.onPause();
        if(thread!=null){
            thread.interrupt();
        }
        mSensorManager.unregisterListener(this); // 센서 반납
    }

 }

 

 

-xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        android:background="#FCFCFD"
        android:orientation="vertical"></LinearLayout>

    <com.jjoe64.graphview.GraphView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/graph" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/xValue"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/yValue"
        android:layout_marginTop="30sp"
        android:layout_below="@+id/xValue"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/zValue"
        android:layout_marginTop="30sp"
        android:layout_below="@+id/yValue"/>


</RelativeLayout>

 

-실행화면