Contents

Develop
2016.10.21 11:16

[android] dp, px 서로 변환

조회 수 7608 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
1129 Develop [Javascript][Ajax] 자바스크립트 강의 산출물 file hooni 2019.10.05 3030
1128 Develop [ios] Locale Identifiers file hooni 2018.11.29 3717
1127 Develop [ios] Swift 4 Dictionary 사용하기 file hooni 2018.11.29 4113
1126 Develop [ios] Swift 4 Singleton inheritance hooni 2018.10.31 5924
1125 Develop [ios] APNS, Remote Push 사용자가 수신을 동의했는지 확인하기 hooni 2018.10.19 3333
1124 Develop [ios] APNS, Remote Push 수신 시점에서 앱의 3가지 실행 상태 hooni 2018.10.19 3120
1123 Develop [ios] Swift 4 String, Date, DateFormatter 예제 hooni 2018.10.18 4394
1122 System/OS Mac에서 Node.js 설치하기 file hooni 2018.09.28 5978
1121 System/OS 서버 확장을 위한 두 가지 방법 file hooni 2018.08.29 4960
1120 Develop [ios] Requesting Location Permissions in iOS file hooni 2018.08.18 8183
1119 Develop [PHP] Mac OS에서 PHP 7 설치하기 file hooni 2018.05.11 7392
1118 Develop [php] Connect to Firebase Console in Laravel file hooni 2018.05.09 5732
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 99 Next
/ 99