将问题中的毫米转换为Android中的像素。

huangapple 未分类评论50阅读模式
标题翻译

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

我手动测试过,

  • 在XML中硬编码添加宽度和高度的情况下 将问题中的毫米转换为Android中的像素。
  • 以及您尝试的方法:将问题中的毫米转换为Android中的像素。

我没有看到它们之间的任何区别,也没有完全理解您的问题。

英文翻译

I tested it manually,

  • in case of adding Width and Height hardCoded in XML 将问题中的毫米转换为Android中的像素。
  • and the way you tried: 将问题中的毫米转换为Android中的像素。

neigher I don't see any difference between them nor i didn't understand your question completely

huangapple
  • 本文由 发表于 2020年3月17日 02:55:46
  • 转载请务必保留本文链接:https://java.coder-hub.com/60711717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定