The compass application is one of the important applications that we need in many programs, whether this application is separate or is part of a broader application like some Islamic applications that help to know the direction of the qiblah or times of prayers and the like.
Let’s go directly.
Open a new project in Android Studio from a file list and then name the project as you want, then start with the following procedures:
First to make a compass application, we need two notes:
Accesster Sensor, Magento Motor Sensor
If your device contains these two sensors, the compass app you have with ease, without problems.
We will use the Accessor Sensor acceleration area with Magento Motor March
Sensor and we will get the Rotation Matrix matrix, and then we will get the Orientation direction.
In order not to prolong the explanation and complicate you some things, let’s go in the blade directly.
First: You should get pictures like the following attached pictures and then prepare them as it should and call them properly and place them inside the Drawable folder.
Place these two pictures inside the Drawable folder and name them, respectively, with the following names:
Compass33.png,
Compass4.png
Of course, you can change all these names and also pictures.
Second: We need to make the XML ACTIVITY file
Let it be in the name of activity_main_compass2.xml
Then we write the following code:
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<ImageView
android:id=”@+id/backgnd”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_centerHorizontal=”true”
app:srcCompat=”@android:color/black” />
Third: We go to the main code, which is Activity Main Java and we write the following code.
Under the name of the application inside the acitivity copy and paste the following code:
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivityCompass extends AppCompatActivity implements SensorEventListener{
private ImageView imageView;
private float[] mGravity = new float[3];
private float[] mGeoMagnetic = new float[3];
private float azimuth=0f;
private float currentAzimuth =0f;
private SensorManager mSensorManager;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_compass2);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
imageView = findViewById(R.id.imageViewCompass);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
protected void onResume(){
super.onResume();
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
final float alpha= 0.97f;
synchronized (this) {
if(sensorEvent.sensor.getType()== Sensor.TYPE_ACCELEROMETER) {
mGravity[0]=alpha*mGravity[0]+(1-alpha)*sensorEvent.values[0];
mGravity[1]=alpha*mGravity[1]+(1-alpha)*sensorEvent.values[1];
mGravity[2]=alpha*mGravity[2]+(1-alpha)*sensorEvent.values[2];
}
if(sensorEvent.sensor.getType()== Sensor.TYPE_MAGNETIC_FIELD) {
mGeoMagnetic[0]=alpha*mGeoMagnetic[0]+(1-alpha)*sensorEvent.values[0];
mGeoMagnetic[1]=alpha*mGeoMagnetic[1]+(1-alpha)*sensorEvent.values[1];
mGeoMagnetic[2]=alpha*mGeoMagnetic[2]+(1-alpha)*sensorEvent.values[2];
}
float R[] = new float[9];
float I[] = new float[9];
boolean succsess = SensorManager.getRotationMatrix(R,I,mGravity,mGeoMagnetic);
if(succsess){
float orientation[] = new float[3];
SensorManager.getOrientation(R,orientation);
azimuth = (float)Math.toDegrees(orientation[0]);
azimuth = (azimuth + 360)%360;
Animation anim = new RotateAnimation(-currentAzimuth, -azimuth, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
currentAzimuth = azimuth;
anim.setDuration(500);
anim.setRepeatCount(0);
anim.setFillAfter(true);
imageView.startAnimation(anim);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
Now you can try the project on your mobile phone and enjoy it and if you have notes or improvements on the project, please write it in the comments to benefit everyone.
For more explanation and know the details about what was written in the code, please refer to the basic reference
Reference: https://www.youtube.com/watch?v=IzzGVLnZBfQ