项目
在 GitHub 查看
duanhong169/DrawableToolbox
关于
🛠️ Android 缺少的 drawable 工具箱。以编程方式创建 drawable,摆脱枯燥且总要重复编写的 drawable.xml 文件。
网站与应用开发可以做什么
- Create drawables programmatically
- Support <shape>, <rotate>, <scale>, <ripple>, <layer-list> drawables
- Support 'flip' (vertical or horizontal)
实测星标增长
- 24小时
- -1
- 7天
- -1
- 15天
- -1
- 1个月
- -5
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README_cn.md
原文DrawableToolbox
English | 中文
功能完备的Drawable工具箱,通过代码构建种类多样的Drawable,摆脱枯燥重复的drawable.xml文件。

功能
- 通过代码构建Drawable
- 支持
<shape>,<rotate>,<scale>,<ripple>,<layer-list>等类型的drawable - 支持镜面翻转(水平或垂直方向)
参与本项目
- 如果你有需要的功能,请提Issue或Pull request
Gradle
dependencies {
implementation 'com.github.duanhong169:drawabletoolbox:${latestVersion}'
...
}
将上方的
${latestVersion}替换为当前最新的版本号,最新版本号参见releases。
使用说明
通过DrawableBuilder来配置Drawable, 然后使用build()构建得到Drawable对象。
完整支持的API请参见DrawableBuilder.kt。
如下是一些示例代码和对应得到的Drawable:
代码:
DrawableBuilder()
.rectangle()
.hairlineBordered()
.strokeColor(COLOR_DEFAULT)
.strokeColorPressed(COLOR_PRESSED)
.ripple()
.build()
结果:

代码:
DrawableBuilder()
.rectangle()
.hairlineBordered()
.mediumDashed()
.strokeColor(COLOR_DEFAULT)
.strokeColorPressed(COLOR_PRESSED)
.ripple()
.build()
结果:

代码:
DrawableBuilder()
.rectangle()
.rounded()
.solidColor(COLOR_DEFAULT)
.solidColorPressed(COLOR_PRESSED)
.build()
结果:

代码:
DrawableBuilder()
.rectangle()
.hairlineBordered()
.longDashed()
.rounded()
.strokeColor(COLOR_DEFAULT)
.strokeColorPressed(COLOR_PRESSED)
.ripple()
.build()
结果:

代码:
DrawableBuilder()
.rectangle()
.rounded()
.gradient()
.linearGradient()
.angle(90)
.startColor(COLOR_DEFAULT)
.endColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
.ripple()
.rippleColor(COLOR_PRESSED)
.build()
结果:

代码:
val baseBuilder = DrawableBuilder()
.rectangle()
.rounded()
.gradient()
.gradientType(GradientDrawable.LINEAR_GRADIENT)
.angle(90)
val normalState = baseBuilder
.startColor(COLOR_DEFAULT)
.endColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
.build()
val pressedState = baseBuilder
.startColor(COLOR_PRESSED)
.endColor(ContextCompat.getColor(context, R.color.colorAccentDark))
.build()
StateListDrawableBuilder()
.normal(normalState)
.pressed(pressedState)
.build()
结果:

代码:
val baseBuilder = DrawableBuilder()
.rectangle()
.rounded()
.hairlineBordered()
.strokeColor(COLOR_DEFAULT)
.solidColorSelected(COLOR_DEFAULT)
.ripple()
return when(type) {
SegmentedControlDrawableSpec.TYPE_LEFT_MOST -> {
baseBuilder.topRightRadius(0)
.bottomRightRadius(0)
.build()
}
SegmentedControlDrawableSpec.TYPE_RIGHT_MOST -> {
baseBuilder.topLeftRadius(0)
.bottomLeftRadius(0)
.build()
}
else -> {
baseBuilder.cornerRadius(0).build()
}
}
结果:

代码:
val layer1 = DrawableBuilder()
.size(200)
.rectangle()
.rounded()
.hairlineBordered()
.strokeColor(COLOR_DEFAULT)
.strokeColorPressed(COLOR_PRESSED)
.build()
val layer2 = DrawableBuilder()
.rectangle()
.rounded()
.solidColor(COLOR_DEFAULT)
.build()
val layer3 = DrawableBuilder()
.rectangle()
.rounded()
.solidColor(Color.WHITE)
.ripple()
.rippleColor(COLOR_DEFAULT)
.build()
LayerDrawableBuilder()
.add(layer1)
.add(layer2)
.inset(10)
.add(layer3)
.inset(20)
.build()
结果:

代码:
val layer1 = DrawableBuilder()
.size(180)
.rectangle()
.build()
val layer2 = DrawableBuilder()
.oval()
.solidColor(COLOR_DEFAULT)
.build()
val layer3 = DrawableBuilder()
.rectangle()
.solidColor(COLOR_DEFAULT_DARK)
.rotate(45f)
.build()
val layer4 = DrawableBuilder()
.rectangle()
.bottomLeftRadius(100)
.solidColor(COLOR_DEFAULT_DARK)
.build()
val layer5 = DrawableBuilder()
.oval()
.solidColor(COLOR_DEFAULT)
.build()
val layerDrawable = LayerDrawableBuilder()
.add(layer1)
.add(layer2)
.inset(20, 20, 100, 100)
.add(layer3)
.inset(100, 20, 20, 100)
.add(layer4)
.inset(20, 100, 100, 20)
.add(layer5)
.inset(100, 100, 20, 20)
.build()
DrawableBuilder()
.baseDrawable(layerDrawable)
.rotate(0f, 360f)
.build()
结果:

代码:
// Rotate & Leveled the Ring
DrawableBuilder()
.size(200)
.ring()
.useLevelForRing()
.solidColor(COLOR_DEFAULT)
.innerRadiusRatio(3f)
.thicknessRatio(10f)
.rotate(0f, 720f)
.build()
结果:

代码:
// Rotate, Sweep & Flip the Ring
DrawableBuilder()
.size(200)
.ring()
.innerRadiusRatio(3f)
.thicknessRatio(10f)
.gradient()
.sweepGradient()
.rotate(0f, 360f)
.flip()
.build()
结果:

代码:
// Rotate, Sweep & Scale the Oval with States
val baseBuilder = DrawableBuilder()
.size(400)
.oval()
.gradient()
.sweepGradient()
.rotate(0f, 360f)
.scale(0.5f)
.scaleGravity(Gravity.START or Gravity.TOP)
val normalState = baseBuilder.build()
val pressedState = baseBuilder
.startColor(COLOR_PRESSED)
.endColor(0x7FFFFFFF)
.build()
StateListDrawableBuilder()
.normal(normalState)
.pressed(pressedState)
.build()
结果:

更多的代码示例请查看SampleCodeSnippets.kt。
License
Copyright 2018 Hong Duan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
探索相关项目
加载中…