kushaldsouza
kushaldsouza

Reputation: 708

ImageView getLocationtOnScreen android

I am trying to get the coordinates of the image on the screen. I currently have an ImageView within an activity. I understand that the getLocationOnScreen() method can only be called once the layout has been created, so calling this method within the oncreate function would return [0,0]. But I do not know how to get this method to return the correct values. I have tried overiding various superclass methods, like the onstart method or the onTouchEvent method and it still returns [0,0] to me. The code I currently have is as follows:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    // Some code here after which ..
    image = (ImageView) findViewById(R.id.imageVfi);        
    image.setImageBitmap(imageData.entrySet().iterator().next().getValue());
}

Then I have the onStart method which I have overriden

@Override
public void onStart()
{
    super.onStart();        
    image.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            int[] dim = new int[2];
            image.getLocationOnScreen(dim);
            new AlertDialog.Builder(DisplayPicture.this)
            .setIcon(R.drawable.icon)
            .setTitle("Touch coordinates : " +
                    String.valueOf(dim[0]) + "x" + String.valueOf(dim[1]))
            .setPositiveButton("OK", 
                     new DialogInterface.OnClickListener() {   
                 public void onClick(DialogInterface dialog, int which) 
                 {
                     // TODO Auto-generated method stub
                 }
            }).show();
            // image calculations go here
            return true;

        }
    });
}   

This returns 0X0 to me. Any help is much appreciated.

Upvotes: 1

Views: 1950

Answers (3)

kushaldsouza
kushaldsouza

Reputation: 708

Solution after following advice given above

Solution:

Following the advice provided by Romain Guy below, I was able to get the co-ordinates but adding the following code to the onStart overidden method.

display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
            int orientation = display.getOrientation();
            float rowStart=0, rowEnd=0, columnStart=0, columnEnd=0;
            if(orientation ==0)
            {
                final Matrix matrix = image.getImageMatrix();
                float[] values = new float[9];
                matrix.getValues(values);
                rowStart = values[0];
                columnStart = values[5];
                rowEnd = image.getWidth()-rowStart;
                columnEnd = image.getHeight()-columnStart;
            }else if(orientation == 1)
            {
                final Matrix matrix = image.getImageMatrix();
                float[] values = new float[9];
                matrix.getValues(values);
                rowStart = values[2];
                columnStart = values[3];
                rowEnd = image.getWidth()-rowStart;
                columnEnd = image.getHeight()-columnStart;
            }

Upvotes: 3

Romain Guy
Romain Guy

Reputation: 98501

ImageView does not offer a way to get the location of its image on screen. You can only query the location of the ImageView itself. You can try to use ImageView.getImageMatrix() but I'm not sure we populate it when the image is simply centered (as opposed to scaled, etc.)

Upvotes: 3

siamii
siamii

Reputation: 24124

If it's only the image view that you have in your activity, then it will be positioned by default in the top left corner with coordinates 0x0.

Upvotes: 0

Related Questions