Android工具集

DataBind第四篇:聚气宝石BindingAdapter多数

2019-06-22  本文已影响1人  Small_Cake

原文链接:https://www.jianshu.com/p/e7c0b877b504

上一篇我们已经讲了用BindingAdapter绑定圆角图片,但有时候我们的图片圆角并不一定都是写死的,所有我们需要用到BindingAdapter的多数据绑定

1.创建
  //图片加载绑定为:圆角图片,圆角系数
    @BindingAdapter(value = {"imageRoundUrl","roundingRadius"}, requireAll = false)
    public static void bindImageRoundUrl(ImageView view, String imageRoundUrl,int roundingRadius){
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher)
                        .transform(new RoundedCorners(roundingRadius==0?20:roundingRadius));
        Glide.with(view)
                .load(imageRoundUrl)
                .apply(options)
                .into(view);
    }

requireAll = false意思是两个参数可以缺省,我们就需要判断一下roundingRadius值是否没填,int类型没填,默然会赋值0,所以这里我们判断如果为0默认给一个20的圆角。

2.使用:
 <ImageView
            app:roundingRadius="@{50}"
            app:imageRoundUrl="@{item.imgUrl}"
            android:layout_width="200dp"
            android:layout_height="200dp" />

效果图:

自定义圆角弧度50

注意:使用app:roundingRadius的时候一定要使用@{}里面填入参数,而不能直接写数字

如果不填

        <ImageView
            app:imageRoundUrl="@{item.imgUrl}"
            android:layout_width="200dp"
            android:layout_height="200dp" />

效果图:


缺省圆角20
上一篇下一篇

猜你喜欢

热点阅读