본문 바로가기

안드로이드 Android

[모바일프로그래밍] 13-3 방향 센서의 개념

activity_main.xml

<?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">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="Orientation Sensor Test"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

MyCustomView.java

package com.example.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class MyCustomView extends View {

    double azimuth = 0.0;
    double pitch = 0.0;
    double roll = 0.0;

    public MyCustomView(Context context) {
        super(context);
    }

    public void setAzimuth(double azimuth) {
        this.azimuth = azimuth;
    }

    public void setPitch(double pitch) {
        this.pitch = pitch;
    }

    public void setRoll(double roll) {
        this.roll = roll;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        myPaint.setColor(Color.RED);
        canvas.save();
        canvas.rotate((float) roll, 400, 400);
        canvas.drawCircle(400, 400, 300, myPaint);
        myPaint.setColor(Color.GRAY);
        canvas.drawRect(380, 120, 420, 680, myPaint);
        myPaint.setTextSize(60);
        myPaint.setColor(Color.BLACK);
        canvas.drawText("N", 390, 50, myPaint);
        canvas.drawText("S", 390, 720, myPaint);
        canvas.restore();
    }
}

여기서 azimuth는 x축의 회전각, pitch는 y축 방향의 회전각, roll은 z축 방향의 회전각이다.

 

MainActivity.java

package com.example.myapplication;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private SensorManager smanager;
    private Sensor myOrientation;
    private TextView mytext;
    private SensorEventListener mylistener;
    MyCustomView myView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myView = new MyCustomView(this);
        setContentView(myView);

        smanager = (SensorManager) getSystemService(SENSOR_SERVICE);
        myOrientation = smanager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        mytext = (TextView) findViewById(R.id.textView1);
        mylistener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent sensorEvent) {
                if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                    myView.setAzimuth(sensorEvent.values[0]);
                    myView.setPitch(sensorEvent.values[1]);
                    myView.setRoll(sensorEvent.values[2]);
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int i) {

            }
        };
    }

    protected void onResume() {
        super.onResume();
        smanager.registerListener(mylistener, myOrientation, SensorManager.SENSOR_DELAY_UI);
    }

    protected void onPause() {
        super.onPause();
        smanager.unregisterListener(mylistener);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
            myView.setAzimuth(sensorEvent.values[0]);
            myView.setPitch(sensorEvent.values[1]);
            myView.setRoll(sensorEvent.values[2]);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

 

AVD 우측의 rotate 버튼을 눌러 보면 나침반이 회전하는 것을 볼 수 있다.

 

안드로이드의 x, y, z축은 OpenGL과 다르다는 것을 주의해야 한다.