|
안드로이드(Android) 스마트폰 가로, 세로 상태 알아내는 방법 |
|
환경 : Eclipse Mars, Android 4.2.2 |
스마트폰에서 가로(portrait), 세로(landscape) 상태인지 알수 있는 방법이 있습니다. 물론 screenOrientation 옵션값에 방향을 고정해 놓으면 방향 전환을 하더라도 값은 바뀌어 있는데 방향은 그대로 여서 의미가 없겠죠. 방향전환을 했을 때 그 값을 알아오는 클래스는 OrientationEventListener 리스너이며 함수는 onOrientationChanged() 입니다.
▼ onOrientationChanged 함수에서 넘어오는 int 값으로 방향을 알수 있는데 0 이면 portrait 이며 90 이상이면 landscape 입니다. 이번 예제에서는 단말기의 방향전환을 시켯을 때 onOrientationChanged 함수로부터 넘어오는 값을 표현해 보겠습니다.
▼ 아래 그림은 세로(portrait) 일때 입니다. TextView 에 표현한 값이 0 이죠. 다음은 회전시켜 보겠습니다. 얼마나 나오는지 보시죠.
▼ 오른쪽에 있는 회전 아이콘을 선택했을 때 입니다. 가로로 바뀌면서 가져온 값이 270 이군요. 이렇게 센서로부터 현재 스마트폰 화면이 가로인지 세로인지 판단할수 있는 방법은 OrientationEventListener 클래스의 onOrientationChanged 함수를 이용하는 것입니다.
▼ 위 화면에서 보았듯이 OrientationEventListener 클래스의 onOrientationChanged 함수를 오버라이드해서 인수로 넘어온 값이 0 이면 가로(portrait) 가 되는 것이고 90 이상이면 세로(landscape) 가 되는 것입니다. 그리고 단말기에서 좌우회전이 가능한지 판단하는 함수가 있습니다. orientEventListener.canDetectOrientation() 로 판단해서 회전 옵션이 중지되어 있다면 orientEventListener.enable() 함수로 가능하게 만듭니다.
import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.OrientationEventListener;
import android.widget.TextView;
import android.widget.Toast;
public class OrientationActivity extends Activity {
TextView txtOrientation;
OrientationEventListener orientEventListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orientation);
txtOrientation = (TextView) findViewById(R.id.textorientation);
orientEventListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int arg0) {
txtOrientation.setText("Orientation: "
+ String.valueOf(arg0));
}
};
if (orientEventListener.canDetectOrientation()) {
Toast.makeText(this, "Can DetectOrientation",
Toast.LENGTH_LONG).show();
orientEventListener.enable();
} else {
Toast.makeText(this, "Can't DetectOrientation",
Toast.LENGTH_LONG).show();
finish();
}
}
}
▼ 메인 activity 에 사용된 레이아웃 xml 입니다. 특별한 것은 없고 센서에서 가져온 값을 표현할 TextView 만 추가 되어 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#3F0099"
android:gravity="center"
android:paddingBottom="@dimen/abc_action_bar_icon_vertical_padding"
android:paddingTop="@dimen/abc_action_bar_icon_vertical_padding"
android:text="Orientation "
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/textorientation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Orientation: " />
</LinearLayout>
'안드로이드 개발' 카테고리의 다른 글
| 안드로이드 폰캡 PhoneGap 설치방법, 기본 앱 만들어서 실행하는 방법 (1) | 2017.11.12 |
|---|---|
| 이클립스(Eclipse) 안드로이드 에뮬레이터 연결해서 실행하는 방법 (3) | 2016.11.17 |
| 안드로이드(Android) AlertDialog.Builder 상속받아 화면 디자인 가능한 AlertDialog 만들기 (0) | 2015.06.04 |
| 안드로이드(Android) 자신이 가지고 있는 스마트폰 안드로이드 버전 알아내는 방법 (0) | 2015.05.30 |
| 안드로이드(Android) Genymotion 지니모션 이클립스에서 프로젝트 연결하는 방법 (1) | 2015.05.27 |
| 안드로이드(Android) Genymotion 지니모션 설치와 이클립스 플러그인 설치해서 연결 (1) | 2015.05.19 |
| [Android] 안드로이드 레이아웃 gravity, layout_gravity 가 적용이 안되는 경우 해결 (0) | 2015.05.15 |
| 안드로이드(Android) PreferenceActivity 에서 addPreferenceFromResource (0) | 2015.04.29 |
