我要从 Picasso
切换过来至Glide
在我的应用程序中。我有一个RecyclerView
,其中每一项都是 CustomView
。这个CustomView
添加TextView
或ImageView
动态地基于不同的数据源。
一个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/