Contents

Develop
2016.10.21 11:16

[android] dp, px 서로 변환

Views 3493 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

# 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
No. Category Subject Author Date Views
1113 Develop [ios] 비동기 블럭 코드 예제 hooni 2014.11.21 895
1112 Database [mysql] MySQL 백업 및 복구 hooni 2019.11.22 897
1111 Develop [ios] GMT Date와 Local Date 변환하기 hooni 2015.04.07 902
1110 Develop [js] e.stopPropagation() VS e.preventDefault () file hooni 2015.04.14 908
1109 Develop [linux] CentOS Apache 웹서버에 HTTPS 적용 hooni 2015.10.23 908
1108 Algorithm OCB5 Injection 앗싸뵹! ㅋㅋ file hooni 2014.07.01 909
1107 Develop [ios] URL 랜딩 속도(OpenURL 10초 정지되는) 이슈 hooni 2015.02.09 913
1106 Etc 사이버보안실무 수업 메모 hooni 2017.03.30 913
1105 Develop [ios] Thread Loop 내에서 UI 업데이트 방법 hooni 2015.01.03 916
1104 Develop [ios] 문자열로 함수 실행하기 (eval 함수처럼) hooni 2015.02.10 921
1103 System/OS [macos] How to Fix ‘You Shut Down Your Computer Because of a Problem’ file hooni 2022.06.01 922
1102 Develop [io] Apple Watch, Today Extension 앱ID 설정 hooni 2016.04.20 923
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 98 Next
/ 98