Reputation: 2210
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.
Upvotes: 0
Views: 249
Reputation: 684
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
Upvotes: 1