Android/Tip&Tech

android 해상도 및 화면중앙 구하기

행복한 수지아빠 2011. 5. 13. 19:33
반응형
해상도 별로 레이아웃 설정을 변경하거나 또는 늘이거나 줄이지 않고, 중앙에 표시하고자 할 때는 아래와 같이 합니다.  현재 진행 중인 게임 강좌에 맞춰서 예제를 만들어 봤습니다.
01.package app.main;
02. 
03.import ryulib.game.GamePlatform;
04.import android.app.Activity;
05.import android.os.Bundle;
06.import android.util.DisplayMetrics;
07.import android.widget.LinearLayout;
08. 
09.public class Main extends Activity {
10. 
11.private static final int _Width = 300;
12.private static final int _Height = 400;
13. 
14./** Called when the activity is first created. */
15.@Override
16.public void onCreate(Bundle savedInstanceState) {
17.super.onCreate(savedInstanceState);
18. 
19.DisplayMetrics displayMetrics = new DisplayMetrics();
20.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
21.int deviceWidth  = displayMetrics.widthPixels;
22.int deviceHeight = displayMetrics.heightPixels;
23. 
24.LinearLayout layout = new LinearLayout(this);
25.layout.setPadding(
26.(deviceWidth  - _Width)  / 2,
27.(deviceHeight - _Height) / 2,
28.(deviceWidth  - _Width)  / 2,
29.(deviceHeight - _Height) / 2
30.);
31.setContentView(layout);       
32. 
33._GamePlatform = new GamePlatform(this);
34.layout.addView(_GamePlatform);
35. 
36._GamePlatform.AddControl(_Box);
37.}
38. 
39.private GamePlatform _GamePlatform = null;
40.private Box _Box = new Box(null);  
41.}

반응형