Giter VIP home page Giter VIP logo

drawabledsl's Introduction

放弃xml写drawable,试试DSL写法

RECTANGLE

Kotlin应该这样写系列

SharedPreferences用Kotlin应该这样写

Glide用Kotlin应该这样封装(一)

Glide用Kotlin应该这样封装(二)

drawable用Kotlin应该这样写

集成和使用

  1. 在项目级的build.gradle文件种添加仓库Jitpack:
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
//添加依赖
dependencies {
	implementation 'com.github.forJrking:DrawableDsl:0.0.3’
}
  1. 抛弃xml创建方式使用代码
//扩展变量开省略setImageDrawable()括号                                                         
image.src = shapeDrawable {                                    
  	//指定shape样式                                    
    shape(ShapeBuilder.Shape.RECTANGLE)                                    
  	//圆角,支持4个角单独设置                                    
  	corner(20f)                                    
  	//solid 颜色                                    
    solid("#ABE2E3")                                    
  	//stroke 颜色,边框dp,虚线设置                                    
    stroke(android.R.color.white, 2f, 5f, 8f)                                    
}  
//按钮点击样式
btn.background = selectorDrawable {
  	//默认样式
    normal = shapeDrawable {
        corner(20f)
        gradient(90, R.color.F97794, R.color.C623AA2)
    }
  	//点击效果
    pressed = shapeDrawable {
        corner(20f)
        solid("#84232323")
    }
}

代码对应效果预览

shape_line

OVAL

LayerList

Selector

注意事项

//如果要使用shape制作圆环 ring,由于ring的api只能通过xml,而且一般要预览
// 这里建议用OVAL方式代替
iv.src = shapeDrawable {
    shape(Shape.OVAL)
    solid(android.R.color.transparent)
    stroke(android.R.color.black, 12f)
    size(200f, 200f)
}

//所有的大小设置,圆角等支持,默认dp
iv.src = shapeDrawable {
    solid(android.R.color.black)
  //设置成px
    size(200f, 200f,isDp = false) 
  //设置dp
    size(200f, 200f)
}

作者

👨:岛主
📮:[email protected]

有内推或岗位请联系我

开源协议

Copyright 2020-present forjrking

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.

drawabledsl's People

Contributors

forjrking avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.