Created
January 4, 2015 08:25
-
-
Save TakWolf/fed84366d646d33749cd to your computer and use it in GitHub Desktop.
Android px, sp, dp convert util
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.takwolf.android.util; | |
import android.content.Context; | |
public class DisplayUtil { | |
/** | |
* 将px值转换为dip或dp值,保证尺寸大小不变 | |
*/ | |
public static int px2dip(Context context, float pxValue) { | |
float scale = context.getResources().getDisplayMetrics().density; | |
return (int) (pxValue / scale + 0.5f); | |
} | |
/** | |
* 将dip或dp值转换为px值,保证尺寸大小不变 | |
*/ | |
public static int dip2px(Context context, float dipValue) { | |
float scale = context.getResources().getDisplayMetrics().density; | |
return (int) (dipValue * scale + 0.5f); | |
} | |
/** | |
* 将px值转换为sp值,保证文字大小不变 | |
*/ | |
public static int px2sp(Context context, float pxValue) { | |
float fontScale = context.getResources().getDisplayMetrics().scaledDensity; | |
return (int) (pxValue / fontScale + 0.5f); | |
} | |
/** | |
* 将px值转换为sp值,保证文字大小不变 | |
*/ | |
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
no entiendo está todo en japonés.