dev_android
dev_android

Reputation: 8828

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?

Screen-shot

Upvotes: 0

Views: 167

Answers (4)

WillingLearner
WillingLearner

Reputation: 739

Try

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

Upvotes: 0

Stephen Walker
Stephen Walker

Reputation: 574

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)

Upvotes: 0

K_Anas
K_Anas

Reputation: 31466

Use View.getLocationOnScreen().You can only invoke it AFTER layout phase.

Upvotes: 1

Carnal
Carnal

Reputation: 22064

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

Upvotes: 1

Related Questions