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
39 Develop [c#] mfc 기반의 웹서비스 서버/클라이언트 샘플과 예제 소스 secret hooni 2013.04.23 2073
38 Develop [ios] Thread Loop 내에서 UI 업데이트 방법 hooni 2015.01.03 2069
37 Develop [js] 스크롤 이벤트 막기 hooni 2015.04.14 2068
36 Develop GPL, AGPL, MPL,.. 한눈에 보는 오픈소스SW 라이선스 file hooni 2014.10.14 2062
35 Develop [android] AlertDialog 메시지 창 띄우기 hooni 2015.07.09 2048
34 Develop [c] 이진 탐색 두 가지 코드 (재귀/반복) file hooni 2015.06.26 2022
33 Develop [ios] NSData to NSString (NSString to NSData) hooni 2015.07.21 2022
32 Develop [ios] SBCampanion App 초안 file hooni 2015.09.16 2007
31 Develop [ios] UIWebView에서 로컬에 있는 html 파일 불러오기 hooni 2015.02.10 1998
30 Develop [ios] 새로 만들고 있는 DateMemo file hooni 2016.07.12 1894
29 Develop [ios] NSNotificationCenter 초간단 사용 예~ ㅋㄷ hooni 2015.06.26 1851
28 Develop [ios] 문자열로 함수 실행하기 (eval 함수처럼) hooni 2015.02.10 1833
Board Pagination Prev 1 ... 62 63 64 65 66 67 68 69 70 71 Next
/ 71