从服务器正确下载和显示图像
Correctly download and display image from server
现在,在我的应用程序中,每个占位符都有大小,这是在 xml 中设置的,下载图像后它会调整 ImageView
高度视觉效果。
如何获取正在下载的图片大小,应该显示在
ImageView
,要使占位符大小与 Facebook 应用程序实时动态中的大小相同?也许我应该在从服务器下载的 json 文件中包括每个 link 的图像比例,或者 Fresco
、Glide
或 Picasso
有一些技巧来实现这个?
尝试将 ResourceTranscoder
与滑行请求一起使用,如下所述:
Glide
.with(this)
.load(imageUrl)
.asBitmap()
.transcode(new BitmapSizeTranscoder(), Size.class)
.into(new SimpleTarget<Size>() {
@Override public void onResourceReady(Size size, GlideAnimation glideAnimation) {
Log.w("SIZE", String.format(Locale.ENGLISH, "%dx%d", size.width, size.height));
}
});
class Size {
int width;
int height;
}
class BitmapSizeTranscoder implements ResourceTranscoder<Bitmap, Size> {
@Override public Resource<Size> transcode(Resource<Bitmap> toTranscode) {
Bitmap bitmap = toTranscode.get();
Size size = new Size();
size.width = bitmap.getWidth();
size.height = bitmap.getHeight();
return new SimpleResource<>(size);
}
@Override public String getId() {
return getClass().getName();
}
}
如果您想在下载图像之前将占位符调整为实际图像尺寸,您必须以某种方式从您的服务器获取此信息。您可以像您已经建议的那样,在请求中包含图像尺寸/纵横比,然后使用该信息预先适当调整视图大小。
由于您提到的这种可怕的视觉效果,Fresco 不支持 wrap_content 但它支持为 DraweeView
设置宽高比,您可以使用服务器数据设置宽高比。
现在,在我的应用程序中,每个占位符都有大小,这是在 xml 中设置的,下载图像后它会调整 ImageView
高度视觉效果。
如何获取正在下载的图片大小,应该显示在
ImageView
,要使占位符大小与 Facebook 应用程序实时动态中的大小相同?也许我应该在从服务器下载的 json 文件中包括每个 link 的图像比例,或者 Fresco
、Glide
或 Picasso
有一些技巧来实现这个?
尝试将 ResourceTranscoder
与滑行请求一起使用,如下所述:
Glide
.with(this)
.load(imageUrl)
.asBitmap()
.transcode(new BitmapSizeTranscoder(), Size.class)
.into(new SimpleTarget<Size>() {
@Override public void onResourceReady(Size size, GlideAnimation glideAnimation) {
Log.w("SIZE", String.format(Locale.ENGLISH, "%dx%d", size.width, size.height));
}
});
class Size {
int width;
int height;
}
class BitmapSizeTranscoder implements ResourceTranscoder<Bitmap, Size> {
@Override public Resource<Size> transcode(Resource<Bitmap> toTranscode) {
Bitmap bitmap = toTranscode.get();
Size size = new Size();
size.width = bitmap.getWidth();
size.height = bitmap.getHeight();
return new SimpleResource<>(size);
}
@Override public String getId() {
return getClass().getName();
}
}
如果您想在下载图像之前将占位符调整为实际图像尺寸,您必须以某种方式从您的服务器获取此信息。您可以像您已经建议的那样,在请求中包含图像尺寸/纵横比,然后使用该信息预先适当调整视图大小。
由于您提到的这种可怕的视觉效果,Fresco 不支持 wrap_content 但它支持为 DraweeView
设置宽高比,您可以使用服务器数据设置宽高比。