Java
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_java);
init();
}
private void init() {
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dpHeight2 = displayMetrics.heightPixels / displayMetrics.density;
float dpWidth2 = displayMetrics.widthPixels / displayMetrics.density;
TextView result = findViewById(R.id.tv_result);
StringBuilder builder = new StringBuilder();
builder.append("density: ").append(density).append("\n\n");
builder.append("dpHeight: ").append(dpHeight).append("\n");
builder.append("dpWidth: ").append(dpWidth).append("\n\n");
builder.append("dpHeight2: ").append(dpHeight2).append("\n");
builder.append("dpWidth2: ").append(dpWidth2).append("\n\n");
builder.append("DPI: ").append(getResources().getDisplayMetrics().densityDpi);
result.setText(builder);
}
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}
fun init() {
val display = windowManager.defaultDisplay
val outMetrics = DisplayMetrics()
display.getMetrics(outMetrics)
val density = resources.displayMetrics.density
val dpHeight = outMetrics.heightPixels / density
val dpWidth = outMetrics.widthPixels / density
val displayMetrics = resources.displayMetrics
val dpHeight2 = displayMetrics.heightPixels / displayMetrics.density
val dpWidth2 = displayMetrics.widthPixels / displayMetrics.density
val result = findViewById<TextView>(R.id.tv_result)
val builder = StringBuilder()
builder.append("density: ").append(density).append("\n\n")
builder.append("dpHeight: ").append(dpHeight).append("\n")
builder.append("dpWidth: ").append(dpWidth).append("\n\n")
builder.append("dpHeight2: ").append(dpHeight2).append("\n")
builder.append("dpWidth2: ").append(dpWidth2).append("\n\n")
builder.append("DPI: ").append(resources.displayMetrics.densityDpi)
result.text = builder
findViewById<View>(R.id.btn_switch).setOnClickListener {
val i = Intent(this, MainActivityInJava::class.java)
startActivity(i)
}
}
'소프트웨어 > 안드로이드' 카테고리의 다른 글
[Android Studio] 파일을 잘못 불러오는 경우 (2) | 2019.11.05 |
---|---|
[Android Studio] Javadoc 생성 시 인코딩 에러 (0) | 2019.09.30 |
Android Compass(나침반) (2) | 2019.09.19 |
[Android] 안드로이드 웹 주소 URL로 브라우저 실행하기 (2) | 2019.09.16 |
[Android] AIDL을 구현해보자! (0) | 2019.09.06 |