Facebook Fresco using wrap_content

前端 未结 5 724
日久生厌
日久生厌 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:52

    Found a solution by extending SimpleDraweeView, it allows me to use wrap_content and it works just fine! How ever I prevent you from setting the size in setContentView and the preview is not working, I be glad if could edit this answer to fix those.

    Usage

    
    

    Source code

    public class WrapContentDraweeView extends SimpleDraweeView {
    
        private int outWidth;
        private int outHeight;
    
        public WrapContentDraweeView(Context context, GenericDraweeHierarchy hierarchy) {
            super(context, hierarchy);
        }
    
        public WrapContentDraweeView(Context context) {
            super(context);
        }
    
        public WrapContentDraweeView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context, attrs);
        }
    
        public WrapContentDraweeView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context, attrs);
        }
    
        public WrapContentDraweeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            init(context, attrs);
        }
    
        private void init(Context context, AttributeSet attrs) {
            if (attrs == null) {
                return;
            }
    
            TypedArray gdhAttrs = context.obtainStyledAttributes(
                    attrs,
                    R.styleable.GenericDraweeView);
            try {
                int placeholderId = gdhAttrs.getResourceId(
                        R.styleable.GenericDraweeView_placeholderImage,
                        0);
                if(placeholderId != 0){
                    if(isInEditMode()){
                        setImageResource(placeholderId);
                    }
                    else {
                        loadSize(placeholderId, context.getResources());
                    }
                }
            } finally {
                gdhAttrs.recycle();
            }
        }
    
        private void loadSize(int placeholderId, Resources resources) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(resources, placeholderId, options);
            outWidth = options.outWidth;
            outHeight = options.outHeight;
        }
    
        @Override
        public void setLayoutParams(ViewGroup.LayoutParams params) {
            params.width = outWidth;
            params.height = outHeight;
            super.setLayoutParams(params);
        }
    }
    

提交回复
热议问题