用Kotlin封装一个Android中View的BackgroundDrawableBuilder

18 篇文章 0 订阅
2 篇文章 0 订阅

参考自:https://github.com/jaychang0917/SimpleText  

一个自定义的TextView,对background的设置进行了简单封装。同时包含了一个ShapeBuilder,可以用于设置给所有View添加背景

简化View的background创建,支持在xml和代码中设置backgroundDrawable, 代码中支持链式调用,或者DSL创建

支持在xml和代码中设置View的background  

本项目源码地址:https://github.com/chenyucheng97/AndroidShapeBuilder

欢迎star?

先上效果图:

 

xml设置方式:

   <com.unicorn.androidshapebuilder.MyTextView
            android:text="textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"

            app:soildBac="@android:color/holo_green_light"
            app:strokeColor="@android:color/holo_red_light"
            app:strokeWidth="2dp"

            app:radiusBottomLeft="15dp"
            app:radiusBottomRight="2dp"
            app:radiusTopLeft="15dp"
            app:radiusTopRight="8dp"
            android:id="@+id/textView5"/>

代码设置方式有多种,可以选择自己喜欢的方式来创建:

        /**
         * 链式调用方式定义对象
         */
        val shapeBuilder1 = SuperBackgroundDrawable().radius(35f).stroke(5, Color.BLACK).solid(Color.RED)

        /**
         * 使用标准库函数方式定义对象
         */
        val shapeBuilder2 = SuperBackgroundDrawable().apply {
            radius(10000f)
            stroke(5, Color.BLACK)
            solid(Color.YELLOW)
        }

        /**
         * DSL方式定义对象
         */
        val shapeBuilder3 = shapeDSLBuilder {
            radius = 35f
            strokeWidth = 5
            strokeColor = Color.BLACK
            solidColor = Color.LTGRAY
        }


        /**
         * 工具类方式定义对象
         */
        val shapeBuilder4 = shapeBuilder {
            radius(35f)
            solid(Color.GREEN)
        }

        /**
         * DSL方式定义对象
         */
        val shapeBuilder5 = shapeDSLBuilder {
            topLeftRadius = 35f
            topRightRadius = 0f
            botLeftRadius = 35f
            botRightRadius = 0f
            strokeWidth = 5
            strokeColor = Color.BLACK
            solidColor = Color.BLUE
        }

        textView0.background = shapeBuilder1
        textView1.background = shapeBuilder2
        textView2.background = shapeBuilder3
        textView3.background = shapeBuilder4
        textView4.background = shapeBuilder5

后期可以考虑加入layerlist支持,感兴趣的同学可以直接在我的源码基础上添加,可以参考原作者项目,其中包含了更多的功能。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值