화면 가로, 세로 구하기(pixcel, dpi, inch)
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //화면 가로, 세로 구하기(pixcel, dpi, inch) DecimalFormat df = new DecimalFormat("#,###"); TextView txtPixcel = (TextView)findViewById(R.id.txtPixcel); txtPixcel.setText(df.format(getScreenWidth("pixcel"))+" X "+df.format(getScreenHeight("pixcel"))+"px"); TextView txtDpi = (TextView)findViewById(R.id.txtDpi); txtDpi.setText(df.format(getScreenWidth("dpi"))+" X "+df.format(getScreenHeight("dpi"))+"dpi"); TextView txtInch = (TextView)findViewById(R.id.txtInch); txtInch.setText(df.format(getScreenWidth("inch"))+" X "+df.format(getScreenHeight("inch"))+"inch"); } //화면크기 - 가로 public float getScreenWidth(String type){ //DisplayMetrics 초기화 DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); if("pixcel".equalsIgnoreCase(type)){ return displayMetrics.widthPixels; } else if("dpi".equalsIgnoreCase(type)){ return displayMetrics.xdpi; } else if("inch".equalsIgnoreCase(type)){ return displayMetrics.widthPixels/displayMetrics.xdpi; } return -1; } //화면크기 - 세로 public float getScreenHeight(String type){ //DisplayMetrics 초기화 DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); if("pixcel".equalsIgnoreCase(type)){ return displayMetrics.heightPixels; } else if("dpi".equalsIgnoreCase(type)){ return displayMetrics.ydpi; } else if("inch".equalsIgnoreCase(type)){ return displayMetrics.heightPixels/displayMetrics.ydpi; } return -1; } } | cs |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="16dp" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:paddingBottom="8dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="화면 가로, 세로 구하기(pixcel, dpi, inch)" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:paddingBottom="8dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="화면크기(pixcel) : " android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/txtPixcel" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:paddingBottom="8dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="화면크기(dpi) : " android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/txtDpi" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:paddingBottom="8dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="화면크기(inch) : " android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/txtInch" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> | cs |
결과
'IT > - 프로그래밍' 카테고리의 다른 글
안드로이드 블루투스 활성화, 검색하기 (1) | 2016.10.30 |
---|---|
안드로이드 모델명, 제조사명, IMEI 정보 가져오기 (0) | 2016.08.31 |
반응형웹 목업 웹사이트 예제 (0) | 2016.08.28 |
반응형웹 플렉스 아이템을 비율대로 늘이거나 줄이기 (0) | 2016.08.28 |
반응형웹 플렉스 아이템 배치 순서 바꾸기 (0) | 2016.08.28 |