100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 屏幕大小 距离单位转换工具类

屏幕大小 距离单位转换工具类

时间:2021-04-19 14:48:45

相关推荐

屏幕大小 距离单位转换工具类

/**

* 屏幕大小、距离单位转换工具类

*/

public class DisplayUtil {

private static DisplayMetrics metrics;

public static void init(Application application) {

if (metrics == null) {

metrics = application.getResources().getDisplayMetrics();

}

}

public static int getScreenHeight() {

checkInit();

return metrics.heightPixels;

}

public static int getScreenWidth() {

checkInit();

return metrics.widthPixels;

}

/**

* px to dip

*

* @param pxValue

* @return

*/

public static int px2dip(float pxValue) {

checkInit();

return (int) (pxValue / metrics.density + 0.5f);

}

/**

* dip to px

*

* @param dipValue

* @return

*/

public static int dip2px(float dipValue) {

checkInit();

return (int) (dipValue * metrics.density + 0.5f);

}

private static void checkInit() {

if (metrics == null) {

throw new NullPointerException("Place init DisplayUtil");

}

}

/**

* px to sp

*

* @param pxValue

* @return

*/

public static int px2sp(float pxValue) {

checkInit();

return (int) (pxValue / metrics.scaledDensity + 0.5f);

}

/**

* sp to px

*

* @param spValue

* @return

*/

public static int sp2px(float spValue) {

checkInit();

return (int) (spValue * metrics.scaledDensity + 0.5f);

}

/**

* @param context

* @param fontSize

* @return

* @category 通过字符大小获取单个字符的Px

*/

public static float getWidthFontSize(Context context, int fontSize) {

TextView textView = new TextView(context);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(

LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

textView.setLayoutParams(layoutParams);

textView.setTextSize(fontSize);

textView.setText("单");

return getcharacterWidth(textView);

}

/**

* @param text

* @return

* @category 获取文本字符串的像素大小

*/

public static float getcharacterWidth(TextView text) {

if (null == text || "".equals(text)) {

return 0f;

}

Paint paint = new Paint();

paint.setTextSize(text.getTextSize() * text.getTextScaleX());

float text_width = paint.measureText(text.getText().toString());

return text_width;

}

public static void getScreenRect(Context ctx_, Rect outrect_) {

Display screenSize = ((WindowManager) ctx_.getSystemService(Context.WINDOW_SERVICE))

.getDefaultDisplay();

outrect_.set(0, 0, screenSize.getWidth(), screenSize.getHeight());

}

/**

* 获取状态栏高度

*

* @param activity

* @return

*/

public static int getStatusHeight(Activity activity) {

int statusHeight = 0;

Rect rect = new Rect();

activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

statusHeight = rect.top;

if (0 == statusHeight) {

Class<?> localClass;

try {

localClass = Class.forName("com.android.internal.R$dimen");

Object object = localClass.newInstance();

int height = Integer.parseInt(localClass.getField("status_bar_height").get(object).toString());

statusHeight = activity.getResources().getDimensionPixelSize(height);

} catch (Exception e) {

e.printStackTrace();

}

}

return statusHeight;

}

/**

* 隐藏键盘

*

* @param activity

*/

public static void hideSoftInput(Activity activity, View view) {

InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

}

public static int getStatusBarHeight(Context context) {

int result = 0;

int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");

if (resourceId > 0) {

result = context.getResources().getDimensionPixelSize(resourceId);

}

return result;

}

/**

* 检测当的网络(WLAN、3G/2G)状态

*

* @param context Context

* @return true 表示网络可用

*/

public static boolean isNetworkAvailable(Context context) {

ConnectivityManager connectivity = (ConnectivityManager) context

.getSystemService(Context.CONNECTIVITY_SERVICE);

if (connectivity != null) {

NetworkInfo info = connectivity.getActiveNetworkInfo();

if (info != null && info.isConnected()) {

// 当前网络是连接的

if (info.getState() == NetworkInfo.State.CONNECTED) {

// 当前所连接的网络可用

return true;

}

}

}

return false;

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。