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
913 Develop [js] Javascript로 만든 포트리스 (2010) 5 file hooni 2017.03.03 5472
912 Develop [Android Error] The number of method references in a .dex file cannot exceed 64K hooni 2016.11.10 5480
911 Develop [php][laravel] 라라벨 개발환경 세팅하기 (Mac, Window) 2 file hooni 2017.12.15 5545
910 Develop [js] show/hide 이벤트 감시 (Observing show/hide event) hooni 2021.02.03 5558
909 System/OS How to Install and Use wget on Mac file hooni 2020.09.03 5570
908 Develop [ios] 웹뷰 history.back() ㅋㄷ file hooni 2016.06.27 5667
907 Etc 영어. 불규칙 동사 정리 file hooni 2017.10.04 5681
906 Database [mysql] ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. hooni 2017.12.15 5715
905 Develop [php] Connect to Firebase Console in Laravel file hooni 2018.05.09 5726
904 Develop [ios] UITableView 특정 Row만 Update hooni 2014.04.08 5791
903 Etc 아이폰의 터치스크린 정확도 file hooni 2015.04.01 5860
902 System/OS 무료로 HTTPS 적용하기 (Lets' Encrypt) file hooni 2017.02.16 5905
Board Pagination Prev 1 ... 18 19 20 21 22 23 24 25 26 27 ... 99 Next
/ 99