如何在xamarin.android中将dp转换为px? [英] How to convert dp to px in xamarin.android?

查看:338
本文介绍了如何在xamarin.android中将dp转换为px?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 xamarin.android 中的 C#代码中将dp转换为px,但是我只能找到在Android中的 java代码 在xamarin中存在一些问题的工作室.我尝试使用类似的方法,例如使用Resources而不是getResources(),我可以解决一些小问题,但是仍然存在一些问题,我无法为它们找到任何等效方法.这是原始代码,我的代码以及我在xamarin中的代码问题:

I want to convert dp to px in my C# code in xamarin.android, but all I could find were java codes in android studio that have some problems in xamarin. I tried to use equivalent like using Resources instead of getResources() and I could solve some little problems, but there are some problems yet that I couldn't find any equivalent for them. here are original codes, my codes, and my codes problems in xamarin:

(可从在LayoutParams上以编程方式将高度设置为密度-独立像素)

Java代码

int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, < HEIGHT >, getResources().getDisplayMetrics());

C#代码

int height = (int)TypedValue.ApplyDimension(TypedValue.COMPLEX_UNIT_DIP, < HEIGHT >, Resources.DisplayMetrics);

问题:

  • "TypedValue"不包含"COMPLEX_UNIT_DIP"的定义

  • 'TypedValue' does not contain a definition for 'COMPLEX_UNIT_DIP'

无效的表达术语< (>的相同错误)

Invalid expression term < (The same error for >)

名称"HEIGHT"在当前上下文中不存在

The name 'HEIGHT' does not exist in the current context

(可从公式px转换为dp,dp转换为px android )

Java代码

DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));

C#代码

DisplayMetrics displayMetrics = Application.Context.Resources.DisplayMetrics;
int pixel = Math.Round(dp * (displayMetrics.Xdpi / DisplayMetrics.DensityDefault));

问题

  • 运算符'/'不能应用于类型为'float'和'DisplayMetricsDensity'的操作数

现在我实际上有两个问题.哪个代码更合适?在xamarin.android中,它们的等效代码是什么?

Now I have actually two questions. Which code is more proper? What's equivalent code for them in xamarin.android?

提前谢谢.

推荐答案

第一个代码" 的解决方案:

Xamarin倾向于将常量移动到它们自己的枚举中.可以在ComplexUnitType枚举上找到COMPLEX_UNIT_DIP.同样,您不能拥有<高度>在您的代码中,您实际上需要传递DIP以获得等效的像素值.在下面的示例中,我将以100度倾斜获取像素.

Xamarin tends to move constants into their own enums. COMPLEX_UNIT_DIP can be found on ComplexUnitType enum. Also you cannot have < HEIGHT > in your code you actually need to pass in dips to get the equivalent pixel value. in the example below I am getting pixels in 100 dips.

var dp = 100;
int pixel = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, dp, Context.Resources.DisplayMetrics);

第二个代码" 的解决方案:

您需要将"DisplayMetrics.DensityDefault"显式转换为浮点型,并将整轮转换为int型:

You need to explicitly cast 'DisplayMetrics.DensityDefault' to a float and the entire round to an int:

int pixel = (int)System.Math.Round(dp * (displayMetrics.Xdpi / (float)DisplayMetrics.DensityDefault));

我更喜欢第一种方法,因为第二种代码专门用于沿"x维度"进行计算: 根据Android文档和 Xamarin.Android 文档的Xdpi属性是

I prefer the first approach as the second code is specifically for calculating along the "x dimension": Per Android docs and Xamarin.Android docs, the Xdpi property is

"X尺寸屏幕上每英寸的确切物理像素."

"The exact physical pixels per inch of the screen in the X dimension."

这篇关于如何在xamarin.android中将dp转换为px?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆