Android 获取屏幕宽度,dp,sp,px转换方法

    xiaoxiao2024-11-21  1

    获取屏幕宽度

    WindowManager wm = this.getWindowManager(); //this 指代当前的activity DisplayMetrics dm= new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); int widthPixels = dm.widthPixels; //或者用content也可以获取windowManager对象 WindowManager wm= (WindowManager)context.getSystemService(Context.WindowService);

    dp转化到px

    private int dp2px(int dpValue){ return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue, getResources().getDisplayMetrics()); }

    sp转化到px

    private int sp2px(int spValue){ return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue, getResources().getDisplayMetrics()); }
    转载请注明原文地址: https://ju.6miu.com/read-1293858.html
    最新回复(0)