计算设备实际高度
//基准宽高
private final static int STANDARD_WIDTH = 1080;
private final static int STANDARD_HEIGHT = 1920 - 48;
//实际设备分辨率
private float displayMetricsWidth;
private float displayMetricsHeight;
private UIUtil(Context context)
{
mContext = context;
WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
if (displayMetricsWidth == 0.0F || displayMetricsHeight == 0.0F) {
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
//获取
int systemBarHight = getSystemBarHeight();
//处理真实宽高得问题
if (displayMetrics.widthPixels > displayMetrics.heightPixels) {//横屏
this.displayMetricsWidth = (float) displayMetrics.heightPixels;
this.displayMetricsHeight = (float) displayMetrics.widthPixels - systemBarHight;
} else {//竖屏
this.displayMetricsWidth = (float) displayMetrics.widthPixels;
this.displayMetricsHeight = (float) displayMetrics.heightPixels - systemBarHight;
}
}
}
获取状态框的高度的两个方法
方法1、通过反射获得
private static String DIME_CLAZZ = "com.android.internal.R$dimen";
private int getSystemBarHeight()
{
return getValue(mContext,DIME_CLAZZ,"system_bar_height",48);
}
private int getValue(Context context, String attrGroupClass,String attrName,int defValue)
{
try{
Class clz = Class.forName(attrGroupClass);
Object obj = clz.newInstance();
Field field = clz.getField(attrName);
int id = Integer.parseInt(field.get(obj).toString());
return context.getResources().getDimensionPixelSize(id);
}catch(Exception e){
return defValue;
}
}
方法2 通过系统给我们提供了一个Resource类
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
计算宽高缩放比例
public float getHorizontalScaleValue()
{
return this.displayMetricsWidth / (float) STANDARD_WIDTH;
}
public float getVerticalScaleValue()
{
return this.displayMetricsHeight / (float) STANDARD_HEIGHT;
}
在自定义布局onMeasure方法中,对子控件的按照缩放比例进行重新计算。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if(!isMeasure) {
isMeasure = true;
int childCount = this.getChildCount();
float scaleX = UIUtil.getInstance(getContext()).getHorizontalScaleValue();
float scaleY = UIUtil.getInstance(getContext()).getVerticalScaleValue();
for (int i = 0; i < childCount; i++) {
View child = this.getChildAt(i);
LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
layoutParams.width = (int) (layoutParams.width * scaleX);
layoutParams.height = (int) (layoutParams.height * scaleY);
layoutParams.leftMargin = (int) (layoutParams.leftMargin * scaleX);
layoutParams.rightMargin = (int) (layoutParams.rightMargin * scaleX);
layoutParams.topMargin = (int) (layoutParams.topMargin * scaleY);
layoutParams.bottomMargin = (int) (layoutParams.bottomMargin * scaleY);
child.setPadding((int)(child.getPaddingLeft()* scaleX),(int)(child.getPaddingTop()* scaleY),(int)(child.getRight()* scaleX),(int)(getPaddingBottom()* scaleY));
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}