我剛剛接觸android,我正在開發一個應用程序來學習更多關於移動傳感器的知識。首先我使用加速度計。如何生成帶有加速度計值的圖形?
首先,我創建了一個帶有兩個按鈕(開始和結束)的佈局,以獲取加速度計值。
<?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="com.example.ivope.sensormanipulator.SensorManipulator">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_x="50dp"
android:layout_y="46dp"
android:text="Accelerometer Signal Analyser"
android:textColor="@color/black"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.197"
android:layout_below="@+id/graph"
android:layout_alignRight="@+id/btnStop"
android:layout_alignEnd="@+id/btnStop"
android:layout_marginTop="71dp" />
<com.jjoe64.graphview.GraphView
android:layout_width="match_parent"
android:layout_height="200dip"
android:id="@+id/graph"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_x="75dp"
android:layout_y="167dp"
android:background="@color/green"
android:clickable="true"
android:text="Start"
tools:layout_editor_absoluteX="56dp"
tools:layout_editor_absoluteY="231dp"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_marginTop="75dp" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="220dp"
android:layout_y="167dp"
android:background="@color/red"
android:clickable="true"
android:text="Finish"
tools:layout_editor_absoluteX="242dp"
tools:layout_editor_absoluteY="231dp"
android:layout_marginRight="47dp"
android:layout_marginEnd="47dp"
android:layout_alignBaseline="@+id/btnStart"
android:layout_alignBottom="@+id/btnStart"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
然後在主類上,我實現了SensorManager和onResume,onPause和onSensorChanged函數。還實現了onclick函數啓動(運行onResume)並完成(運行onPause)。它假設給我3個值:ax,ay和az。用這3個值我想生成一個圖。我嘗試使用Android GraphView,我的應用程序構建成功,但它不運行。有人可以幫幫我嗎?是我試圖讓加速度計值正確的方式嗎?我怎樣才能生成這些值的圖形?
Java代碼:使用加速計值
package com.example.ivope.sensormanipulator;
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.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
public class SensorManipulator extends AppCompatActivity implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mAccelerometer;
double ax, ay, az, y;
public SensorManipulator() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor_manipulator);
GraphView graph = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>();
for (int i = 0; i < 5; i++){
ax = ax + 0.1;
y = Math.sin(ax);
series.appendData(new DataPoint(ax, y), true, 5);
}
graph.addSeries(series);
Button btnStart = (Button) findViewById(R.id.btnStart);
Button btnStop = (Button) findViewById(R.id.btnStop);
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onResume();
}
}
);
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onPause();
}
}
);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
ax = event.values[0];
ay = event.values[1];
az = event.values[2];
}
}
}
使用日誌來了解您的應用崩潰的位置。 https://developer.android.com/reference/android/util/Log.html –