常用代碼整理:尺寸工具類(SizeUtil)

說明:大部分內容都是參考別的文章,這裏做整理是爲了以後的編程有實用的模板,可以即需即用。

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.WindowManager;

/**
 * 尺寸工具類
 */
public class SizeUtil {

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 獲取當前設備寬度,單位px
     */
    public static int getDeviceWidth(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return manager.getDefaultDisplay().getWidth();
    }

    /**
     * 獲取當前設備高度,單位px
     */
    public static int getDeviceHeight(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return manager.getDefaultDisplay().getHeight();
    }

    /**
     * 獲取狀態欄高度,要在onWindowFocusChanged中調用,在onCreate中獲取高度爲0
     */
    public static int getStatusBarHeight(Activity activity) {
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        return frame.top;
    }
}

1、dip2px 和 px2dip 中 加 0.5f 的原因
看官網是如何解釋的:https://developer.android.com/guide/practices/screens_support.html

Converting dp units to pixel units:The DisplayMetrics.density field specifies the scale factor you must use to convert dp units to pixels, according to the current screen density. On a medium-density screen, DisplayMetrics.density equals 1.0; on a high-density screen it equals 1.5; on an extra-high-density screen, it equals 2.0; and on a low-density screen, it equals 0.75. This figure is the factor by which you should multiply the dp units on order to get the actual pixel count for the current screen. (Then add 0.5f to round the figure up to the nearest whole number, when converting to an integer.) For more information, refer to the DisplayMetrics class.

翻譯過來就是:displaymetrics.density字段指定比例因子必須使用轉換DP單位像素,根據當前屏幕的密度。在中等密度屏幕,displaymetrics.density等於1;在高密度的屏幕,它等於1.5;在超高密度的屏幕,它等於2;在低密度的屏幕,它等於0.75。這個數字的因素,你應該乘DP單位爲當前屏幕獲得實際的像素數。(然後添加0.5f繞圖到最接近的整數,當轉換成一個整數。)的更多信息,請參閱displaymetrics類。


參考文章:
1、https://blog.csdn.net/zhaokaiqiang1992
2、https://blog.csdn.net/u010477502/article/details/52093827

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章