Facebook Fresco 使用 wrap_content [英] Facebook Fresco using wrap_content

查看:30
本文介绍了Facebook Fresco 使用 wrap_content的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆我想使用 fresco 加载的可绘制对象,我想为这些图像使用 wrap_content 大小,我怎样才能用 fresco 在 xml 中做到这一点?或者如果 xml 是不可能的,你如何在代码中做到这一点?

I got a bunch of drawables that I want to load using fresco, I want to use wrap_content size for those images, how can I do it in xml with fresco? Or if xml is not possible how do you do it in code?

<com.facebook.drawee.view.SimpleDraweeView
          android:id="@+id/myImage"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          fresco:placeholderImage="@mipmap/myImage"/>

除非我设置了固定大小,否则上面的代码不起作用.

The above code is not working unless I set a fixed size.

推荐答案

我是 Fresco 团队的一员,我是做出不支持 wrap-content 的设计决定的人.文档 中解释了基本原理.但简而言之,问题在于您不能保证图像将立即可用(您可能需要先获取它),这意味着一旦图像到达,视图大小将不得不更改.这在大多数情况下是不可取的,您可能应该重新考虑您的用户界面.

I am part of the Fresco team and I was the one who made the design decision to not support wrap-content. The rationale is explained in the documentation. But in short, the problem is that you can't guarantee that the image will be available immediately (you may need to fetch it first) and that means that the view size would have to change once the image arrives. This is in most cases not desirable and you should probably rethink your UI.

无论如何,如果你真的需要/想要这样做,你可以这样做:

Anyways, if you really really need/want to do that, you can do it like this:

void updateViewSize(@Nullable ImageInfo imageInfo) {
  if (imageInfo != null) {
    draweeView.getLayoutParams().width = imageInfo.getWidth();
    draweeView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
    draweeView.setAspectRatio((float) imageInfo.getWidth() / imageInfo.getHeight());
  }
}

ControllerListener listener = new BaseControllerListener {
    @Override
    public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
      updateViewSize(imageInfo);
    }

    @Override
    public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) {
      updateViewSize(imageInfo);
    }
  };

DraweeController controller = draweeControllerBuilder
  .setUri(uri)
  .setControllerListener(listener)
  .build();
draweeView.setController(controller);

我从头开始写了这段代码,我还没有真正测试过它.但是思路应该很清晰,只要稍微调整一下就行.

I wrote this code from the top of my head, I haven't actually tested it. But the idea should be clear, and it should work with minor adjustments.

这篇关于Facebook Fresco 使用 wrap_content的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆