Contents

Develop
2016.10.21 11:16

[android] dp, px 서로 변환

Views 7603 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
25 Develop [ios][swift] 초간단 카달로그 앱 (Catalog App) file hooni 2016.06.27 14307
24 Develop [iphone] 아이폰 어플 모음 ㅋㅋ secret hooni 2013.04.23 13707
23 Develop Mac OS 에 Jenkins 설치하기 (Homebrew) 2 file hooni 2017.03.15 13251
22 Develop [ios] 동영상 플레이어 샘플 (for PIP Player) file hooni 2017.03.15 12911
21 Develop [c] 프로세스 검사하기 hooni 2013.04.23 11660
20 Develop [ios] UUID 생성 + Key Chain 연동 file hooni 2016.05.13 10177
19 Develop [ios] Facebook SDK 로그인 설명 file hooni 2017.04.19 9277
18 Develop [ios] Did UIScrollView End Scrolling? hooni 2016.04.19 8319
17 Develop [ios] 동영상 플레이어 샘플 (for Remote Url) file hooni 2017.02.07 8166
16 Develop [ios] Requesting Location Permissions in iOS file hooni 2018.08.18 8163
» Develop [android] dp, px 서로 변환 hooni 2016.10.21 7603
14 Develop [ios] FlckrFeed Example App (Swift) file hooni 2016.11.27 7495
Board Pagination Prev 1 2 3 4 5 6 Next
/ 6