简体   繁体   English

将所有Android设备的视图放置在完全相同的位置

[英]Placing a view to exact same place for all android devices

My task is to allow user to upload an image and let user to put a custom view anywhere he clicks. 我的任务是允许用户上传图像,并允许用户在他单击的任何位置放置自定义视图。 (Like taging a pic on facebook) That is simple by getting the touch location and dynamically adding view to a relative layout and setting leftMargin and topMargin. (就像在Facebook上标记照片一样),这很简单,只需获取触摸位置并将其动态添加到相对布局中,然后设置leftMargin和topMargin。

But I want this custom component to get drawn in the 'exact' same position in every android device. 但是我希望这个自定义组件能够在每个android设备的“完全相同”位置绘制。

For example; 例如; Think that you uploaded a picture into your profile and tag someone, other people who look at your pic with different devices should see the tag in the exact same place relative to the pic. 假设您将图片上传到个人资料中并为某人添加了标签,那么其他使用不同设备查看您的图片的人应该在相对于图片的完全相同的位置看到标签。

I know that I have to take account of screen density as well but don't know how to manipulate x, y according to that. 我知道我也必须考虑屏幕密度,但是不知道该如何操作x,y。

Look at the example screenshot: (Blue box is put by user click) 看一下示例屏幕截图:(蓝色框由用户单击放置)

This screenshot is from a test device with screen density 1, another device with density 1.5 shows the blue box in a different position. 该屏幕截图来自屏幕密度为1的测试设备,另一个密度为1.5的设备显示了处于不同位置的蓝色框。

(Images Updated:) (图片已更新:)

密度为1的设备

密度为1.5的设备

This is a test code: 这是一个测试代码:

            icon = new ImageView(getActivity());
            icon.setBackgroundColor(getResources().getColor(R.color.blue));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
            int density = this.getResources().getDisplayMetrics().density;
            params.leftMargin = (int)(posX/density);
            params.topMargin = (int)(posY/density);

Think that you uploaded a picture into your profile and tag someone, other people who look at your pic with different devices should see the tag in the exactly same place. 假设您将图片上传到个人资料中并标记了某人,则其他使用不同设备查看您照片的人应该在完全相同的位置看到该标记。

Then you do not want to be placing "a view to absolute X, Y for all android devices". 然后,您不想放置“所有android设备的绝对X,Y视图”。

For example, suppose we have two devices, A and B. On Device A, your image is a small phone, and the image happens to be rendered at a size of 480 pixels high by 270 pixels wide. 例如,假设我们有两个设备A和B。在设备A上,您的图像是一部小型电话,并且该图像恰好以480像素高×270像素宽的大小渲染。 The user taps at a position that happens to be 80 pixels down from the top and 70 pixels in from the left. 用户点击的位置恰好是从顶部向下80像素,从左侧70像素。

Device B has a screen approximately the size of the country of Turkey. 设备B的屏幕大约等于土耳其国家/地区的大小。 More specifically, your image is rendered at 1600km by 900km on an -mdpi screen density, in a portrait orientation, meaning the image is around 10,078,736,000 pixels high by 5,669,289,000 pixels wide. 更具体地说,您的图像是在-mdpi屏幕密度上以纵向方向以1600 km x 900 km渲染的,这表示图像的高度约为10,078,736,000像素,宽为5,669,289,000像素。

You do not want to be putting your marker 80 pixels down from the top and 70 pixels in from the left. 您不想将标记从顶部向下放置80个像素,从左侧放置70个像素。 That will put the marker almost in the upper-left corner. 这将使标记几乎位于左上角。

Assuming that you arrange to show your image at the same aspect ratio, you will want to save the percentages that the marker is from some corner. 假设您安排以相同的宽高比显示图像,则将需要保存标记从某个角所占的百分比 In this example, 80 pixels from the top is 80/480 or ~16.7% from the top, and 70 pixels in from the left is 70/270 or ~25.9% from the left. 在此示例中,从顶部开始的80个像素为顶部的80/48080/480 %,从左侧开始的70个像素为左侧的70/27070/270 %。 You would then apply the percentages to the size of the image on another device, and so show your marker ~16.7% from the top and ~25.9% from the left on the Turkey-sized screen. 然后,您可以将百分比应用于另一台设备上的图像大小,从而在土耳其尺寸的屏幕上从顶部显示〜16.7%,从左侧显示〜25.9%。 Or on a tablet. 或在平板电脑上。 Or on a larger phone. 或在较大的电话上。 Or on a TV. 或在电视上。 Or on whatever you are choosing to use. 或在您选择使用的任何东西上。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM