I created an ImageView with a fixed image ratio. Therefore I need android.widget.ImageView#getMaxWidth
. By converting my code to be Android 10 compatible I can't use this functionality. What would be a workaround to geht the max width/height of the image view?
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int w, h;
if (getDrawable() == null) {
w = h = 0;
} else {
w = getDrawable().getIntrinsicWidth();
h = getDrawable().getIntrinsicHeight();
}
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
boolean resizeWidth = widthSpecMode != MeasureSpec.EXACTLY;
boolean resizeHeight = heightSpecMode != MeasureSpec.EXACTLY;
int wPadding = getPaddingLeft() + getPaddingRight();
int hPadding = getPaddingTop() + getPaddingBottom();
int widthSize = resolveAdjustedSize(w + wPadding, getMaxWidth(), widthMeasureSpec);
int heightSize = resolveAdjustedSize(h + hPadding, getMaxHeight(), heightMeasureSpec);
float actualRatio = (float) (widthSize - wPadding) / (heightSize - hPadding);
if (Math.abs(actualRatio - ratio) > 0.0000001) {
boolean done = false;
if (resizeWidth) {
int newWidth = (int) ((heightSize - hPadding) / ratio) + wPadding;
widthSize = resolveAdjustedSize(newWidth, getMaxWidth(), widthMeasureSpec);
} else if (!done && resizeHeight) {
int newHeight = (int) ((widthSize - wPadding) * ratio) + hPadding;
heightSize = resolveAdjustedSize(newHeight, getMaxHeight(), heightMeasureSpec);
}
}
setMeasuredDimension(widthSize, heightSize);
}