让simpleDraweeView实现自适应高宽+圆角

踩过坑的朋友都知道,要实现自适应又需要圆角,大部分框架都有毛病,包括这个也是有的.
解决方法如下
xml设置:

<com.facebook.drawee.view.SimpleDraweeView
                            android:id="@+id/iv_banner"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="@dimen/young_author_page_margin_left_right"
                            android:scaleType="centerCrop"
                            app:actualImageScaleType="centerCrop"
                            app:radiusX="@{@dimen/young_theme_radius}"
                            app:ratioimage="@{model.image}" />

databind代码如下

@BindingAdapter({"ratioimage", "radiusX"})
    public static void imageloaderxx(final SimpleDraweeView imageView, final String image,
                                     final float radius) {
        if (image == null) {
            return;
        }
/*
        if (TextUtils.isEmpty(image)) {
            return;
        }
*/

        final ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();

        final int viewWidth = imageView.getWidth();
        if (viewWidth == 0) {
            imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } else {
                        imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }
                    if (imageView.getWidth() == 0) {


                        return;
                    }

                    imageloaderxx(imageView, image, radius);
                }
            });
            return;

        }

        ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                if (imageInfo == null) {
                    return;
                }
                int height = imageInfo.getHeight();
                int width = imageInfo.getWidth();


                layoutParams.width = imageView.getWidth();
                int heightScale = (int) ((float) (viewWidth * height) / (float) width);
                layoutParams.height = heightScale;
                imageView.setLayoutParams(layoutParams);



                setImageRadius(imageView, radius);


//                imageView.setAspectRatio(height / width);
            }

            @Override
            public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
                Log.d("TAG", "Intermediate image received");
            }

            @Override
            public void onFailure(String id, Throwable throwable) {
                throwable.printStackTrace();
            }
        };

        DraweeController controller = Fresco.newDraweeControllerBuilder().setControllerListener(controllerListener).setUri(Uri.parse(image)).build();
        imageView.setController(controller);
    }

切忌不要写死宽度,否则会导致圆角又失效了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值