Matrix: 利用Matrix来设置ImageView的宽高,使图片能正常显示

 在Android中加载ImageView一般都不会给ImageView的宽高设置一个确切的值,一般都是直接写成:

<ImageView
        android:id="@+id/iv_test_img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_scale_type_img"
        android:background="@android:color/holo_blue_light"
        />

或者是将width设置成wrap_content, 再结合ImageView的scaleType属性来保证ImageView来正常显示,因为如果把值写死的话,在不同的设备上图片可能会发生变形和扭曲,有时设置了scaleType还好些,要是不设置scaleType,最终显示成什么样子估计连自己都说不清楚。不过设置了scaleType就完美了么?也不尽然,比如要显示的图片是这个样子的:

如果不使用scaleType:

使用scaleType:

SetScaleType(ImageView.ScaleType.CENTER);

又或者:SetScaleType(ImageView.ScaleType.CENTER_CROP);

 

再比如:setScaleType(ImageView.ScaleType.FIT_XY);

 

以上的三种scaleType应该是实际使用时应用的比较广泛的三种scaleType了,还有一些ScaleType.FIT_START,FIT_CENTER,FIT_END就不说了,但是就算设置了scaleType,充其量也就是能保证图片不变形,但是该有的问题还是会有,比如上面的几种scaleType,有的图片上下两侧会有留白,有的根本就显示不全,这里说明一下,ImageView默认的scaleType是FIT_CENTER,有留白是因为比如图片的宽大于高,在把图片放入到ImageView中时,会对图片进行等比例缩放,这样空出的部分自然就是留白了,一般在竖屏的情况下,通常图片的宽度会大于高度,这样在以FIT_CENTER显示的时候,上下都会有留白,但图片内容是可以全部显示的,那有没有什么办法既能全部显示图片内容,又可以使上下没有留白呢?

我的想法是,只要让ImageView的高度与ImageView里显示的图片高度一致就可以了,具体说来就是:

     iv_share_img.setImageBitmap(shareBitmap);
        iv_share_img.post(new Runnable() {
            @Override
            public void run() {
                /*
                 * 获取ImageView中image的宽高
                 * 注意:这里的宽高是Image原始的宽高,不是当前在ImageView中显示的宽高
                 */
                int img_width = iv_share_img.getDrawable().getBounds().width();
                int img_height = iv_share_img.getDrawable().getBounds().height();

                // 获取ImageView中Image的变换矩阵
                float[] emptyMatrix = new float[9];
                iv_share_img.getImageMatrix().getValues(emptyMatrix);

                // 分别从矩阵中获取X和Y的缩放系数
                float scaleX = emptyMatrix[0];
                float scaleY = emptyMatrix[4];

                // 计算Image在屏幕上实际绘制的宽高
                int realWidth = (int) (img_width * scaleX);
                int realHeight = (int) (img_height * scaleY);

                // 将ImageView的高度重新设置为ImageView中实际绘制的Image的高度,这样上下的留白就没有了,图片也可以完整显示了,不必
                // 刻意设置scaleType,使用默认的FIT_CENTER就好。
                ViewGroup.LayoutParams layoutParams = iv_share_img.getLayoutParams();
                layoutParams.height = realHeight;
                iv_share_img.setLayoutParams(layoutParams);
            }
        });

最终显示效果:

这样就基本满足了需求,既能完整显示图片,上下又不会有留白。

 

转载于:https://www.cnblogs.com/yongdaimi/p/11130401.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现这个要求,你可以自定义一个继承自 ImageView 的类,并在其中重写 onMeasure 方法来控制 ImageView 的最大长度和度。同时,在加载图片时,你可以根据图片比例来计算缩放比例,然后使用 Matrix图片进行缩放操作。 下面是一个示例代码: ```java public class CustomImageView extends ImageView { private int mMaxWidth; // 最大宽度 private int mMaxHeight; // 最大度 public CustomImageView(Context context) { super(context); init(); } public CustomImageView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { // 设置默认的最大宽度度 mMaxWidth = Integer.MAX_VALUE; mMaxHeight = Integer.MAX_VALUE; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 获取 ImageView 的测量模式和尺寸 int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); // 计算根据最大宽度度得到的宽度度 int maxWidth = Math.min(widthSize, mMaxWidth); int maxHeight = Math.min(heightSize, mMaxHeight); int scaledWidth = widthSize; int scaledHeight = heightSize; // 根据图片比例计算缩放比例 Drawable drawable = getDrawable(); if (drawable != null) { int imageWidth = drawable.getIntrinsicWidth(); int imageHeight = drawable.getIntrinsicHeight(); float scale = Math.min((float) maxWidth / imageWidth, (float) maxHeight / imageHeight); scaledWidth = (int) (imageWidth * scale); scaledHeight = (int) (imageHeight * scale); } // 根据测量模式设置最终的宽度度 int finalWidth = (widthMode == MeasureSpec.EXACTLY) ? widthSize : scaledWidth; int finalHeight = (heightMode == MeasureSpec.EXACTLY) ? heightSize : scaledHeight; // 设置最终的宽度度 setMeasuredDimension(finalWidth, finalHeight); } public void setMaxSize(int maxWidth, int maxHeight) { mMaxWidth = maxWidth; mMaxHeight = maxHeight; } } ``` 你可以在布局文件中使用这个自定义的 ImageView,然后通过调用 `setMaxSize()` 方法来设置最大宽度度。当加载图片时,ImageView 会按照比例缩放图片来适应最大的宽度度限制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值