最初に使用TYPE_ORIENTATION
しましたが、これは減価償却されました。したがって、コードをより新しいバージョンに変更し、この投稿で提案されているように使用TYPE_ACCELEROMETER
しました。 TYPE_MAGNETIC_FIELD
// Initialize android device sensor capabilities
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
次に、onResume() で、これを使用しました。
// For the system's orientation sensor registered listeners
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
そしてその下で私が使用した:
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// not in use
}
float[] mGravity;
float[] mGeomagnetic;
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
float azimuthInRadians = orientation[0];
float azimuthInDegrees = (float) Math.toDegrees(azimuthInRadians);
if (azimuthInDegrees < 0.0f) {
azimuthInDegrees += 360f;
}
float degree = Math.round(azimuthInDegrees);
directionHeading.setText("Heading: " + degree + " degrees");
// Create rotation animation
RotateAnimation ra = new RotateAnimation(currentDegree, -degree,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// How long the animation will take place
ra.setDuration(210);
// Set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
image.startAnimation(ra);
currentDegree = -degree;
}
}
}
昔のやり方はかなり正確で、まったく揺れませんでした。しかし今、減価償却されていない方法では、大きく揺れます。それはなぜですか、どうすれば手ぶれを修正できますか?