android之ImageView显示超长大图模糊的问题

今天运营部返回了一个app商家端的问题:商品详情页只有一张超长的大图的时候,显示会变模糊。
看了下之前的代码,直接使用ImageView来显示,并未做什么特殊处理,包括ScaleType等都没有设置,所以怀疑是没有根据屏幕自适应的问题。由于使用的是Glide作为图片库加载,因此在.into()方法中加入如下代码进行自适应:

new GlideDrawableImageViewTarget(imageView) {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                        super.onResourceReady(resource, animation);
                        if (null != resource) {
                            float width = resource.getIntrinsicWidth();
                            float height = resource.getIntrinsicHeight();
                            float ivWidth = imageView.getWidth();
                            if (ivWidth == 0) {
                                ivWidth = imageView.getResources().getDisplayMetrics().widthPixels;
                            }
                            int ivHeight = (int) (height / width * ivWidth);
                            ViewGroup.LayoutParams lp = imageView.getLayoutParams();
                            lp.height = ivHeight;
                            imageView.setLayoutParams(lp);
                        }
                    }
                }

试了下结果,显示确实正常。然而,事情并非想象的那么简单,我测试了多个数据,发现显示超长图片的时候,小于2M的图片(分辨率750*8029,我的手机是OPPOR9m)显示都是ok的,然而有张超长大图(超过2M, 分辨率为750*10294),在手机上却没有显示出来(显示异常),我在Android Studio中确找到这样的Log信息:W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (750x10294, max=8192x8192),出现这个问题的原因是开启硬件加速时,高度超出了底层OpenGL绘制的高度,在Canvas绘制时,OpenGL无法绘制出来。从log信息来看,这台手机的最高值是8192。
由于是开启硬件加速的原因,因此比较简单粗暴的在AndroidManifest.xml中关闭了硬件加速,验证是ok的。
其实还有另外两种解决方案,一个是根据texture的max最大值对Bitmap进行压缩;第二个是使用BitmapRegionDecoder。由于压缩有失真的风险,因此以后研究下第二种方式。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android 中,如果使用 `ImageView` 直接加载长,可能会出现模糊的情况。这是因为 `ImageView` 默认只会根据自己的尺寸缩放图片,如果图片比 `ImageView` 大很多,那么缩放比例就会很大,导致图片变得模糊。 为了解决这个问题,可以使用以下两种方法: 1. 使用 `BitmapRegionDecoder` 加载局部区域的图片 可以使用 `BitmapRegionDecoder` 加载图片的局部区域,然后将局部区域的图片显示在 `ImageView` 中,这样就可以避免图片缩放比例过大而导致的模糊问题。 下面是使用 `BitmapRegionDecoder` 加载局部区域图片的示例代码: ```java public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); } public static Bitmap decodeRegionFromBitmap(Bitmap bitmap, Rect rect) { BitmapRegionDecoder decoder = null; Bitmap bitmapRegion = null; try { decoder = BitmapRegionDecoder.newInstance(bitmap, false); bitmapRegion = decoder.decodeRegion(rect, null); } catch (IOException e) { e.printStackTrace(); } finally { if (decoder != null) { decoder.recycle(); } } return bitmapRegion; } // 计算 inSampleSize 的方法 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) { inSampleSize *= 2; } } return inSampleSize; } ``` 然后在 `ImageView` 中加载局部区域的图片: ```java Bitmap bitmap = decodeSampledBitmapFromResource(getResources(), R.drawable.large_image, imageView.getWidth(), imageView.getHeight()); Rect rect = new Rect(0, 0, imageView.getWidth(), imageView.getHeight()); Bitmap regionBitmap = decodeRegionFromBitmap(bitmap, rect); imageView.setImageBitmap(regionBitmap); ``` 2. 使用 `SubsamplingScaleImageView` 加载大 `SubsamplingScaleImageView` 是一个开源库,可以用来加载大并进行缩放、拖拽等操作。使用这个库可以避免图片缩放比例过大而导致的模糊问题。 下面是使用 `SubsamplingScaleImageView` 加载大的示例代码: 1. 在 app 的 build.gradle 文件中添加依赖项: ```groovy implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.10.0' ``` 2. 在布局文件中添加 `SubsamplingScaleImageView`: ```xml <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 3. 在代码中加载图片: ```java SubsamplingScaleImageView imageView = findViewById(R.id.imageView); imageView.setImage(ImageSource.resource(R.drawable.large_image)); ``` 这样就可以使用 `SubsamplingScaleImageView` 加载大并避免模糊问题了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值