Android 如何使ImageView显示在不同显示器上的相同位置?
蓝色的矩形是不可见的区域(为了调试而可见),我点击了一下就发生了一些事情。在IDE上,它们位于正确的位置。 但在genymotion(同一型号)上,它们的位置有点不同——为什么?可能是什么问题-屏幕丹尼斯?Android 如何使ImageView显示在不同显示器上的相同位置?,android,android-imageview,Android,Android Imageview,蓝色的矩形是不可见的区域(为了调试而可见),我点击了一下就发生了一些事情。在IDE上,它们位于正确的位置。 但在genymotion(同一型号)上,它们的位置有点不同——为什么?可能是什么问题-屏幕丹尼斯? 屏幕密度只是一种可能性,很可能是促成因素之一。另一种可能是,您将蓝色矩形放置在与屏幕相关的位置,但您应该将其放置在与图像相关的位置 对于屏幕密度,这会稍微困难一些,因为在2倍密度的屏幕上,100px只会将矩形移动一半,所以与其使用像素,不如考虑使用百分比。这使它更容易在精神上处理 例
屏幕密度只是一种可能性,很可能是促成因素之一。另一种可能是,您将蓝色矩形放置在与屏幕相关的位置,但您应该将其放置在与图像相关的位置 对于屏幕密度,这会稍微困难一些,因为在2倍密度的屏幕上,100px只会将矩形移动一半,所以与其使用像素,不如考虑使用百分比。这使它更容易在精神上处理 例如,“门”矩形看起来从图像宽度的75%变为图像宽度的90%。它大约是图像顶部高度的15%,到图像高度的75% 了解所有这些(psuedocode示例):
- DoorRectangle.left=Image.left+(Image.width*0.75)
- DoorRectangle.right=Image.left+(Image.width*0.90)
- DoorRectangle.top=Image.top+(Image.height*0.15)
- DoorRectangle.bottom=Image.top+(Image.height*0.75)
- 在屏幕布局中添加一个新的
(它也恰好是RelativeLayout
)RelativeLayout
- 调整并重新定位
以居中并使用全屏大小等RelativeLayout
- 将新的
背景图像设置为您的图片RelativeLayout
- 将您的矩形放置在该
内,该位置将始终保持相对位置。这就是RelativeLayout
中的RelativeLayout
的意思;)李>relative
- 作为旁注,“dp”单位应考虑密度
您试图做的是让矩形成为图像的前置部分,但当您调整/移动图像时,矩形无法知道“父”图像的尺寸前置部分(即使它不是真正的父图像,这也是问题的一部分)。通过使两个对象相对于父对象移动,可以始终保持正确的位置 你的xml代码在哪里?@Josef,我现在就编辑它两个蓝色矩形的id是什么?关于“dp单位应该考虑密度”呢?我该如何调整?抱歉,我指的是“边距”,即安卓:layout_marginLeft=“100dp”,安卓:layout_marginTop=“100dp”。您可以在Eclipse布局预览属性面板中编辑/添加这些属性,或者自己将边距添加到矩形的XML中。将很快更新答案以反映这一点。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/main">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/background"
android:src="@drawable/page_2_bkg"
android:layout_alignParentTop="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/door_closed2"
android:src="@drawable/page2_door_closed" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/door_open2"
android:src="@drawable/page2_door_opened"
android:visibility="invisible" />
<ImageView
android:layout_width="80dp"
android:layout_height="300dp"
android:id="@+id/boy"
android:layout_marginLeft="160dp"
android:visibility="visible"
android:src="#ff2b16ff"
android:layout_marginBottom="50dp" />
<ImageView
android:layout_width="150dp"
android:layout_height="300dp"
android:id="@+id/doorBox"
android:visibility="visible"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/background"
android:layout_alignEnd="@+id/background"
android:layout_marginRight="118dp"
android:layout_marginEnd="118dp"
android:src="#ffff3240" />