ImageView里设置centerCrop后导致图片失真,该怎样解决

需要以填满整个ImageView为目的,将原图的中心对准ImageView的中心,等比例放大原图,直到填满ImageView为止(指的是ImageView的宽和高都要填满),原图超过ImageView的部分作裁剪处理,但是在Android中的ImageView里设置centerCrop后导致图片失真,该怎样解决???

下图为ImageView里设置centerCrop后:

可以通过使用Glide库来加载图片并实现您所需的目的。以下是一份示例代码:

Glide.with(context)
    .load(imageUrl)
    .centerCrop()
    .into(new ImageViewTarget<Drawable>(imageView) {
        @Override
        protected void setResource(@Nullable Drawable resource) {
            if (resource instanceof BitmapDrawable) {
                Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                int viewWidth = imageView.getWidth();
                int viewHeight = imageView.getHeight();

                float scaleFactor = Math.max((float) viewWidth / (float) width, (float) viewHeight / (float) height);
                int scaledWidth = (int) (width * scaleFactor);
                int scaledHeight = (int) (height * scaleFactor);

                Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);

                int x = (scaledWidth - viewWidth) / 2;
                int y = (scaledHeight - viewHeight) / 2;

                if (x < 0) {
                    x = 0;
                }

                if (y < 0) {
                    y = 0;
                }

                Bitmap croppedBitmap = Bitmap.createBitmap(scaledBitmap, x, y, viewWidth, viewHeight);

                imageView.setImageBitmap(croppedBitmap);
            } else {
                imageView.setImageDrawable(resource);
            }
        }
    });

这段代码会利用Glide库来加载图片,并在加载完成后自动对图片进行缩放和裁剪,以适配ImageView的尺寸。其中,centerCrop()实现了缩放和裁剪的效果。

需要注意的是,如果ImageView的尺寸为0或未确定,可能会导致图片无法正确加载和裁剪。因此,在使用这段代码前,请确保ImageView已经被正确地测量和布局。

下图为采用该方法设置后的效果:

下面是Kotlin代码:

Glide.with(context)
    .load(imageUrl)
    .centerCrop()
    .into(object : ImageViewTarget<Drawable>(imageView) {
        override fun setResource(resource: Drawable?) {
            if (resource is BitmapDrawable) {
                val bitmap = resource.bitmap
                val width = bitmap.width
                val height = bitmap.height
                val viewWidth = imageView.width
                val viewHeight = imageView.height

                val scaleFactor = max(viewWidth.toFloat() / width.toFloat(), viewHeight.toFloat() / height.toFloat())
                val scaledWidth = (width * scaleFactor).toInt()
                val scaledHeight = (height * scaleFactor).toInt()

                val scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true)

                var x = (scaledWidth - viewWidth) / 2
                var y = (scaledHeight - viewHeight) / 2

                if (x < 0) {
                    x = 0
                }

                if (y < 0) {
                    y = 0
                }

                val croppedBitmap = Bitmap.createBitmap(scaledBitmap, x, y, viewWidth, viewHeight)

                imageView.setImageBitmap(croppedBitmap)
            } else {
                imageView.setImageDrawable(resource)
            }
        }
    })

在kotlin中使用了这段代码后可能会发生如下报错,java.lang.IllegalArgumentException: width and height must be > 0.

这个报错通常是由于传递给Bitmap.createBitmap()方法的宽或高为零或负数所导致的。您可以检查它们的值并进行调整。

在上面的代码中,Bitmap.createBitmap()方法的宽和高参数是viewWidthviewHeight。因此,您需要检查这些值是否大于零。如果没有,您可以尝试使用其他值来替换它们,比如图片宽度和高度:

val croppedBitmap = Bitmap.createBitmap(scaledBitmap, x, y, bitmap.width, bitmap.height)

或者您可以检查是否已正确设置ImageView的宽度和高度,以确保它们大于零:

if (imageView.width <= 0 || imageView.height <= 0) {
    // 宽度或高度无效
    return
}

val viewWidth = imageView.width
val viewHeight = imageView.height

// 下面是原来的代码
val scaledWidth = (width * scaleFactor).toInt()
val scaledHeight = (height * scaleFactor).toInt()

val scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true)

var x = (scaledWidth - viewWidth) / 2
var y = (scaledHeight - viewHeight) / 2

if (x < 0) {
    x = 0
}

if (y < 0) {
    y = 0
}

val croppedBitmap = Bitmap.createBitmap(scaledBitmap, x, y, viewWidth, viewHeight)

imageView.setImageBitmap(croppedBitmap)

PS:(这是一个图片查看器,我下方的图片缩略图采用的是自定义RecyclerView的一个滚动样式,如果您的需求不是我这种场景下的情况,可能在ImageView里设置centerCrop就可以了并不会导致图片失真模糊)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值