minWidth和minHeight对ImageView(Android)没有影响

时间:2016-07-09 03:32:03

标签: java android imageview adjustviewbounds

无论出于何种原因,设置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" />

0 个答案:

没有答案