Android tutorial

Android minecraft compass drawing App Example

Android minecraft compass drawing App Example

The compass is , a tool that indicates directions such as North, South and East. They are also called the cardinal directions.

It’s actually a magnetic metal piece that will point towards Earth’s magnetic north compass. Pocket compasses are the most common. The compass looks like a small watch, and when it is held in the hand, it will show you which direction to go north.

What is a compass and its function ?

Earth is a huge magnet with two centres of power, the North Pole and South Pole. As the core of our planet spins, it creates a field. It is this magnetic field that makes up the north and southern poles, and compasses can work.

The needle of a compass, which is usually made of iron, is magnetised and placed on a pin or pivot. It’s then suspended in liquid, most commonly mineral oil or white spirits, so that it can turn freely. When held flat in the hand, the compass needle detects the magnetic field on Earth. It then faces Magnetic North. The device allows the user to determine all other directions.

How is a compass useful?

Maps could be made with the help of the compass. The compass even helped to establish that the Earth was a globe, and not flat.

Compasses are used primarily at sea because there are no landmarks that can help in navigation. The stars, and in particular Polaris (the North Star), were used by sailors to find their way. Even though the sky might be stormy or cloudy, they would not be able tell which direction they were going. Compasses are especially important when at sea.

Compasses are now found in many modern devices, as well as on their own. They are also available in many DIY and craft tools and smartphones.

What is compass rose ?

A rose compass can be found on nautical charts, maps and compasses. Also known as “windrose” or “Rose of the Winds“, it is also found on maps, compasses and nautical charts.

The compass rose was created by Europeans in the 12th Century. It allowed the creation of eight main winds which include the four cardinal direction (North, South East and West) as well as the new intercardinal direction; North-East South-East South-West North-West.

They were a great help to early sailors on their voyages at sea. There are also compasses with points that have 16, 32 or more. This includes directions like North-Northwest, East-Northeast, South-Southwest.

Android Make App compass drawing Example

Let’s Start to create a new project for drawing compass design in android. Start your  android studio and create a new android project select java language. For this App your also need a compass image in this image North direction in the top as of feature image.

Android App Gridle Build 

dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

 

Android Compass App Ui Design XML file Source Code.

lets make UI for your compass App open your activity xml class and make design for the. you need to design a image also to set on image view and add a text view to show the angle. follow the below code.

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff" >

        <TextView
            android:id="@+id/tvHeading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="40dp"
            android:layout_marginTop="20dp"
            android:text="Heading: 0.0" />

        <ImageView
            android:id="@+id/imageViewCompass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/tvHeading"
            android:layout_centerHorizontal="true"
            android:src="@drawable/img_compass" />

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

Android Compass App MainActivity Java File Source code.

 

package com.codeplayon.androidcompass;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener {

    // define the display assembly compass picture
    private ImageView image;

    // record the compass picture angle turned
    private float currentDegree = 0f;

    // device sensor manager
    private SensorManager mSensorManager;

    TextView tvHeading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // our compass image
        image = (ImageView) findViewById(R.id.imageViewCompass);

        // TextView that will tell the user what degree is he heading
        tvHeading = (TextView) findViewById(R.id.tvHeading);

        // initialize your android device sensor capabilities
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // for the system's orientation sensor registered listeners
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onPause() {
        super.onPause();

        // to stop the listener and save battery
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        // get the angle around the z-axis rotated
        float degree = Math.round(event.values[0]);

        tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");

        // create a rotation animation (reverse turn degree degrees)
        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;

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // not in use
    }
}

 

Read More Tutorial