Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不用传context的dp 转 px工具 #18

Open
gb18030 opened this issue Jan 8, 2018 · 0 comments
Open

不用传context的dp 转 px工具 #18

gb18030 opened this issue Jan 8, 2018 · 0 comments

Comments

@gb18030
Copy link

gb18030 commented Jan 8, 2018

public class DisplayUtil {

static {
    DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
    density = displayMetrics.density;
    scaledDensity = displayMetrics.scaledDensity;
}
private static float density;
private static float scaledDensity;

public static int px2dip(float px){
    return (int) (px/density + 0.5f);
}

public static float dip2px(float dp){
    return dp*density;
}
public static int px2sp(float px){
    return (int) (px/scaledDensity + 0.5f);
}
public static float sp2px(float sp){
    return  sp*scaledDensity;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant