This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 int value to represent dp equivalent to px value | |
*/ | |
fun convertPxToDp(context: Context, px: Float): Int { | |
val resources = context.resources | |
val metrics = resources.displayMetrics | |
val dp = | |
px / (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 int value to represent px equivalent to dp depending on device density | |
*/ | |
fun convertDpToPx(context: Context?, dp: Float): Int { | |
if (context == null) | |
return 0 | |
val resources = context.resources | |
val metrics = resources.displayMetrics |