Android : How to read imageView coordinates if view is centered inside relative layout

on Tuesday, September 30, 2014


I've wrote custom view to be used as cropping rectangle for an image. Now my idea is to position imageView (which will be cropped) in the center of the layout, read coordinates and pass them to the cropping view for setting up initial position and bounds. I tried following but I end up getting 0 for X and Y.



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

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/image"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />


</RelativeLayout>


ACTIVITY:



public class MainActivity extends Activity {

ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

image = (ImageView) findViewById(R.id.image);

int[] posXY = new int[2];
image.getLocationOnScreen(posXY);
int x = posXY[0];
int y = posXY[1];

Log.i("IMAGE x", String.valueOf(x));
Log.i("IMAGE y", String.valueOf(y));


}
}

0 comments:

Post a Comment