标题翻译
Convert issue mm to pixel in Android
问题
我尝试创建一个尺寸为30mm x 10mm的线性布局,如下所示。
LinearLayout llBody = view.findViewById(R.id.ll_body);
float width, height;
width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 30, getResources().getDisplayMetrics());
height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 10, getResources().getDisplayMetrics());
ViewGroup.LayoutParams layoutParams = llBody.getLayoutParams();
layoutParams.width = (int) width;
layoutParams.height = (int) height;
llBody.setLayoutParams(layoutParams);
但结果是,我得到了一个稍微放大的线性布局。
我记录了宽度,得到了625像素。
设备的dpi为420。
我尝试手动计算,得到了37.797619048毫米。
我找不到原因和解决方法。
我在Android Studio 3.5.3中使用了Nexus 5X API 29 (x86) 模拟器。
谢谢。
英文翻译
I've tried to make a 30mmx10mm-size linear layout as follows.
LinearLayout llBody = view.findViewById(R.id.ll_body);
float width, height;
width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 30, getResources().getDisplayMetrics());
height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 10, getResources().getDisplayMetrics());
ViewGroup.LayoutParams layoutParams = llBody.getLayoutParams();
layoutParams.width = (int) width;
layoutParams.height = (int) height;
llBody.setLayoutParams(layoutParams);
But as a result, I've got a little magnified LinearLayout.
I logged width and I got 625 pixels.
And the dpi of the device is 420.
I tried to calculate manually, I got 37.797619048 mm.
I can't find the reason and any solutions.
I used Nexus 5X API 29 (x86) Emulator in Android Studio 3.5.3.
Thank you.
答案1
得分: 0
我手动测试过,
我没有看到它们之间的任何区别,也没有完全理解您的问题。
专注分享java语言的经验与见解,让所有开发者获益!
评论