/** * 获取版本号 * * @return 当前应用的版本号 */ public static String getVersion() { try { PackageManager manager = mInstance.getPackageManager(); PackageInfo info = manager.getPackageInfo(mInstance.getPackageName(), 0); String version = info.versionName; return version; } catch (Exception e) { e.printStackTrace(); return ""; } }
/** * 获取当前系统语言 * * @return 当前系统语言 */ public static String getLanguage() { Locale locale = mInstance.getResources().getConfiguration().locale; String language = locale.getDefault().toString(); return language; }
/** * 像素转换工具 * * @author [email protected] */ public class PixelUtil { /** The context. */ private static Context mContext = MyApplication.getInstance(); /** * 获取屏幕宽度 * * @return */ public static int getWindowWidth() { WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); return width; } /** * 获取屏幕高度 * * @return */ public static int getWindowHeight() { WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); int height = wm.getDefaultDisplay().getHeight(); return height; } /** * dp转 px. * * @param value the value * @return the int */ public static int dp2px(float value) { final float scale = mContext.getResources().getDisplayMetrics().densityDpi; return (int) (value * (scale / 160) + 0.5f); } /** * dp转 px. * * @param value the value * @param context the context * @return the int */ public static int dp2px(float value, Context context) { final float scale = context.getResources().getDisplayMetrics().densityDpi; return (int) (value * (scale / 160) + 0.5f); } /** * px转dp. * * @param value the value * @return the int */ public static int px2dp(float value) { final float scale = mContext.getResources().getDisplayMetrics().densityDpi; return (int) ((value * 160) / scale + 0.5f); } /** * px转dp. * * @param value the value * @param context the context * @return the int */ public static int px2dp(float value, Context context) { final float scale = context.getResources().getDisplayMetrics().densityDpi; return (int) ((value * 160) / scale + 0.5f); } /** * sp转px. * * @param value the value * @return the int */ public static int sp2px(float value) { Resources r; if (mContext == null) { r = Resources.getSystem(); } else { r = mContext.getResources(); } float spvalue = value * r.getDisplayMetrics().scaledDensity; return (int) (spvalue + 0.5f); } /** * sp转px. * * @param value the value * @param context the context * @return the int */ public static int sp2px(float value, Context context) { Resources r; if (context == null) { r = Resources.getSystem(); } else { r = context.getResources(); } float spvalue = value * r.getDisplayMetrics().scaledDensity; return (int) (spvalue + 0.5f); } /** * px转sp. * * @param value the value * @return the int */ public static int px2sp(float value) { final float scale = mContext.getResources().getDisplayMetrics().scaledDensity; return (int) (value / scale + 0.5f); } /** * px转sp. * * @param value the value * @param context the context * @return the int */ public static int px2sp(float value, Context context) { final float scale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (value / scale + 0.5f); } }