项目作者: infotech-group

项目描述 :
DSL for constructing the drawables in Kotlin instead of in XML
高级语言: Kotlin
项目地址: git://github.com/infotech-group/android-drawable-dsl.git
创建时间: 2017-04-07T09:40:41Z
项目社区:https://github.com/infotech-group/android-drawable-dsl

开源协议:Apache License 2.0

下载


Android Drawable Kotlin DSL CircleCI

DSL for constructing the drawables in Kotlin instead of in XML

Examples

Shape drawables

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="oval">
  4. <solid android:color="#199fff" ></solid>
  5. <stroke
  6. android:width="2dp"
  7. android:color="#444444" ></stroke>
  8. </shape>

replace it with

  1. shapeDrawable {
  2. shape = GradientDrawable.OVAL
  3. solidColor = Color.parseColor("#199fff")
  4. stroke {
  5. width = dip(2)
  6. color = Color.parseColor("#444444")
  7. }
  8. }

State selectors

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_checked="true">
  4. <shape>
  5. <solid android:color="@android:color/black" ></solid>
  6. </shape>
  7. </item>
  8. <item android:state_pressed="true">
  9. <shape>
  10. <solid android:color="@android:color/holo_red_dark" ></solid>
  11. </shape>
  12. </item>
  13. <item>
  14. <shape>
  15. <solid android:color="@android:color/white" ></solid>
  16. </shape>
  17. </item>
  18. </selector>

replace it with

  1. stateListDrawable {
  2. checkedState {
  3. shapeDrawable {
  4. solidColor = Color.BLACK
  5. }
  6. }
  7. pressedState {
  8. shapeDrawable {
  9. solidColor = Color.RED
  10. }
  11. }
  12. defaultState {
  13. shapeDrawable {
  14. solidColor = Color.WHITE
  15. }
  16. }
  17. }

Layer drawables

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item>
  4. <shape android:shape="oval">
  5. <stroke
  6. android:width="5dp"
  7. android:color="#000000" ></stroke>
  8. </shape>
  9. </item>
  10. <item>
  11. <shape android:shape="oval">
  12. <stroke
  13. android:width="2dp"
  14. android:color="#ffffff" ></stroke>
  15. </shape>
  16. </item>
  17. </layer-list>

replace it with

  1. layerDrawable(
  2. shapeDrawable {
  3. shape = GradientDrawable.OVAL
  4. stroke {
  5. width = ctx.dip(5)
  6. color = Color.parseColor("#000000")
  7. }
  8. },
  9. shapeDrawable {
  10. shape = GradientDrawable.OVAL
  11. stroke {
  12. width = ctx.dip(2)
  13. color = Color.parseColor("#ffffff")
  14. }
  15. }
  16. )

Install

  1. repositories {
  2. maven { url 'https://jitpack.io' }
  3. }


  1. compile "com.github.infotech-group:android-drawable-dsl:0.3.0"

Contribute

We haven’t covered 100% of the XML DSL, contributions are very welcome

Please write a test for every new tag you add, we (hopefully) made it easy to do