android - 为什么 Glide 加载的图像在 RecyclerView 中会被降采样到低分辨率?

标签 android android-layout android-recyclerview android-glide

我要从 Picasso 切换过来至Glide在我的应用程序中。我有一个RecyclerView ,其中每一项都是 CustomView 。这个CustomView添加TextViewImageView动态地基于不同的数据源。

一个FillWidthImageView用于显示这些图像,它自动填充屏幕宽度。当我使用Picasso时,效果很好。但是,当我使用Glide时加载图片,图片显示在 FillWidthImageView看起来像分辨率极低的马赛克,因为它被大量下采样。<​​/p>

如果我使用普通的ImageView相反,加载图像的大小与占位符相同,并且图像看起来仍然像马赛克。如果没有占位符,图像在屏幕上看起来很小。如果我在空 Activity 中显示图像,它们将以全分辨率和正确的尺寸加载。


添加代码ImageView在我的CustomView (扩展 LinearLayout ),拨入 RecyclerView.onCreateViewHolder()

public void addImageView() {
    ImageView imageView = new FillWidthImageView(getContext());
    imageView.setPadding(0, 0, 0, 10);
    imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    addView(imageView);
}

代码 FillWidthImageView

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Drawable drawable = getDrawable();
    if (drawable != null) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int diw = drawable.getIntrinsicWidth();
        if (diw > 0) {
            int height = width * drawable.getIntrinsicHeight() / diw;
            setMeasuredDimension(width, height);
        } else
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    } else
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

加载图片的代码,在RecyclerView.onBindItemViewHolder()中调用

Glide.with(getContext()).load(url).into(imageView);

最佳答案

谢谢@Hitesh Kr Sahu,但我已经自己解决了这个错误。这是因为 Glide 没有获得我的 CustomView 包装器的正确尺寸。我通过调用 .override(int,int).fitCenter() 应用尺寸解决了这个错误。我将屏幕的宽度和高度传递给它,因此它将缩放以适合屏幕尺寸。

关于android - 为什么 Glide 加载的图像在 RecyclerView 中会被降采样到低分辨率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33844648/

相关文章:

android - android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback 中的奇怪 ArrayIndexOutOfBoundsException

Android,为框架布局配置点击监听器

安卓 :How can i make a list of events day wise. ?

android - 使布局在android中处于非 Activity 状态

android - Activity 恢复时更新 RecyclerView?

android - 对于 feed 来说,RecyclerView 的替代品有哪些

android - Android 上加速度计读数的最大值

android - 洛蒂动画根本不显示

java - 如何更新 recyclerview 项目中的上传进度百分比?

android - 当应用程序中为 false 时,如何默认启用同步选项?