0
我正在研究一個簡單的指南針應用程序。我正在使用旋轉動畫來旋轉圖像,但是當我從0旋轉360度時,圖像翻轉回來以0度開始。如何阻止圖像向後旋轉至0度。Android旋轉動畫
float[] mGravity;
float[] mGeomagnetic;
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = lowPass(event.values, mGravity);
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = lowPass(event.values, mGeomagnetic);
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);
//Convert azimuth to degrees
float azimuthInDegrees = (float) (Math.toDegrees(orientation[0])+360)%360;
//Show coord name
String coordNames[] = {"N","NNE", "NE","ENE","E", "ESE","SE","SSE", "S","SSW", "SW","WSW", "W","WNW", "NW","NNW", "N"};
double directionid = Math.round(azimuthInDegrees/22.5);
// no of array contain 360/16=22.5
if (directionid < 0) {
directionid = directionid + 16;
//no. of contains in array
}
String compasLoc = coordNames[(int) directionid];
tvHeading.setText("Heading: " + Float.toString(Math.round(azimuthInDegrees)) + " " + compasLoc);
// create a rotation animation (reverse turn degree degrees)
RotateAnimation ra = new RotateAnimation(
currentDegree,
-azimuthInDegrees,
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 = -azimuthInDegrees;
}
}
}
你能告訴我們你的源代碼嗎? –
您的意思是說,在360°旋轉之後,它在相反的方向上旋轉回到起始位置? – rmtheis
是的,它正確地旋轉回到它的起始位置360 –