手把手教你搭建android模块化项目框架(十二)——实现自定义view的一些小技巧~

原来今天才是周六~那就今天水

自定义view怎么实现,我今天不想多说,毕竟也不是给新人看的。

那么今天直接讲一些实现自定义view的小技巧吧。

本期举例的自定义view只是抛砖引玉,随手写的没有经过测试,如果想使用一定要三思而后行~

1.利用databinding或者viewbinding,告别如下代码~

kotlin 复制代码
animView = findViewById(R.id.anim_view)
iconView = findViewById(R.id.iv_tab)
textView = findViewById(R.id.tv_tab)
badgeView = findViewById(R.id.iv_badge)

那么我们直接看优化后的代码~

kotlin 复制代码
private val mBinding by lazy {
    ViewMainBottomLayoutBinding.inflate(
        LayoutInflater.from(context),
        this,
        true
    )
}

mBinding.tvTab.xxxxxxxx
//直接使用,节省时间。
  1. 让你的自定义view支持style,方便使用

首先看我们的自定义属性

xml 复制代码
<declare-styleable name="BottomNavigationView">
    <attr name="iconWidth" format="dimension" />
    <attr name="iconHeight" format="dimension" />
    <attr name="textSize" format="dimension" />
    <attr name="textColor" format="color" />
</declare-styleable>

然后定义默认style

xml 复制代码
<style name="BottomNavigationViewStyle">
    <item name="iconWidth">30dp</item>
    <item name="iconHeight">30dp</item>
    <item name="textColor">@color/color_bottom_nav_view_text_default</item>
    <item name="textSize">12sp</item>
</style>

在创建view的构造函数中填入默认style,然后其他与正常写自定义view就一样啦~

kotlin 复制代码
constructor(context: Context, attrs: AttributeSet?) : super(
    context,
    attrs,
    R.style.BottomNavigationViewStyle
) {
    setupAttr(attrs)
}

如果我们想动态加入主题呢?可以在自定义view中添加setTheme方法,然后取值方式如下,可能还有其他取值方式~不过懒得找了。

kotlin 复制代码
fun setTheme(themeId: Int) {
    val mTheme = context.resources.newTheme()
    mTheme.applyStyle(themeId, true)
    mTheme.obtainStyledAttributes(
        intArrayOf(
            R.attr.iconWidth,
            R.attr.iconHeight,
            R.attr.textColor,
            R.attr.textSize
        )
    ).run {
        iconWidth =
            this.getDimensionPixelSize(this.getIndex(0), iconWidth)
        iconHeight =
            this.getDimensionPixelSize(this.getIndex(1), iconHeight)
        textColor =
            this.getColorStateList(this.getIndex(2)) ?: textColor
        textSize = this.getDimension(this.getIndex(3), textSize)
        recycle()
    }
    setup()
}

如此,我们便可以直接配置style给自定义view啦~由于本demo使用的是组合view,所以我们可以在父view中接受自定义参数例如:

xml 复制代码
<declare-styleable name="BottomNavigationGroup">
    <attr name="navBottomViewStyle" format="reference" />
</declare-styleable>

然后获取:

kotlin 复制代码
context.obtainStyledAttributes(attrs, R.styleable.BottomNavigationGroup).run {
    navViewThemeId =
        getResourceId(R.styleable.BottomNavigationGroup_navBottomViewStyle, navViewThemeId)
    recycle()
}

之后在Build子view时,将themeId传入即可~

当然,写法有很多,本篇仅仅是抛砖引玉而已。

  1. dsl构建view参数

先看效果~ 可以是这样的

kotlin 复制代码
mBinding.homeTab.setup {
    options(
        bottomNavOption {
            id { R.id.home }
            tabText { "home" }
            iconRes { R.drawable.ic_main_nav_home }
        },
        bottomNavOption {
            id { R.id.topic }
            tabText { "topic" }
            iconRes { R.drawable.ic_main_nav_home }
        },
        bottomNavOption {
            id { R.id.find }
            tabText { "find" }
            iconRes { R.drawable.ic_main_nav_home }
        },
        bottomNavOption {
            id { R.id.me }
            tabText { "me" }
            iconRes { R.drawable.ic_main_nav_home }
        }
    )
    listener {
        object : BottomNavigationGroup.OnCheckedChangeListener {
            override fun onCheckedChanged(group: BottomNavigationGroup?, checkedId: Int) {

            }

        }
    }
    defaultChecked {
        R.id.home
    }
}

也可以是这样的~

kotlin 复制代码
mBinding.homeTab.setup {
    options(
        bottomNavOption {
            id { R.id.home }
            tabText { "home" }
            iconRes { R.drawable.ic_main_nav_home }
        })
    options(bottomNavOption {
        id { R.id.topic }
        tabText { "topic" }
        iconRes { R.drawable.ic_main_nav_home }
    })
    options(
        bottomNavOption {
            id { R.id.find }
            tabText { "find" }
            iconRes { R.drawable.ic_main_nav_home }
        })
    bottomNavOption {
        id { R.id.me }
        tabText { "me" }
        iconRes { R.drawable.ic_main_nav_home }
    }
    )
    listener {
        object : BottomNavigationGroup.OnCheckedChangeListener {
            override fun onCheckedChanged(group: BottomNavigationGroup?, checkedId: Int) {

            }

        }
    }
    defaultChecked {
        R.id.home
    }
}

当然,写法有很多,本文最终提交的是第一种的写法~

这个dsl看起来复杂,其实很简单,例如option构建时我们多写一些方法~

kotlin 复制代码
class Option {
    @IdRes
    var id: Int = -1
        private set
    var tabText: String = ""

    @DrawableRes
    var iconRes: Int = 0
        private set

    var textColor: ColorStateList? = null
        private set

    var iconW: Int = 0
        private set

    var iconH: Int = 0
        private set

    var textSize: Float = 0f
        private set

    fun id(init: () -> Int) {
        id = init()
    }

    fun tabText(init: () -> String) {
        tabText = init()
    }

    fun iconRes(init: () -> Int) {
        iconRes = init()
    }

    fun textColor(init: () -> Int) {
        textColor = ResourceUtil.getColorStateList(resId = init())
    }

    fun iconW(init: () -> Int) {
        iconW = init()
    }

    fun iconH(init: () -> Int) {
        iconH = init()
    }

    fun textSize(init: () -> Float) {
        textSize = init()
    }
}

这样就可以使用高阶函数进行构建了,配合kotlin的lambda特性即可达到效果~

当然,为了看起来更舒适,也少不了我们的扩展函数啦~

kotlin 复制代码
fun bottomNavOption(init: BottomNavigationView.Option.() -> Unit): BottomNavigationView.Option {
    val option = BottomNavigationView.Option()
    option.init()
    return option
}

至此,我们便完成了一个优雅的自定义view

完整代码

相关推荐
飞猿_SIR1 小时前
Android Exoplayer 实现多个音视频文件混合播放以及音轨切换
android·音视频
HumoChen991 小时前
GZip+Base64压缩字符串在ios上解压报错问题解决(安卓、PC模拟器正常)
android·小程序·uniapp·base64·gzip
邪恶的贝利亚2 小时前
《Docker 入门与进阶:架构剖析、隔离原理及安装实操》
docker·容器·架构
沙振宇5 小时前
【HarmonyOS】ArkTS开发应用的横竖屏切换
android·华为·harmonyos
镜舟科技6 小时前
湖仓一体架构在金融典型数据分析场景中的实践
starrocks·金融·架构·数据分析·湖仓一体·物化视图·lakehouse
Ramseyuu6 小时前
Mybatis-plus
微服务·云原生·架构
charlie1145141917 小时前
内核深入学习3——分析ARM32和ARM64体系架构下的Linux内存区域示意图与页表的建立流程
linux·学习·架构·内存管理
橙子199110167 小时前
Kotlin 中的作用域函数
android·开发语言·kotlin
zimoyin7 小时前
Kotlin 懒初始化值
android·开发语言·kotlin
堕落年代8 小时前
SpringBoot的单体和分布式的任务架构
spring boot·分布式·架构