Last active
January 15, 2016 01:47
-
-
Save unclechen/0d6b98d1c371bed11e2b to your computer and use it in GitHub Desktop.
PxUtils
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
package com.tiktokview.utils; | |
import android.content.Context; | |
/** | |
* dp,sp和px互转工具类 | |
* Created by noughtchen on 2015/12/2. | |
*/ | |
public class PxUtils { | |
public static int px2dp(Context context, float pxValue) { | |
float scale = context.getResources().getDisplayMetrics().density; | |
return (int) (pxValue / scale + 0.5f); | |
} | |
public static int dp2px(Context context, float dpValue) { | |
float scale = context.getResources().getDisplayMetrics().density; | |
return (int) (dpValue * scale + 0.5f); | |
} | |
public static int px2sp(Context context, float pxValue) { | |
float fontScale = context.getResources().getDisplayMetrics().scaledDensity; | |
return (int) (pxValue / fontScale + 0.5f); | |
} | |
public static int sp2px(Context context, float spValue) { | |
float fontScale = context.getResources().getDisplayMetrics().scaledDensity; | |
return (int) (spValue * fontScale + 0.5f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment