在两个不同设备上的同一张图像上绘制相同的圆(不同的屏幕密度)
我在两个具有不同屏幕尺寸和密度的不同设备上有一个图像。
使用画布,我想在设备A图像上画一个圆圈,然后将中心坐标发送到设备B,并在相同位置绘制圆圈,即使同一图像具有不同的大小。
在发送 x 坐标之前,我在设备 A 上执行的操作如下:
float density = getResources().getDisplayMetrics().density;
int width = getWidth();
float inchesLength = width/density;
float scaledXCenter = xCenter / inchesLength;
我对 y 坐标执行相同的操作。
在设备 B 上,我获得相同的参数,并将接收到的坐标乘以英寸长度:
float density = getResources().getDisplayMetrics().density;
int width = getWidth();
float inchesLength = width/density;
float restoredXCenter = scaledXCenter * inchesLength;
我正在 AVD 上对此进行测试。 问题是每个AVD的屏幕密度为1,即使我可以清楚地看到在较小的设备(设备A)上,相同的图像被完全显示,但占用的空间更小!
这种方法在真实设备上工作时应该有效吗?
有没有更好的办法呢?
I have an image on two different devices with different screen size and density.
Using a canvas, I want to draw a circle on device A image, then send the center coordinates to device B, and draw the circle in the same position, even if the same image has a different size.
What I do on device A before sending the the x coordinate is the following:
float density = getResources().getDisplayMetrics().density;
int width = getWidth();
float inchesLength = width/density;
float scaledXCenter = xCenter / inchesLength;
I do the same for the y coordinate.
On device B, I get the same parameters and multiply the received coordinate for the inchesLenght:
float density = getResources().getDisplayMetrics().density;
int width = getWidth();
float inchesLength = width/density;
float restoredXCenter = scaledXCenter * inchesLength;
I am testing this on AVDs.
The problem is that each AVD has a screen density of 1, even if I can clearly see that on the smaller device (device A), the same image is fully displayed, but occupies a smaller space!
Should this approach work when working on real devices?
Is there any better way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到画布的宽度和高度,然后将宽度和高度相除得出比率。该比率应与每个值相乘
这是示例代码
find the width and height of the canvas and by dividing width and height find a ratio. This ratio should be multiplied with each value
Here is the sample code