/** * 根据屏幕宽高设置传入View的宽高(按照宽度比例等比缩放) * * @param view * @param designedWidthResId UI设计的宽度资源ID(以像素为单位) * @param designedHeightResId UI设计的高度资源ID(以像素为单位) * @return 是否成功设置 */ public static boolean setSize(View view, int designedWidthResId, int designedHeightResId) { float designedWidth = BaseApplication.getAppContext().getResources().getDimension(designedWidthResId); float designedHeight = BaseApplication.getAppContext().getResources().getDimension(designedHeightResId); LayoutParams params = null; if (view instanceof ViewGroup) params = view.getLayoutParams(); if (params == null) params = ((View) (view.getParent())).getLayoutParams(); if (params == null) return false; params.width = (int) (designedWidth * SCALE_RATIO_HORIZONTAL); params.height = (int) (designedHeight * SCALE_RATIO_HORIZONTAL); view.setLayoutParams(params); return true; }
/** * 根据屏幕宽度设置传入TextView的文本大小 * * @param view * @param designedTextSizeResId UI设计的文本大小资源ID(以像素为单位) * @return 是否成功设置 */ public static boolean setTextSize(TextView view, int designedTextSizeResId) { if (view == null) return false; float designedSize = BaseApplication.getAppContext().getResources().getDimension(designedTextSizeResId); float size = designedSize * SCALE_RATIO_HORIZONTAL; view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); return true; }
/** * 通过反射计算状态栏高度 * * @return */ public static int getStatusBarHeight() { if (STATUS_BAR_HEIGHT != 0) return STATUS_BAR_HEIGHT; Class<?> c = null; Object obj = null; Field field = null; int x = 0, statusBarHeight = 0; try { c = Class.forName("com.android.internal.R$dimen"); obj = c.newInstance(); field = c.getField("status_bar_height"); x = Integer.parseInt(field.get(obj).toString()); statusBarHeight = BaseApplication.getAppContext().getResources().getDimensionPixelSize(x); STATUS_BAR_HEIGHT = statusBarHeight; } catch (Exception e1) { e1.printStackTrace(); } return STATUS_BAR_HEIGHT; }
public static void computeWindowRotation() { WindowManager windowManager = (WindowManager) BaseApplication.getAppContext().getSystemService(Context.WINDOW_SERVICE); WINDOW_ROTATION = windowManager.getDefaultDisplay().getRotation(); LogUtil.d(TAG, "computeWindowRotation rotation : " + WINDOW_ROTATION); }
/** * dip转换px * * @param dip * @return */ public static int px2dip(float px) { float f = BaseApplication.getAppContext().getResources().getDisplayMetrics().density; return (int) (px / f); }
/** * dip转换px * * @param dip * @return */ public static int dip2px(float dip) { float f = BaseApplication.getAppContext().getResources().getDisplayMetrics().density; return (int) (dip * f + 0.5F); }
/** * 检查当前屏幕方向是否为横向 * * @return */ public static boolean isLandscape() { return BaseApplication.getAppContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; }
/** 计算屏幕密度 */ public static void computeScreenDensity() { DisplayMetrics dm = new DisplayMetrics(); dm = BaseApplication.getAppContext().getResources().getDisplayMetrics(); if (dm == null) return; SCREEN_DENSITY = dm.density; }
/** * 计算资源文件中定义的尺寸像素值 * * @param resId dimen.xml中定义的资源ID * @return */ public static float computeDimen(int resId) { return BaseApplication.getAppContext().getResources().getDimension(resId); }
/** * 获取屏幕高度(像素) * * @return */ public static int getWindowHeight() { DisplayMetrics dm = new DisplayMetrics(); dm = BaseApplication.getAppContext().getResources().getDisplayMetrics(); if (dm == null) return 0; return dm.heightPixels; }
/** * 获取高度值(像素)(按照高度比例缩放) * * @return */ public static float getVerticalScaledDimen(int heightResId) { return BaseApplication.getAppContext().getResources().getDimension(heightResId) * SCALE_RATIO_VERTICAL; }
/** * 获取高度值(像素)(按照宽度比例缩放) * * @return */ public static float getHorizontalScaledDimen(int heightResId) { return BaseApplication.getAppContext().getResources().getDimension(heightResId) * SCALE_RATIO_HORIZONTAL; }