我在每个单元格中使用带有图像视图的recyclerview。每个图像视图都从网络加载图像,可以是正方形或宽度大于高度或高度大于宽度,即任何尺寸。我将在加载时为每个图像显示一个占位符背景(带有进度条)。但问题是图像的尺寸是未知的,我想将占位符的大小与图像的大小完全相同,例如 9gag 应用程序,其中占位符在加载时与图像的大小完全相同bacground。如何在 android 中实现这一点?我不想使用 wrap-content(在下载图像后产生不和谐的效果)或图像视图的特定高度(裁剪图像)。我正在使用 UIL目前计划改用 Fresco 或 Picassa。
问问题
1765 次
3 回答
1
如果您的占位符只是填充了某种颜色,您可以轻松地模拟具有完全相同大小的颜色可绘制对象。
/**
* The Color Drawable which has a given size.
*/
public class SizableColorDrawable extends ColorDrawable {
int mWidth = -1;
int mHeight = -1;
public SizableColorDrawable(int color, int width, int height) {
super(color);
mWidth = width;
mHeight = height;
}
@Override public int getIntrinsicWidth() {
return mWidth;
}
@Override public int getIntrinsicHeight() {
return mHeight;
}
}
要与毕加索一起使用:
Picasso.with(context).load(url).placeholder(new SizableColorDrawable(color, width, height)).into(imageView);
现在有一些提示ImageView
:
public class DynamicImageView extends ImageView {
@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable drawable = getDrawable();
if (drawable == null) {
super.onMeasure(widthSpecureSpec, heightMeasureSpec);
return;
}
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int reqWidth = drawable.getIntrinsicWidth();
int reqHeight = drawable.getIntrinsicHeight();
// Now you have the measured width and height,
// also the image's width and height,
// calculate your expected width and height;
setMeasuredDimension(targetWidth, targetHeight);
}
}
希望这会对某人有所帮助..
于 2015-09-07T11:52:27.133 回答
0
您可以将图像尺寸与图像 url 一起提供。(我假设您从源获取图像列表,例如 JSON 文件或其他内容。)并根据其尺寸调整ImageView
内部支架的大小,然后启动图像下载过程。RecyclerView
于 2015-08-30T21:30:07.807 回答
0
如果您使用 fresco,则需要从 Web 服务器传递图像的宽度和高度,然后将绘图视图的布局参数设置为该宽度和高度。
像这样:
RelativeLayout.LayoutParams draweeParams =
new RelativeLayout.LayoutParams(desiredImageWidthPixel,
desiredImageHeightPixel);
yourDraweeView.setLayoutParams(draweeParams);
从您从网络服务器传递的图像的宽度和高度,您可以根据需要按比例计算/调整视图大小。其中desiredImageWidthPixel 是要在yourDraweeView 中显示的计算图像宽度,desiredImageHeightPixel 是要在yourDraweeView 中显示的计算图像高度。
别忘了打电话
yourDraweeView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
使 yourDraweeView 与您之前设置的实际参数匹配。希望这可以帮助
于 2015-09-03T16:23:01.187 回答