Skip to content

Instantly share code, notes, and snippets.

@MeilCli
Created January 19, 2017 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MeilCli/cc34e7af739bd9d066b1fe6d24d36a71 to your computer and use it in GitHub Desktop.
Save MeilCli/cc34e7af739bd9d066b1fe6d24d36a71 to your computer and use it in GitHub Desktop.
package com.example.yoshihiro.streetrhythm;
import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
public class Sensor_Activity extends Activity implements SensorEventListener{
SensorManager manager;
Sensor sensor;
TextView xTextView;
TextView yTextView;
TextView zTextView;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor_);
xTextView = (TextView) findViewById(R.id.xValue);
yTextView = (TextView) findViewById(R.id.yValue);
zTextView = (TextView) findViewById(R.id.zValue);
manager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mChart = (LineChart) findViewById(R.id.lineChart);
mChart.setDescription(""); // 表のタイトルを空にする
mChart.setData(new LineData()); // 空のLineData型インスタンスを追加
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy){
}
@Override
protected void onResume(){
super.onResume();
manager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void onPause(){
super.onPause();
manager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event){
xTextView.setText(String.valueOf(event.values[0]));
yTextView.setText(String.valueOf(event.values[1]));
zTextView.setText(String.valueOf(event.values[2]));
LineData data = mChart.getLineData();
if(data != null){
for(int i = 0; i < 3; i++){ // 3軸なのでそれぞれ処理します
ILineDataSet set = data.getDataSetByIndex(i);
if(set == null){
set = createSet(names[i], colors[i]); // ILineDataSetの初期化は別メソッドにまとめました
data.addDataSet(set);
}
data.addEntry(new Entry(set.getEntryCount(), event.values[i]), i); // 実際にデータを追加する
data.notifyDataChanged();
}
mChart.notifyDataSetChanged(); // 表示の更新のために変更を通知する
mChart.setVisibleXRangeMaximum(50); // 表示の幅を決定する
mChart.moveViewToX(data.getEntryCount()); // 最新のデータまで表示を移動させる
}
}
LineChart mChart;
String[] names = new String[]{"x-value", "y-value", "z-value"};
int[] colors = new int[]{Color.RED, Color.GREEN, Color.BLUE};
private LineDataSet createSet(String label, int color){
LineDataSet set = new LineDataSet(null, label);
set.setLineWidth(2.5f); // 線の幅を指定
set.setColor(color); // 線の色を指定
set.setDrawCircles(false); // ポイントごとの円を表示しない
set.setDrawValues(false); // 値を表示しない
return set;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment