/**

 * 픽셀단위를 현재 디스플레이 화면에 비례한 크기로 반환합니다.

 *

 * @param pixel

 *            픽셀

 * @return 변환된 값 (DP)

 */

public static int PixelToDp(Context context, int pixel) {

DisplayMetrics metrics = context.getResources().getDisplayMetrics();

float dp = pixel / (metrics.densityDpi / 160f);

return (int) dp;

}


/**

 * 현재 디스플레이 화면에 비례한 DP단위를 픽셀 크기로 반환합니다.

 *

 * @param DP

 *            픽셀

 * @return 변환된 값 (pixel)

 */

public static int DpToPixel(Context context, int DP) {

float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DP, context.getResources().getDisplayMetrics());

return (int) px;

}



private float dpFromPx(float px)
{
    return px / this.getContext().getResources().getDisplayMetrics().density;
}


private float pxFromDp(float dp)
{
    return dp * this.getContext().getResources().getDisplayMetrics().density;
}

Posted by [czar]
,