Last active
May 27, 2018 03:57
-
-
Save kheldiente/97e7365706e1a48bc3febc81008c8162 to your computer and use it in GitHub Desktop.
Calculate pixel to dp and vice versa
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
import android.content.Context | |
object PixelUtil { | |
fun pxToDp(context: Context, px: Float): Float { | |
return px / context.resources.displayMetrics.density | |
} | |
fun dpToPx(context: Context, dp: Float): Float { | |
return dp * context.resources.displayMetrics.density | |
} | |
fun pxToDp(context: Context, px: Int): Float { | |
return px / context.resources.displayMetrics.density | |
} | |
fun dpToPx(context: Context, dp: Int): Float { | |
return dp * context.resources.displayMetrics.density | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment