获取屏幕宽度
WindowManager wm = this
.getWindowManager()
DisplayMetrics dm= new DisplayMetrics()
wm
.getDefaultDisplay()
.getMetrics(dm)
int widthPixels = dm
.widthPixels
//或者用content也可以获取windowManager对象
WindowManager wm= (WindowManager)context
.getSystemService(Context
.WindowService)
dp转化到px
private int dp2px(
int dpValue){
return (
int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,
getResources().getDisplayMetrics());
}
sp转化到px
private int sp2px(
int spValue){
return (
int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,
getResources().getDisplayMetrics());
}
转载请注明原文地址: https://ju.6miu.com/read-1293858.html