i have a custom compass with 3 image Views first compass direction always to north second always on direction i specify 3 proven, so i want it to rotate smoothly but when i close from reach 0 or 360 degree its like wrap round like it restart or something like that.
-i want my compass rotate smoothly without wrapping round when i close from reach some degrees.
``public class GeoLocationActivity extends AppCompatActivity implements SensorEventListener {
private ImageView compass, satDirect;
private TextView satName, degree, azmuthT, elevationT, skewT;
private Sensor magSensor, accSensor;
private SensorManager sensorManager;
private float[] lastAcc;
private float[] lastMag;
private float[] orientation;
private float[] rotation;
private boolean isLastAccCopied;
private boolean isLastMagCopied;
private long lastUpdateTime1, lastUpdateTime2;
private float currentDegree1, currentDegree2;
private float satAzmuth, dishElevation, lnbSkew;
private float smoothAzimuth1 = 0f, smoothAzimuth2 = 0f;
private static final float SMOOTHING_FACTOR = 0.1f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setContentView(R.layout.activity_geo_location);
init();
}
private void init() {
Bundle b = getIntent().getExtras();
satAzmuth = b.getFloat("azmuth", 0);
dishElevation = b.getFloat("elev", 0);
lnbSkew = b.getFloat("lnb", 0);
compass = findViewById(R.id.compass);
satDirect = findViewById(R.id.satD);
satName = findViewById(R.id.satName);
degree = findViewById(R.id.degree);
azmuthT = findViewById(R.id.azmuth);
azmuthT.setText(satAzmuth + "");
elevationT = findViewById(R.id.elevation);
elevationT.setText(dishElevation + "");
skewT = findViewById(R.id.lnb_skew);
skewT.setText(lnbSkew + "");
satName.setText(b.getString("sat", ""));
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
lastAcc = new float[3];
lastMag = new float[3];
orientation = new float[3];
rotation = new float[9];
isLastAccCopied = false;
isLastMagCopied = false;
lastUpdateTime1 = 0;
currentDegree1 = 0f;
lastUpdateTime2 = 0;
currentDegree2 = 0f;
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
setNorthDirection(sensorEvent);
setSatDirect(sensorEvent);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
private void setNorthDirection(SensorEvent sensorEvent) {
if (sensorEvent.sensor == accSensor) {
lastAcc = sensorEvent.values;
isLastAccCopied = true;
} else if (sensorEvent.sensor == magSensor) {
lastMag = sensorEvent.values;
isLastMagCopied = true;
}
if (isLastAccCopied || isLastMagCopied && System.currentTimeMillis() - lastUpdateTime2 > 250) {
SensorManager.getRotationMatrix(rotation, null, lastAcc, lastMag);
SensorManager.getOrientation(rotation, orientation);
float azimuthInRadiant = orientation[0];
float azimuthInDegree = (float) Math.toDegrees(azimuthInRadiant);
smoothAzimuth2 = smoothAzimuth2 * (1 - SMOOTHING_FACTOR) + azimuthInDegree * SMOOTHING_FACTOR;
RotateAnimation rotateAnimation = new RotateAnimation(currentDegree2, -smoothAzimuth2, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(0);
rotateAnimation.setFillAfter(true);
compass.setAnimation(rotateAnimation);
currentDegree2 = -smoothAzimuth2;
lastUpdateTime2 = System.currentTimeMillis();
}
}
private void setSatDirect (SensorEvent sensorEvent) {
if (sensorEvent.sensor == accSensor) {
lastAcc = sensorEvent.values;
isLastAccCopied = true;
} else if (sensorEvent.sensor == magSensor) {
lastMag = sensorEvent.values;
isLastMagCopied = true;
}
if (isLastAccCopied || isLastMagCopied && System.currentTimeMillis() - lastUpdateTime1 > 250) {
SensorManager.getRotationMatrix(rotation, null, lastAcc, lastMag);
SensorManager.getOrientation(rotation, orientation);
float azimuthInRadiant = orientation[0];
float azimuthInDegree = (float) Math.toDegrees(azimuthInRadiant);
float azimuthToNilesar = azimuthInDegree - satAzmuth;
if (azimuthToNilesar < 0f) {
azimuthToNilesar += 360f;
}
smoothAzimuth1 = smoothAzimuth1 * (1 - SMOOTHING_FACTOR) + azimuthToNilesar * SMOOTHING_FACTOR;
RotateAnimation rotateAnimation = new RotateAnimation(currentDegree1, -smoothAzimuth1, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
rotateAnimation.setFillAfter(true);
satDirect.setAnimation(rotateAnimation);
currentDegree1 = -smoothAzimuth1;
int intDRG = (int) currentDegree1;
degree.setText(-intDRG + "°");
lastUpdateTime1 = System.currentTimeMillis();
}
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, magSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this, accSensor);
sensorManager.unregisterListener(this, accSensor);
}
public void backbtn(View view){
onBackPressed();
}
}