如何按比例缩放任何宽度等于 "match_parent "的图像(imageview)?[英] How proportionally scale any image (of imageview) with width equal to "match_parent"?

本文是小编为大家收集整理的关于如何按比例缩放任何宽度等于 "match_parent "的图像(imageview)?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我可能有不同的可滑动刀(大或小),我总是跨越宽度(match_parent)并按比例增加或减少高度.保持比率.

如何实现?

我试过:

<ImageView
            android:id="@+id/iv_TEST"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/test" />

问题是它会增加或减少宽度,看起来很糟糕.

在此处输入图像描述

推荐答案

固定.要更正此问题,您需要执行以下操作:

  1. 自定义ImageView
  2. 将可绘制到"android:src",从代码(imageview.setImageResource())

替换ResizableImageView的ImageView:

<"the name of your package".ResizableImageView
            android:id="@+id/iv_test"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

resizableimageview.class

    public class ResizableImageView extends ImageView {
        public ResizableImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public ResizableImageView(Context context) {
            super(context);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            Drawable d = getDrawable();
            if (d == null) {
                super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
                return;
            }

            int imageHeight = d.getIntrinsicHeight();
            int imageWidth = d.getIntrinsicWidth();

            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);

            float imageRatio = 0.0F;
            if (imageHeight > 0) {
                imageRatio = imageWidth / imageHeight;
            }
            float sizeRatio = 0.0F;
            if (heightSize > 0) {
                sizeRatio = widthSize / heightSize;
            }

            int width;
            int height;
            if (imageRatio >= sizeRatio) {
                // set width to maximum allowed
                width = widthSize;
                // scale height
                height = width * imageHeight / imageWidth;
            } else {
                // set height to maximum allowed
                height = heightSize;
                // scale width
                width = height * imageWidth / imageHeight;
            }

            setMeasuredDimension(width, height);
        }
    }

解决方案:我需要大小ImageView以表规划方式依据地

本文地址:https://www.itbaoku.cn/post/102554.html

问题描述

I may have different drawables (large or small) and I always spans the width (match_parent) and increase or decrease the height proportionally. Maintaining the ratio.

How is this possible?

I tried with:

<ImageView
            android:id="@+id/iv_TEST"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/test" />

The problem is that it increases or decreases the width and it looks bad.

enter image description here

推荐答案

Fixed. To correct this problem you need to do:

  1. Custom ImageView
  2. Set drawable to "android:src", from code (imageview.setImageResource())

Replace ImageView by ResizableImageView:

<"the name of your package".ResizableImageView
            android:id="@+id/iv_test"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

ResizableImageView.class

    public class ResizableImageView extends ImageView {
        public ResizableImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public ResizableImageView(Context context) {
            super(context);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            Drawable d = getDrawable();
            if (d == null) {
                super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
                return;
            }

            int imageHeight = d.getIntrinsicHeight();
            int imageWidth = d.getIntrinsicWidth();

            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);

            float imageRatio = 0.0F;
            if (imageHeight > 0) {
                imageRatio = imageWidth / imageHeight;
            }
            float sizeRatio = 0.0F;
            if (heightSize > 0) {
                sizeRatio = widthSize / heightSize;
            }

            int width;
            int height;
            if (imageRatio >= sizeRatio) {
                // set width to maximum allowed
                width = widthSize;
                // scale height
                height = width * imageHeight / imageWidth;
            } else {
                // set height to maximum allowed
                height = heightSize;
                // scale width
                width = height * imageWidth / imageHeight;
            }

            setMeasuredDimension(width, height);
        }
    }

Solution: I need to size the imageView inside my table row programatically