Convert from dp to px
public static float convertDpToPixel(float dp, Context context) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}
or
public static float convertDpToPixel(float dp, Context context) {
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
return px;
}
Convert from dp to px
public static float convertPixelsToDp(float px, Context context) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}
沒有留言:
張貼留言