Contents

Develop
2016.10.21 11:16

[android] dp, px 서로 변환

Views 3496 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
85 Develop [ios] 동영상 플레이어 샘플 (for Local File) file hooni 2017.02.07 1191
84 Develop [ios][swift] 초간단 To-do Memo file hooni 2016.06.27 1176
83 Develop [ios] Did UIScrollView End Scrolling? hooni 2016.04.19 1164
82 Develop [ios] 상위 ViewController 가져오기 hooni 2015.10.12 1159
81 Develop [ios] StoryBoard(xib) 없이 프로젝트 만들기 file hooni 2015.02.12 1148
80 Develop [ios] 로컬에 있는 html 실행하기 hooni 2015.02.10 1135
79 Develop '2014 모바일 개발 트렌드' 발표자료입니다. file hooni 2014.10.02 1131
78 Develop [c] FSN 온라인 코딩 테스트 (Sorting, Binary Search) file hooni 2015.06.26 1124
77 Develop [ios] Xcode의 디버그 모드에서 콜스택 file hooni 2015.01.03 1103
76 Develop [ubuntu] 우분투 18.04에 PHP5 설치하기 hooni 2020.11.14 1088
75 Develop [js] 스크롤 이벤트 막기 hooni 2015.04.14 1082
74 Develop [ios] APNS, Remote Push 수신 시점에서 앱의 3가지 실행 상태 hooni 2018.10.19 1078
Board Pagination Prev 1 ... 59 60 61 62 63 64 65 66 67 68 ... 71 Next
/ 71