简体   繁体   中英

Android X,Y determination of an Image

I am using the following xml for Layout.

<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>

And here is my screen-shot. I want to the coordinate of the marked point(x,y of top-left corner of the image) in the screen. How it can be determine?

屏幕截图

int coords[] = {0,0};
yourImageView.getLocationOnScreen(coords);
int x = coords[0];
int y = coords[1];

使用View.getLocationOnScreen() 。您只能在布局阶段后调用它。

I'm not sure ImageView provides a way to get the location of its image on screen.

You can only query the location of the ImageView itself.

Try using ImageView.getImageMatrix() but I'm not sure if it's populated when the image is centered (not scaled)

Try

View v;
int x = v.getLeft();
int y = v.getTop();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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