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

查看:140
本文介绍了如何按比例缩放的任何图像(ImageView的)的宽度等于" match_parent"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能有不同的可绘制(或大或小),我总是跨越宽度(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" />

的问题在于,它增加或减少的宽度,它看起来坏

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

推荐答案

修正。要解决这个问题,你需要做的:

Fixed. To correct this problem you need to do:

  1. 在自定义的ImageView
  2. 设置可绘制到机器人:SRC,从code( imageview.setImageResource()
  1. Custom ImageView
  2. Set drawable to "android:src", from code (imageview.setImageResource())

由ResizableImageView替换ImageView的:

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

解决方法:<一href="http://stackoverflow.com/questions/12982404/i-need-to-size-the-imageview-inside-my-table-row-programatically">I需要大小的ImageView在我的表行编程

这篇关于如何按比例缩放的任何图像(ImageView的)的宽度等于&QUOT; match_parent&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆