简体   繁体   English

如何在不同的Android设备上绘制相同物理尺寸的圆?

[英]How to draw a circle of the same physical size on different Android devices?

I have defined a circle radius dimension in a resources file like so: 我已经在资源文件中定义了圆半径尺寸,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="circleRadius">15dp</dimen>
</resources>

And then I draw the circle in a custom View like this: 然后在自定义视图中绘制圆形,如下所示:

Resources res = getResources();
float radius = res.getDimension(R.dimen.circleRadius);

...

canvas.drawCircle(randomX, randomY, radius, paint);

I was under the impression that this would produce a circle of the same physical size on any device, because the units are specified in dp, but it doesn't. 我的印象是,这将在任何设备上产生一个物理尺寸相同的圆,因为单位是在dp中指定的,但事实并非如此。 See the screenshots below. 请参见下面的屏幕截图。

Device 1 (skin=WVGA800, density=240): 设备1 (皮肤= WVGA800,密度= 240):

设备1

Device 2 (skin=QVGA, density=120): 设备2 (皮肤= QVGA,密度= 120):

设备2

Device 3 (skin=1024x768, density=160): 设备3 (皮肤= 1024x768,密度= 160):

设备3

For each device, I ticked the Scale display to real size option when launching, and used the same settings (screen size=3.7 in, monitor dpi=105). 对于每个设备,我在启动时都选中了“将Scale display to real size选项,并使用相同的设置(屏幕尺寸= 3.7英寸,显示器dpi = 105)。 Is this where I've gone wrong? 这是我出问题了吗? Is there something I am not understanding? 有我不明白的东西吗?

You just got wrong impression. 您只是有一个错误的印象。 To compare the way you do, you need to normalize the output. 要比较您的方式,您需要对输出进行标准化。 Grab all the screenshots and scale them to the same density and resolution. 抓取所有屏幕截图并将其缩放到相同的密度和分辨率。 Then compare again. 然后再次比较。

EDIT 编辑

If you want to show 1 inch diameter circle, despite of the hardware you should not be using dp but in or pt unit. 如果要显示1英寸直径的圆圈,则尽管使用了硬件,但您不应使用dp而应使用inpt单位。 See docs: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension 参见文档: http : //developer.android.com/guide/topics/resources/more-resources.html#Dimension

Use: 采用:

//get dots per inch

int dpi = getApplicationContext().getResources().getDisplayMetrics().densityDpi;

//the physical size you want in inches

float x = 0.25f;

//get number of dots for that physical size

final float radius = x*(new Float(dpi));

//now you use radius in drawCircle() method

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中在直径相同的不同屏幕(设备)上绘制圆 - How to draw a circle on different screens(devices) with same diameter in Android 不同尺寸的android设备中的相同图片尺寸 - Same picture size in different size of android devices 在Android中不同物理设备上工作的同一应用之间共享数据库 - share database between same app working on different physical devices in android 如何在Android中绘制带有动画的圆圈,其圆圈大小基于值 - How to draw a circle with animation in android with circle size based on a value 如何在android中的同一点绘制位图和圆 - How to draw bitmap and circle at the same point in android 如何在Android中的不同设备上保持相同的图像大小 - How to keep same image size on different devices in android 在两个不同的设备(不同的屏幕密度)上的同一图像上绘制相同的圆圈 - Draw the same circle on the same image on two different devices (different screen density) 同一文件在不同的Android设备上的大小是否相同? - Is size for the same file identical on different Android devices? Android是否属于同一类别,但设备尺寸不同? - Android same category but different size devices? Android-无法在所有设备上绘制相同大小的正方形 - Android - Can't draw same size square on all devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM