Skip to content

Instantly share code, notes, and snippets.

@unclechen
Last active January 15, 2016 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unclechen/0d6b98d1c371bed11e2b to your computer and use it in GitHub Desktop.
Save unclechen/0d6b98d1c371bed11e2b to your computer and use it in GitHub Desktop.
PxUtils
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