AndroidUtilCode icon indicating copy to clipboard operation
AndroidUtilCode copied to clipboard

SizeUtils 中尺寸转换 因为使用的是以系统的像素密度来转换, 在像素密度适配方式中存在冲突

Open prczhb opened this issue 3 years ago • 0 comments

  • AndroidUtilCode 的版本:1.31.1
  • 出现 Bug 的设备型号:所有型号
  • 设备的 Android 版本:所有版本

描述 Bug

SizeUtils中 因为尺寸转换是以系统的像素密度来处理的 Resources.getSystem().getDisplayMetrics().density; 在那些通过修改activity像素密度来适配的应用里, 转换会产生严重误差

context.getResources().getDisplayMetrics().density;

是否可以增加一个通过上下文来转换尺寸的方法 如下

相关代码

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

prczhb avatar Oct 17 '22 11:10 prczhb