android - 图像在滑动缩放时失真

标签 android android-recyclerview android-glide

我正在使用 glide 将图像加载到 recyclerview 中的 ImageView 中 但是对于低分辨率手机图像会失真,而在浏览器中打开时可以正确看到相同的图像

它在高端手机上正常工作

下面是我的代码

Glide.with(context).load(url).apply(requestOptions).
            into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(Drawable drawable, Transition<? super Drawable> transition) {
                    BitmapDrawable bd = (BitmapDrawable) drawable;
                    int width = bd.getBitmap().getWidth();
                    int height = bd.getBitmap().getHeight();
                    int phoneWidth = PhoneUtils.getScreenWidth(context);
                    float phW = phoneWidth;
                    float wid = width;
                    float factor = phW / wid;
                    double h = factor * height;
                    height = (int) Math.ceil(h);
                    width = phoneWidth;
                    Bitmap bitmap = Bitmap.createScaledBitmap(bd.getBitmap(), width, height, false);
                    imageView.setImageBitmap(bitmap);
                }
            });

书写的文字略有失真

图片缩放有问题吗?

最佳答案

试试这个解决方案

@Override
public void onResourceReady(Drawable drawable, Transition<? super Drawable> transition) {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            int width = drawable.getIntrinsicWidth();
            int height = drawable.getIntrinsicHeight();
            int phoneWidth = PhoneUtils.getScreenWidth(context);
            float phW = phoneWidth;
            float wid = width;
            float factor = phW / wid;
            Matrix matrix = new Matrix();
            matrix.postScale(factor, factor);
            Bitmap bitmap = Bitmap.createBitmap(bd.getBitmap(), 0, 0,
                    width, height, matrix, true);
            imageView.setImageBitmap(bitmap);
 }

使用矩阵调整大小。

关于android - 图像在滑动缩放时失真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48258453/

相关文章:

android - 使用 Glide 加载位图时,谁负责回收它?

android - Glide 库 java.lang.NoClassDefFoundError

android - 使用 setNestedScrollingEnabled(false) 时如何避免阻止滚动本身?

java - android.view.InflateException 二进制 XML 文件行 #0 : Error inflating class <unknown> Caused by: java. lang.reflect.InitationTargetException

android - 如何在Android中获取子字符串?

android网络更改广播接收器并获取网络类型

android - Youtubestandaloneplayer在recycleradapter错误中

android - 在 android 通知中显示带 Glide 的 GIF

android - multiDexEnabled不起作用

java - 使用 ACTION_IMAGE_CAPTURE 从相机拍摄照片后如何跳过或避免 'retake and review' 选项