需求分析
这里只作简单介绍,最后会分享源码地址
- 窗帘分为三部分,上面的窗帘杆,杆下面的窗帘布,以及布中间的滑块,实现还是蛮简单的,我们可以通过自定义view把这个窗帘画出来
- 窗帘杆是一个上面是圆角,下面是直角的矩形,窗帘的叶子是上面直角,下面圆角的矩形,这里我们可以通过canvas.drawRoundRect来进行圆角矩形的绘制,然后叠加在一起显示。
- 但是进行自定义view的绘制时,需要避免重复绘制 因此直接通过canvas.drawRoundRect来绘制在叠加在一起是不行,所以这里我是采用Path.addRoundRect方法来实现
- 这里提供了一个自定义属性curtain_type来设置这个窗帘时单开帘还是双开帘
实现
定义好所需要的自定义view属性,这样可以支持定制化的颜色以及滑块图标
<declare-styleable name="CurtainView">
<!-- 窗帘最小范围 -->
<attr name="min" format="integer" />
<!-- 窗帘最大范围 -->
<attr name="max" format="integer" />
<!-- 窗帘当前进度 -->
<attr name="progress" format="integer" />
<!-- 最小进度为窗帘两边的距离,应该要大于thumb一半的宽度-->
<attr name="min_progress" format="float" />
<!-- 动画时长 -->
<attr name="duration" format="integer" />
<!-- 窗帘杆的颜色 -->
<attr name="curtain_rod_color" format="color" />
<!-- 窗帘杆的高度 -->
<attr name="curtain_rod_height" format="dimension" />
<!-- 窗帘叶子的颜色 -->
<attr name="curtain_leaves_color" format="color" />
<!-- 窗帘滑块 -->
<attr name="curtain_thumb" format="reference" />
<!-- 窗帘类型 单开帘还是双开帘 -->
<attr name="curtain_type" format="boolean" />
</declare-styleable>
编写自定义View-CurtainView
val obtainStyledAttributes =
context.obtainStyledAttributes(attributeSet, R.styleable.CurtainView)
setMin(obtainStyledAttributes.getInt(R.styleable.CurtainView_min, mMin))
setMax(obtainStyledAttributes.getInt(R.styleable.CurtainView_max, mMax))
setDurtain(obtainStyledAttributes.getInt(R.styleable.CurtainView_duration, mDuration))
setProgress(obtainStyledAttributes.getInt(R.styleable.CurtainView_progress, mProgress))
setMinProgress(
obtainStyledAttributes.getFloat(
R.styleable.CurtainView_min_progress,
minProgress