Contents

Develop
2016.10.21 11:16

[android] dp, px 서로 변환

조회 수 3350 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

# Context가 있는 Activity 내부용 함수

//dp를 px로 변환 (dp를 입력받아 px을 리턴)
public float convertDpToPixel(float dp){
    Resources resources = this.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return px;
}

//px을 dp로 변환 (px을 입력받아 dp를 리턴)
public float convertPixelsToDp(float px){
    Resources resources = this.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return dp;
}


# Context를 변수로 받아서 쓰는 유틸 라이브러리용 Static 함수

//dp를 px로 변환 (dp를 입력받아 px을 리턴)
public static float convertDpToPixel(float dp, Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return px;
}

//px을 dp로 변환 (px을 입력받아 dp를 리턴)
public static float convertPixelsToDp(float px, Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return dp;
}



?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
1089 System/OS 무료로 HTTPS 적용하기 (Let's Encrypt) file hooni 2017.10.28 1376
1088 Etc How to completely Uninstall Coda hooni 2017.10.24 1873
1087 Etc 영어. 불규칙 동사 정리 file hooni 2017.10.04 3431
1086 System/OS [linux] iptables 초간단 세팅 스크립트 hooni 2017.09.26 1209
1085 Develop [ios] VIN Scanner (VIN barcode) 스캐너 file hooni 2017.09.16 616
1084 System/OS [mac] Homebrew/rvm/cocoapod setting hooni 2017.07.29 973
1083 Etc 캘리포니아 운전면허 문제 file hooni 2017.07.22 941
1082 Develop [android] 안드로이드 앱 문서 샘플 file hooni 2017.07.11 2045
1081 Develop [coding] 공부해야 하는거 ㅋㅋ secret hooni 2017.06.27 0
1080 Develop [coding] Find all anagrams in a string hooni 2017.06.27 1111
1079 Etc IT감사 기법 시험 file hooni 2017.06.14 1375
1078 Develop [android] SQLiteOpenHelper를 이용한 DBManager hooni 2017.06.14 2012
Board Pagination Prev 1 ... 3 4 5 6 7 8 9 10 11 12 ... 98 Next
/ 98