Skip to content

Instantly share code, notes, and snippets.

@suclike
Forked from laaptu/DpToPxAndPxToDp
Created April 1, 2019 14:47
Show Gist options
  • Select an option

  • Save suclike/4b068db012c21cbec148cdcd8c5a680d to your computer and use it in GitHub Desktop.

Select an option

Save suclike/4b068db012c21cbec148cdcd8c5a680d to your computer and use it in GitHub Desktop.

Revisions

  1. @laaptu laaptu revised this gist Dec 9, 2013. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions DpToPxAndPxToDp
    Original file line number Diff line number Diff line change
    @@ -14,11 +14,11 @@
    //http://stackoverflow.com/questions/8309354/formula-px-to-dp-dp-to-px-android


    private int convertDpToPx(int dp){
    return Math.round(dp*(getResources().getDisplayMetrics().xdpi/DisplayMetrics.DENSITY_DEFAULT));
    private int convertDpToPx(int dp){
    return Math.round(dp*(getResources().getDisplayMetrics().xdpi/DisplayMetrics.DENSITY_DEFAULT));

    }
    }

    private int convertPxToDp(int px){
    return Math.round(px/(Resources.getSystem().getDisplayMetrics().xdpi/DisplayMetrics.DENSITY_DEFAULT));
    }
    private int convertPxToDp(int px){
    return Math.round(px/(Resources.getSystem().getDisplayMetrics().xdpi/DisplayMetrics.DENSITY_DEFAULT));
    }
  2. @laaptu laaptu revised this gist Dec 9, 2013. 2 changed files with 24 additions and 7 deletions.
    7 changes: 0 additions & 7 deletions DpToPx
    Original file line number Diff line number Diff line change
    @@ -1,7 +0,0 @@
    private int convertDpToPx(int dp){

    return Math.round(dp*(getResources().getDisplayMetrics().xdpi/DisplayMetrics.DENSITY_DEFAULT));
    //or
    return getResources().getDimensionPixelSize(R.dimen.someDimen);

    }
    24 changes: 24 additions & 0 deletions DpToPxAndPxToDp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    public static float convertPixelsToDp(float px){
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return Math.round(dp);
    }

    public static float convertDpToPixel(float dp){
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return Math.round(px);
    }
    //http://stackoverflow.com/questions/4605527/converting-pixels-to-dp
    //The above method results accurate method compared to below methods
    //http://stackoverflow.com/questions/8309354/formula-px-to-dp-dp-to-px-android


    private int convertDpToPx(int dp){
    return Math.round(dp*(getResources().getDisplayMetrics().xdpi/DisplayMetrics.DENSITY_DEFAULT));

    }

    private int convertPxToDp(int px){
    return Math.round(px/(Resources.getSystem().getDisplayMetrics().xdpi/DisplayMetrics.DENSITY_DEFAULT));
    }
  3. @laaptu laaptu created this gist Dec 9, 2013.
    7 changes: 7 additions & 0 deletions DpToPx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    private int convertDpToPx(int dp){

    return Math.round(dp*(getResources().getDisplayMetrics().xdpi/DisplayMetrics.DENSITY_DEFAULT));
    //or
    return getResources().getDimensionPixelSize(R.dimen.someDimen);

    }