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

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

问题描述

我想要使​​用壁画加载一堆可绘画,我想为这些图像使用 wrap_content 尺寸,我怎么能在xml中用壁画呢?或者如果xml不可能在代码中怎么做?

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

上述代码不工作,除非我设置了一个固定的大小。

解决方案

我是Fresco团队的一部分,我是设计决定不支持包装内容的人。原理解释在文档中。但简而言之,问题是您无法保证图像将立即可用(您可能需要先获取图像),这意味着视图大小在图像到达之后必须更改。这在大多数情况下是不可取的,你可能应该重新考虑你的UI。



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

  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 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.

解决方案

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天全站免登陆