0
我正在開發Android應用程序,其中我想水平滾動大圖像。我使用加速度計(Sensor.TYPE_ACCELEROMETER)和磁場(Sensor.TYPE_MAGNETIC_FIELD)數據來獲取旋轉角度。這些數據會頻繁出現噪音,我無法實現平穩的運動效果。沿X軸旋轉的手機角度
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_MAGNETIC_FIELD:
mags = event.values.clone();
break;
case Sensor.TYPE_ACCELEROMETER:
accels = event.values.clone();
break;
}
if (mags != null && accels != null) {
gravity = new float[16];
boolean success = SensorManager.getRotationMatrix(gravity, null, accels, mags);
if (success) {
float[] outGravity = new float[16];
SensorManager.remapCoordinateSystem(gravity, SensorManager.AXIS_X, SensorManager.AXIS_Z, outGravity);
SensorManager.getOrientation(outGravity, values);
rollingAverage[0] = roll(rollingAverage[0], values[0]);
rollingAverage[1] = roll(rollingAverage[1], values[1]);
rollingAverage[2] = roll(rollingAverage[2], values[2]);
azimuth = Math.toDegrees(values[0]);
pitch = Math.toDegrees(values[1]);
roll = Math.toDegrees(values[2]);
mags = null;
accels = null;
double diffRoll = lastRoll - roll;
double diffPitch = lastPitch - pitch;
long curTime = System.currentTimeMillis();
if (Math.abs(diffRoll) >= 2) {
if (diffRoll > 0)
imageView.panLeft();
else
imageView.panRight();
lastRoll = roll;
}
}
}
}
有關使用其他方法實現此目的的任何想法?
加速度傳感器是一個嘈雜的傳感器。除非你使用一些DSP算法來過濾/平滑它,否則你會跳到所有地方。看起來你正在試圖用roll()函數來做到這一點,但我們需要看到它來知道你已經在做什麼。 –
這是如何在運動遊戲或VR中實現的? – Sameer