项目作者: duanhong169

项目描述 :
🛠️ The missing drawable toolbox for Android. Create drawables programmatically and get rid of the boring and always repeated drawable.xml files.
高级语言: Kotlin
项目地址: git://github.com/duanhong169/DrawableToolbox.git
创建时间: 2018-08-01T07:57:06Z
项目社区:https://github.com/duanhong169/DrawableToolbox

开源协议:Apache License 2.0

下载


DrawableToolbox

gitHub release
platform

license
Awesome Kotlin Badge

English | 中文

The missing DrawableToolbox for Android. Create drawables programmatically and get rid of the boring and always repeated drawable.xml files.



Features

  • Create drawables programmatically
  • Support <shape>, <rotate>, <scale>, <ripple>, <layer-list> drawables
  • Support ‘flip’ (vertical or horizontal)

Contribute

Gradle

  1. dependencies {
  2. implementation 'com.github.duanhong169:drawabletoolbox:${latestVersion}'
  3. ...
  4. }

Replace ${latestVersion} with the latest version code. See releases.

Usage

Use the DrawableBuilder to setup the Drawable and call build() to create it.

Please check all the supported APIs in DrawableBuilder.kt.

Here are some examples:

Code:

  1. DrawableBuilder()
  2. .rectangle()
  3. .hairlineBordered()
  4. .strokeColor(COLOR_DEFAULT)
  5. .strokeColorPressed(COLOR_PRESSED)
  6. .ripple()
  7. .build()

Result:

Bordered with Ripple

Code:

  1. DrawableBuilder()
  2. .rectangle()
  3. .hairlineBordered()
  4. .mediumDashed()
  5. .strokeColor(COLOR_DEFAULT)
  6. .strokeColorPressed(COLOR_PRESSED)
  7. .ripple()
  8. .build()

Result:

Medium-dashed, Bordered with Ripple

Code:

  1. DrawableBuilder()
  2. .rectangle()
  3. .rounded()
  4. .solidColor(COLOR_DEFAULT)
  5. .solidColorPressed(COLOR_PRESSED)
  6. .build()

Result:

Rounded, Filled with States

Code:

  1. DrawableBuilder()
  2. .rectangle()
  3. .hairlineBordered()
  4. .longDashed()
  5. .rounded()
  6. .strokeColor(COLOR_DEFAULT)
  7. .strokeColorPressed(COLOR_PRESSED)
  8. .ripple()
  9. .build()

Result:

Rounded, Long-dashed, Bordered with Ripple

Code:

  1. DrawableBuilder()
  2. .rectangle()
  3. .rounded()
  4. .gradient()
  5. .linearGradient()
  6. .angle(90)
  7. .startColor(COLOR_DEFAULT)
  8. .endColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
  9. .ripple()
  10. .rippleColor(COLOR_PRESSED)
  11. .build()

Result:

Rounded, Gradient with Ripple

Code:

  1. val baseBuilder = DrawableBuilder()
  2. .rectangle()
  3. .rounded()
  4. .gradient()
  5. .gradientType(GradientDrawable.LINEAR_GRADIENT)
  6. .angle(90)
  7. val normalState = baseBuilder
  8. .startColor(COLOR_DEFAULT)
  9. .endColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
  10. .build()
  11. val pressedState = baseBuilder
  12. .startColor(COLOR_PRESSED)
  13. .endColor(ContextCompat.getColor(context, R.color.colorAccentDark))
  14. .build()
  15. StateListDrawableBuilder()
  16. .normal(normalState)
  17. .pressed(pressedState)
  18. .build()

Result:

Rounded, Gradient with States

Code:

  1. val baseBuilder = DrawableBuilder()
  2. .rectangle()
  3. .rounded()
  4. .hairlineBordered()
  5. .strokeColor(COLOR_DEFAULT)
  6. .solidColorSelected(COLOR_DEFAULT)
  7. .ripple()
  8. return when(type) {
  9. SegmentedControlDrawableSpec.TYPE_LEFT_MOST -> {
  10. baseBuilder.topRightRadius(0)
  11. .bottomRightRadius(0)
  12. .build()
  13. }
  14. SegmentedControlDrawableSpec.TYPE_RIGHT_MOST -> {
  15. baseBuilder.topLeftRadius(0)
  16. .bottomLeftRadius(0)
  17. .build()
  18. }
  19. else -> {
  20. baseBuilder.cornerRadius(0).build()
  21. }
  22. }

Result:

Segmented Control

Code:

  1. val layer1 = DrawableBuilder()
  2. .size(200)
  3. .rectangle()
  4. .rounded()
  5. .hairlineBordered()
  6. .strokeColor(COLOR_DEFAULT)
  7. .strokeColorPressed(COLOR_PRESSED)
  8. .build()
  9. val layer2 = DrawableBuilder()
  10. .rectangle()
  11. .rounded()
  12. .solidColor(COLOR_DEFAULT)
  13. .build()
  14. val layer3 = DrawableBuilder()
  15. .rectangle()
  16. .rounded()
  17. .solidColor(Color.WHITE)
  18. .ripple()
  19. .rippleColor(COLOR_DEFAULT)
  20. .build()
  21. LayerDrawableBuilder()
  22. .add(layer1)
  23. .add(layer2)
  24. .inset(10)
  25. .add(layer3)
  26. .inset(20)
  27. .build()

Result:

Layer List: Several Borders

Code:

  1. val layer1 = DrawableBuilder()
  2. .size(180)
  3. .rectangle()
  4. .build()
  5. val layer2 = DrawableBuilder()
  6. .oval()
  7. .solidColor(COLOR_DEFAULT)
  8. .build()
  9. val layer3 = DrawableBuilder()
  10. .rectangle()
  11. .solidColor(COLOR_DEFAULT_DARK)
  12. .rotate(45f)
  13. .build()
  14. val layer4 = DrawableBuilder()
  15. .rectangle()
  16. .bottomLeftRadius(100)
  17. .solidColor(COLOR_DEFAULT_DARK)
  18. .build()
  19. val layer5 = DrawableBuilder()
  20. .oval()
  21. .solidColor(COLOR_DEFAULT)
  22. .build()
  23. val layerDrawable = LayerDrawableBuilder()
  24. .add(layer1)
  25. .add(layer2)
  26. .inset(20, 20, 100, 100)
  27. .add(layer3)
  28. .inset(100, 20, 20, 100)
  29. .add(layer4)
  30. .inset(20, 100, 100, 20)
  31. .add(layer5)
  32. .inset(100, 100, 20, 20)
  33. .build()
  34. DrawableBuilder()
  35. .baseDrawable(layerDrawable)
  36. .rotate(0f, 360f)
  37. .build()

Result:

Showcase: Layer List 1

Code:

  1. // Rotate & Leveled the Ring
  2. DrawableBuilder()
  3. .size(200)
  4. .ring()
  5. .useLevelForRing()
  6. .solidColor(COLOR_DEFAULT)
  7. .innerRadiusRatio(3f)
  8. .thicknessRatio(10f)
  9. .rotate(0f, 720f)
  10. .build()

Result:

Rotate & Leveled the Ring

Code:

  1. // Rotate, Sweep & Flip the Ring
  2. DrawableBuilder()
  3. .size(200)
  4. .ring()
  5. .innerRadiusRatio(3f)
  6. .thicknessRatio(10f)
  7. .gradient()
  8. .sweepGradient()
  9. .rotate(0f, 360f)
  10. .flip()
  11. .build()

Result:

Rotate, Sweep & Flip the Ring

Code:

  1. // Rotate, Sweep & Scale the Oval with States
  2. val baseBuilder = DrawableBuilder()
  3. .size(400)
  4. .oval()
  5. .gradient()
  6. .sweepGradient()
  7. .rotate(0f, 360f)
  8. .scale(0.5f)
  9. .scaleGravity(Gravity.START or Gravity.TOP)
  10. val normalState = baseBuilder.build()
  11. val pressedState = baseBuilder
  12. .startColor(COLOR_PRESSED)
  13. .endColor(0x7FFFFFFF)
  14. .build()
  15. StateListDrawableBuilder()
  16. .normal(normalState)
  17. .pressed(pressedState)
  18. .build()

Result:

Rotate, Sweep & Scale the Oval with States

Please check out the app sample code SampleCodeSnippets.kt for more details.

License

  1. Copyright 2018 Hong Duan
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.