Contents

Develop
2016.10.21 11:16

[android] dp, px 서로 변환

Views 7609 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
39 Develop How to Test SMTP AUTH using Telnet hooni 2018.04.05 6223
38 Develop [php] Laravel 4. twitter bootstrap 적용하기 hooni 2018.04.05 4666
37 Develop [js] Click button copy to clipboard hooni 2018.04.05 4400
36 Develop [api] 인스타그램에서 최신 이미지 가져오기 (Using Instagram API) 20 file hooni 2018.04.05 15807
35 Develop [api] 인스타그램 API Access_Token 발급 방법 (Instagram API) 3 file hooni 2018.04.05 11046
34 Develop [php] Connect to Firebase Console in Laravel file hooni 2018.05.09 5732
33 Develop [PHP] MacOS에서 PHP 7 설치하기 file hooni 2018.05.11 7392
32 Develop [ios] Requesting Location Permissions in iOS file hooni 2018.08.18 8194
31 Develop [ios] Swift 4 String, Date, DateFormatter 예제 hooni 2018.10.18 4394
30 Develop [ios] APNS, Remote Push 수신 시점에서 앱의 3가지 실행 상태 hooni 2018.10.19 3120
29 Develop [ios] APNS, Remote Push 사용자가 수신을 동의했는지 확인하기 hooni 2018.10.19 3334
28 Develop [ios] Swift 4 Singleton inheritance hooni 2018.10.31 5924
Board Pagination Prev 1 ... 62 63 64 65 66 67 68 69 70 71 Next
/ 71