0

I have an ImageView with MathParent height and width
In my activity it loads a pic from resource to ImageView. How can i get width and height of the picture inside the ImageView AFTER it has been scaled.
I have not set the android:scaleType in XML
these dimensions i mean!
Screenshot

weston
  • 54,145
  • 21
  • 145
  • 203
Sepehr Behroozi
  • 1,780
  • 1
  • 17
  • 32
  • Why do you want this? As there might be easier solutions than knowing the size of the image. – weston Feb 20 '15 at 08:32
  • possible duplicate: http://stackoverflow.com/questions/3855218/trying-to-get-the-display-size-of-an-image-in-an-imageview – Ispas Claudiu Feb 20 '15 at 08:35
  • @weston I need this cause i want to show user some specific points at fixed dimensions of this pic. as my app is going to run on different screen sizes, i can't use `dp` or `px` in my code. – Sepehr Behroozi Feb 20 '15 at 19:26
  • Yeah, so you don't need the size, map the points with the matrix as seen in my answer. – weston Feb 20 '15 at 19:28

1 Answers1

1

You can do a lot with the matrix the view uses to display the image.

Here I calculate the scale the image is drawn at:

private float scaleOfImageView(ImageView image) {
    float[] coords = new float[]{0, 0, 1, 1};
    Matrix matrix = image.getImageMatrix();
    matrix.mapPoints(coords);
    return coords[2] - coords[0]; //xscale, method assumes maintaining aspect ratio
}

Applying the scale to the image dimensions gives the displayed image size:

private void logImageDisplaySize(ImageView image) {
    Drawable drawable = image.getDrawable();
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    float scale = scaleOfImageView(image);
    float displayedWidth = scale * width;
    float displayedHeight = scale * height;
    Log.d(TAG, String.format("Image drawn at scale: %.2f => %.2f x %.2f",
            scale, displayedWidth, displayedHeight));
}

I suspect you don't really care about the image size, I suspect you want to map touch points back to a coordinate on the image, this answer shows how to do this (also using the image matrix): https://stackoverflow.com/a/9945896/360211

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203