|
< 개발 환경 > < 프로젝트 적용 > |
안드로이드/Android Acitivity 실행시 화면 모드 얻어 오기 ~! (가로 / 세로)
Activity 실행시 화면 모드 얻어 오는 방법 입니다.
package arabiannight.tistroy.com.configuration;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
public class TestConfigurationActivity extends Activity {
private static final String TAG = "TestConfigurationActivity";
private int layout = R.layout.main;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Activity 실행시 화면모드 확인 하기
*/
if(getResources().getConfiguration().orientation == Configuration.
ORIENTATION_LANDSCAPE) {
layout = R.layout.main;
} else if (getResources().getConfiguration().orientation == Configuration.
ORIENTATION_PORTRAIT) {
layout = R.layout.main;
} else {
layout = R.layout.main;
}
setContentView(layout);
}
/**
* Activity 화면 전환시 호출
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
// 세로 전환시 발생
}
else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
// 가로 전환시 발생
}
}
/**
* px을 dip로 반환
* @param con
* @param px
* @return dip
*/
public static int getPxToDip(Context con, int px) {
float density = 0.0f;
density = con.getResources().getDisplayMetrics().density;
Log.d(TAG, "density = " + density);
return (int)(px / density);
}
/**
* 단말기 가로 해상도 구하기
* @param activity
* @return width
*/
public static int getScreenWidth(Activity activity) {
Log.d(TAG, "getScreenWidth() Start");
int width = 0;
width = activity.getWindowManager().getDefaultDisplay().getWidth();
Log.i(TAG, "Screen width = " + width);
return width;
}
/**
* 단말기 세로 해상도 구하기
* @param activity
* @return hight
*/
public static int getScreenHeight(Activity activity) {
Log.d(TAG, "getScreenHeight() Start");
int height = 0;
height = activity.getWindowManager().getDefaultDisplay().getHeight();
Log.i(TAG, "Screen height = " + height);
return height;
}
}
Activity 실행시 화면 모드를 얻어와 화면 모드 별로 처리 할 수 있게 제공해 줍니다.
파일첨부 :
감사합니다.
'Android > Activity' 카테고리의 다른 글
| 안드로이드/Android 필수암기! Activity가 제공하는 필수 암기 메서드() ! (0) | 2013.02.02 |
|---|---|
| 안드로이드/Android Context를 이용해 Activity에 있는 메서드 호출 하기~! (5) | 2013.01.25 |
| 안드로이드/Android 아이스크림 샌드위치(4.0) 이상 버전 화면 전환시 Activity 재생성 방지 속성 ~! (3) | 2013.01.19 |
| 안드로이드/Android Activity onWindowFocusChanged 메서드란? (2) | 2013.01.16 |
| 안드로이드/Android Flag Activity 사용법 및 주의사항 ~! (4) | 2013.01.12 |
TestConfiguration.zip