Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用需要API级别16(当前最小值为10):android.widget.ImageView#getMaxWidth_Android_User Interface_View_Imageview_Compatibility - Fatal编程技术网

调用需要API级别16(当前最小值为10):android.widget.ImageView#getMaxWidth

调用需要API级别16(当前最小值为10):android.widget.ImageView#getMaxWidth,android,user-interface,view,imageview,compatibility,Android,User Interface,View,Imageview,Compatibility,我创建了一个具有固定图像比率的ImageView。因此,我需要android.widget.ImageView#getMaxWidth。通过将我的代码转换为Android 10兼容,我无法使用此功能。如何解决图像视图的最大宽度/高度问题 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int w, h; if (getDrawable() == null) {

我创建了一个具有固定图像比率的ImageView。因此,我需要
android.widget.ImageView#getMaxWidth
。通过将我的代码转换为Android 10兼容,我无法使用此功能。如何解决图像视图的最大宽度/高度问题

@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);
}

好的,这就是我所做的。为了跟踪图像的最大宽度/高度,我覆盖了属性的setter。只有setter用于写入这些属性

private int mMaxWidth = Integer.MAX_VALUE;
private int mMaxHeight = Integer.MAX_VALUE;

@Override
public void setMaxHeight(int maxHeight) {
    mMaxHeight = maxHeight;
    super.setMaxHeight(maxHeight);
}

@Override
public void setMaxWidth(int maxWidth) {
    mMaxWidth = maxWidth;
    super.setMaxWidth(maxWidth);
}