我正在开发Android应用程序,在那里我可以设置我的建筑物的室内导航系统。我正在使用Accelorometer传感器来开发系统。但我很少陷入导航箭头的运动。箭头没有移动。
mycode的:
package com.sample.accelerometer;
import android.content.Context;
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.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager senSensorManager;
private Sensor senAccelerometer;
private long lastUpdate = 0;
private float last_x, last_y, last_z;
TextView textView;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.act);
imageView = (ImageView) findViewById(R.id.imageView);
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
senSensorManager.unregisterListener(this);
}
@Override
protected void onResume() {
super.onResume();
senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Sensor mySensor = sensorEvent.sensor;
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];
long curTime = System.currentTimeMillis();
if ((curTime - lastUpdate) > 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
last_x = x;
last_y = y;
last_z = z;
textView.setText("X = "+x+" ,Y = "+y+" , Z = "+z);
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation a = new TranslateAnimation(
Animation.ABSOLUTE,-last_x, Animation.ABSOLUTE,last_x,
Animation.ABSOLUTE,-last_y, Animation.ABSOLUTE,last_y);
a.setDuration(1000);
animationSet.addAnimation(a);
imageView.startAnimation(animationSet);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
这是我上面的代码,请建议我在导航方向上移动箭头的一些解决方案。