0

I have an ImageView defined as follows in XML. It's inside a custom ViewPager.

<ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:scaleType="fitXY"/>

It is working like a charm but I need to know the actual height of the image, after it has been scaled. I am taking the values in the onMeasure method of the custom ViewPager, like this:

View child = getChildAt(0);

child.measure(widthMeasureSpec, MeasureSpec
    .makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

int h = child.getMeasuredHeight();

The height I am getting is not the height of the image scaled, it's the original size of the image before been scaled.

Thanks.

Ziem
  • 6,579
  • 8
  • 53
  • 86
Olaia
  • 2,172
  • 25
  • 27

1 Answers1

1

You override the onMeasure function to get the image matrix and find the scale values. like so

float[] f = new float[9];
getImageMatrix().getValues(f);
final float scaleX = f[Matrix.MSCALE_X];
final float scaleY = f[Matrix.MSCALE_Y];

// Get the drawable's real width and height
final Drawable d = imageView.getDrawable();
final int origW = d.getIntrinsicWidth();
final int origH = d.getIntrinsicHeight();

// Calculate the actual dimensions
final int actW = Math.round(origW * scaleX);
final int actH = Math.round(origH * scaleY);
log.v("actual dimensions",""+actW+actH);

Source :https://stackoverflow.com/a/15538856/4743812

Community
  • 1
  • 1
Imprfectluck
  • 654
  • 6
  • 26