/*** This method converts dp unit to equivalent pixels, depending on device density.** @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels* @param context Context to get resources and device specific display metrics* @return A float value to represent px equivalent to dp depending on device density*/public static float convertDpToPixel(float dp, Context context){return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);}
/*** This method converts device specific pixels to density independent pixels.** @param px A value in px (pixels) unit. Which we need to convert into db* @param context Context to get resources and device specific display metrics* @return A float value to represent dp equivalent to px value*/public static float convertPixelsToDp(float px, Context context){return px / ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);}
public int convertToPx(int dp) {// Get the screen's density scalefinal float scale = getResources().getDisplayMetrics().density;// Convert the dps to pixels, based on density scalereturn (int) (dp * scale + 0.5f);}
DisplayMetrics displaymetrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);int h = displaymetrics.heightPixels;float d = displaymetrics.density;int heightInPixels=(int) (h/d);
public static int dpToPx(int dp){return (int) (dp * Resources.getSystem().getDisplayMetrics().density);}
public static int pxToDp(int px){return (int) (px / Resources.getSystem().getDisplayMetrics().density);}
public static int dp2px(Resources resource, int dp) {return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp,resource.getDisplayMetrics());}
将像素转换为dp。
public static float px2dp(Resources resource, float px) {return (float)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,px,resource.getDisplayMetrics());}
public static int pxToDp(int px) {return (int) (px / Resources.getSystem().getDisplayMetrics().density);}
public static int dpToPx(int dp) {return (int) (dp * Resources.getSystem().getDisplayMetrics().density);}
fun Int.toPx(context: Context): Int = (this * context.resources.displayMetrics.density).toInt()
fun Int.toDp(context: Context): Int = (this / context.resources.displayMetrics.density).toInt()
val Float.toPx get() = this * Resources.getSystem().displayMetrics.density
val Float.toDp get() = this / Resources.getSystem().displayMetrics.density
val Int.toPx get() = (this * Resources.getSystem().displayMetrics.density).toInt()
val Int.toDp get() = (this / Resources.getSystem().displayMetrics.density).toInt()
private fun toDP(context: Context,value: Int): Int {return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value.toFloat(),context.resources.displayMetrics).toInt()}
// The gesture threshold expressed in dpprivate static final float GESTURE_THRESHOLD_DP = 16.0f;
// Get the screen's density scalefinal float scale = getResources().getDisplayMetrics().density;// Convert the dps to pixels, based on density scalemGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);
// Use mGestureThreshold as a distance in pixels...
静态编程语言示例:
// The gesture threshold expressed in dpprivate const val GESTURE_THRESHOLD_DP = 16.0f...private var mGestureThreshold: Int = 0...override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)
// Get the screen's density scaleval scale: Float = resources.displayMetrics.density// Convert the dps to pixels, based on density scalemGestureThreshold = (GESTURE_THRESHOLD_DP * scale + 0.5f).toInt()
// Use mGestureThreshold as a distance in pixels...}
我从Java示例创建的Java方法效果很好:
/*** Convert dp units to pixel units* https://developer.android.com/training/multiscreen/screendensities#dips-pels** @param dip input size in density independent pixels* https://developer.android.com/training/multiscreen/screendensities#TaskUseDP** @return pixels*/private int dpToPixels(final float dip) {// Get the screen's density scalefinal float scale = this.getResources().getDisplayMetrics().density;// Convert the dps to pixels, based on density scalefinal int pix = Math.round(dip * scale + 0.5f);
if (BuildConfig.DEBUG) {Log.i(DrawingView.TAG, MessageFormat.format("Converted: {0} dip to {1} pixels",dip, pix));}
return pix;}