6

无论出于何种原因,设置 minHeight 和 maxHeight 对 ImageView 组件内的图像尺寸没有影响。在某些情况下,我最终会得到一个非常小的图像。我认为这是因为我将宽度和高度设置为“wrap_content”并将“adjustViewBounds”设置为 true。

如果我将“adjustViewBounds”设置为 false,图像会扩展为我为 minWidth 和/或 minHeight 指定的任何内容。但是,在这种情况下,图像会变形,因为它的宽度和高度没有按比例缩放。我也想避免这种情况。

我最终编写了大量代码(如下)来覆盖 ImageView 的 onMeasure() 方法。这真的有必要吗?

public class FlexibleImageView extends ImageView {

    private static final String LOG_TAG = "FlexibleImageView";

    public FlexibleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
            boolean adjustViewBounds = getAdjustViewBounds();
            int minWidth = Math.max(getMinimumWidth(), getSuggestedMinimumWidth());
            int minHeight = Math.max(getMinimumHeight(), getSuggestedMinimumHeight());
            int maxWidth = getMaxWidth();
            int maxHeight = getMaxHeight();
            int widthSpec = getMeasuredWidth();
            int heightSpec = getMeasuredHeight();

            Drawable d = getDrawable();
            if (d != null && adjustViewBounds) {

                // Scale UP if the size is too small
                float scale, scaleW = 1.0f, scaleH = 1.0f;
                if (widthSpec < minWidth)
                    scaleW = (float) minWidth / (float) widthSpec;
                if (heightSpec < minHeight)
                    scaleH = (float) minHeight / (float) heightSpec;
                scale = (scaleW > scaleH) ? scaleW : scaleH;

                if (scale > 1.0) {

                    int targetW = (int) Math.ceil(widthSpec * scale);
                    int targetH = (int) Math.ceil(heightSpec * scale);

                    Log.d(LOG_TAG, "Measured dimension (" + widthSpec + "x" + heightSpec
                            + ") too small.  Resizing to " + targetW + "x" + targetH);

                    // Make sure targetW, targetH stays within the bounds of maxW, maxH
                    if(targetW > maxWidth) {
                        targetH = targetH * maxWidth / targetW;
                        targetW = maxWidth;

                        Log.d(LOG_TAG, "Capping width to " + maxWidth);
                    }
                    if(targetH > maxHeight) {
                        targetW = targetW * maxHeight / targetH;
                        targetH = maxHeight;

                        Log.d(LOG_TAG, "Capping height to " + maxHeight);
                    }
                    setMeasuredDimension(targetW, targetH);
                }
            }
        }
    }
}

和 XML 部分:

<cards.myb.ui.FlexibleImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageCard"
    android:contentDescription="@string/card_image"
    android:adjustViewBounds="true"
    android:minHeight="@dimen/card_editor_img_min_dimen"
    android:scaleType="fitXY"
    android:src="@drawable/fb_logo" />
4

0 回答 0