Facebook Fresco using wrap_content

前端 未结 5 717
日久生厌
日久生厌 2020-12-05 03:07

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

5条回答
  •  温柔的废话
    2020-12-05 03:47

    Based on @plamenko's answer, I made a custom view as follows:

    /**
     * Works when either height or width is set to wrap_content
     * The view is resized based on the image fetched
     */
    public class WrapContentDraweeView extends SimpleDraweeView {
    
        // we set a listener and update the view's aspect ratio depending on the loaded image
        private final 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);
            }
        };
    
        public WrapContentDraweeView(Context context, GenericDraweeHierarchy hierarchy) {
            super(context, hierarchy);
        }
    
        public WrapContentDraweeView(Context context) {
            super(context);
        }
    
        public WrapContentDraweeView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public WrapContentDraweeView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public WrapContentDraweeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    
        @Override
        public void setImageURI(Uri uri, Object callerContext) {
            DraweeController controller = ((PipelineDraweeControllerBuilder)getControllerBuilder())
                    .setControllerListener(listener)
                    .setCallerContext(callerContext)
                    .setUri(uri)
                    .setOldController(getController())
                    .build();
            setController(controller);
        }
    
        void updateViewSize(@Nullable ImageInfo imageInfo) {
            if (imageInfo != null) {
                setAspectRatio((float) imageInfo.getWidth() / imageInfo.getHeight());
            }
        }
    }
    

    You can include this class in the XML, an example usage:

    
    

提交回复
热议问题