[英]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):
Device 2 (skin=QVGA, density=120): 设备2 (皮肤= QVGA,密度= 120):
Device 3 (skin=1024x768, density=160): 设备3 (皮肤= 1024x768,密度= 160):
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
而应使用in
或pt
单位。 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.