Android X,Y图像的确定
我使用以下xml进行布局Android X,Y图像的确定,android,android-layout,imageview,coordinates,Android,Android Layout,Imageview,Coordinates,我使用以下xml进行布局 <LinearLayout 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:orientation="vert
<LinearLayout 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:orientation="vertical"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingBottom="20dip"
tools:context=".ZoneSelectionActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="10dip"
android:textSize="35sp"
android:paddingBottom="10dip"
android:text="@string/affected_zone" />
<FrameLayout
android:id="@+id/imageHolder"
android:layout_gravity="center"
android:background="#0B3861"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/subsegment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:contentDescription="subsegment"
android:src="@drawable/sub_segment01" />
</FrameLayout>
</LinearLayout>
这是我的屏幕截图。我想知道屏幕上标记点的坐标(图像左上角的x,y)。如何确定
int coords[] = {0,0};
yourImageView.getLocationOnScreen(coords);
int x = coords[0];
int y = coords[1];
使用
View.getLocationOnScreen()
。您只能在布局阶段后调用它。我不确定ImageView是否提供了一种获取其图像在屏幕上位置的方法
您只能查询ImageView本身的位置
尝试使用ImageView.getImageMatrix(),但我不确定它是否在图像居中(未缩放)时填充
View v;
int x = v.getLeft();
int y = v.getTop();
“布局阶段之后”是什么意思@dev_android在将视图放置在屏幕上之前不会调用此方法,否则将无法工作。它只会给出距离w.r.t.父视图。你问的是x,y坐标,对吗?我的答案是像素中的x,y坐标。如果按它,它将不会给出正确的值。