Created
September 17, 2020 02:41
-
-
Save huuphuoc1396/0053ed88e02eee6b7308d1661711500e to your computer and use it in GitHub Desktop.
Convert dp, px, sp among each other (ref: https://bit.ly/35Qqyx4)
This file contains hidden or 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
| public static int dpToPx(float dp, Context context) { | |
| return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); | |
| } |
This file contains hidden or 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
| public static int dpToSp(float dp, Context context) { | |
| return (int) (dpToPx(dp, context) / context.getResources().getDisplayMetrics().scaledDensity); | |
| } |
This file contains hidden or 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
| public static int spToPx(float sp, Context context) { | |
| return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment