|
| 1 | +通过封装GradientDrawable、StateListDrawable 、LayerDrawable 代替每次都需要创建一个shape.xml的不便,一定程度上也可以减少apk体积,并且使用简单。 |
| 2 | + |
| 3 | +## 功能 |
| 4 | +1.支持shape的绝大部分常用属性。 |
| 5 | +2.可代替Selector |
| 6 | +3.可代替Layer-list |
| 7 | + |
| 8 | +## How to: |
| 9 | +**Step 1. Add the JitPack repository to your build file** |
| 10 | +Add it in your root build.gradle at the end of repositories: |
| 11 | + |
| 12 | +``` |
| 13 | + allprojects { |
| 14 | + repositories { |
| 15 | + ... |
| 16 | + maven { url 'https://jitpack.io' } |
| 17 | + } |
| 18 | + } |
| 19 | +``` |
| 20 | +Step 2. Add the dependency |
| 21 | + |
| 22 | +``` |
| 23 | +dependencies { |
| 24 | + compile 'com.github.sdfdzx:SupperShape:v1.0.0' |
| 25 | + } |
| 26 | +``` |
| 27 | + |
| 28 | +## 使用 |
| 29 | +### 一、ShapeBuilder代替shape。 |
| 30 | +#### Demo |
| 31 | +``` |
| 32 | +ShapeBuilder.create() |
| 33 | + .Type(RECTANGLE) |
| 34 | + .Soild(Color.RED) |
| 35 | + .Stroke(5,Color.BLACK) |
| 36 | + .build(View); |
| 37 | +``` |
| 38 | +#### Use |
| 39 | +设置对应的属性,调用build(View)传入需要设置背景的view即可。获得构建的drawable可以调用该build()方法返回。 |
| 40 | +#### Api |
| 41 | +1.type形状属性 |
| 42 | +type:RECTANGLE,OVAL,LINE,RING |
| 43 | +``` |
| 44 | +Type(int type) |
| 45 | +``` |
| 46 | +2.Stroke边框属性 |
| 47 | +``` |
| 48 | + /** |
| 49 | + * 边线 |
| 50 | + * @param px -width,需要px值 |
| 51 | + * @param color -color值 |
| 52 | + * @param dashWidth -dashWidth 横线的宽度 |
| 53 | + * @param dashGap -dashGap 点与点间的距离 |
| 54 | + */ |
| 55 | +public ShapeBuilder Stroke(int px, int color) |
| 56 | +public ShapeBuilder Stroke(int px, int color, int dashWidth, int dashGap) |
| 57 | +``` |
| 58 | +3.Soild填充属性 |
| 59 | +``` |
| 60 | + /** |
| 61 | + * |
| 62 | + * @param color -背景颜色 |
| 63 | + */ |
| 64 | + public ShapeBuilder Soild(int color) |
| 65 | +``` |
| 66 | +4.Radius圆角属性 |
| 67 | +``` |
| 68 | + /** |
| 69 | + * |
| 70 | + * @param px -圆角,四个角保持一致 |
| 71 | + */ |
| 72 | + public ShapeBuilder Radius(float px) |
| 73 | + /* |
| 74 | + * 圆角 |
| 75 | + * @param topleft 左上 |
| 76 | + * @param topright 右上 |
| 77 | + * @param botleft 左下 |
| 78 | + * @param botright 右下 |
| 79 | + */ |
| 80 | + public ShapeBuilder Radius(float topleft, float topright, float botleft, float botright) |
| 81 | +``` |
| 82 | +5.Gradient渐变属性 |
| 83 | +``` |
| 84 | + /* |
| 85 | + * 渐变,默认的Linear渐变 |
| 86 | + * @param startColor 开始颜色 |
| 87 | + * @param centerColor 中心颜色 |
| 88 | + * @param endColor 结束颜色 |
| 89 | + */ |
| 90 | + public ShapeBuilder Gradient(int startColor, int centerColor, int endColor) |
| 91 | + // @param angle 角度,需要是45的整数倍 |
| 92 | + public ShapeBuilder Gradient(int angle, int startColor, int centerColor, int endColor) |
| 93 | + /* |
| 94 | + * 渐变,设置渐变方向 |
| 95 | + * @param orientation 方向支持类型 |
| 96 | + * 0-LEFT_RIGHT |
| 97 | + * 45-BL_TR |
| 98 | + * 90-BOTTOM_TOP |
| 99 | + * 135-BR_TL |
| 100 | + * 180-RIGHT_LEFT |
| 101 | + * 225-TR_BL |
| 102 | + * 270-TOP_BOTTOM |
| 103 | + * 315-TL_BR |
| 104 | + */ |
| 105 | + public ShapeBuilder Gradient(GradientDrawable.Orientation orientation, int startColor, int |
| 106 | + centerColor, int endColor) |
| 107 | +``` |
| 108 | +6.GradientType渐变类型属性 |
| 109 | +``` |
| 110 | + /* |
| 111 | + * 渐变type |
| 112 | + * @param type linear (default.)-LINEAR_GRADIENT |
| 113 | + * circular-RADIAL_GRADIENT |
| 114 | + * sweep-SWEEP_GRADIENT |
| 115 | + * @return |
| 116 | + */ |
| 117 | + public ShapeBuilder GradientType(int type) |
| 118 | +``` |
| 119 | +7.GradientCenter渐变中心属性 |
| 120 | +``` |
| 121 | + /* |
| 122 | + * 这两个属性只有在type不为linear情况下起作用。 |
| 123 | + * @param x 相对X的渐变位置 |
| 124 | + * @param y 相对Y的渐变位置 |
| 125 | + * @return |
| 126 | + */ |
| 127 | + public ShapeBuilder GradientCenter(float x, float y) |
| 128 | +``` |
| 129 | +8.GradientRadius渐变颜色半径 |
| 130 | +``` |
| 131 | + /* |
| 132 | + * 该属性只有在type="radial"有效 |
| 133 | + * @param radius 渐变颜色的半径 |
| 134 | + * @return |
| 135 | + */ |
| 136 | + public ShapeBuilder GradientRadius(float radius) |
| 137 | +``` |
| 138 | +9.Size属性 |
| 139 | +``` |
| 140 | + /* |
| 141 | + * 设置size |
| 142 | + * @param width 宽 |
| 143 | + * @param height 高 |
| 144 | + * @return |
| 145 | + */ |
| 146 | + public ShapeBuilder setSize(int width, int height) |
| 147 | +``` |
| 148 | +### 二、ShapeListBuilder替代Selector |
| 149 | +#### Demo |
| 150 | +``` |
| 151 | + ShapeBuilder builder1 = ShapeBuilder.create() |
| 152 | + .Type(RECTANGLE) |
| 153 | + .Soild(Color.RED); |
| 154 | + ShapeBuilder builder2 = ShapeBuilder.create() |
| 155 | + .Type(RECTANGLE) |
| 156 | + .Soild(Color.RED); |
| 157 | +
|
| 158 | + ShapeListBuilder.create(builder1.build()) |
| 159 | + .addShape(builder2.Soild(Color.BLUE).build(), android.R.attr.state_selected) |
| 160 | + .build(findViewById(R.id.tv1)); |
| 161 | +
|
| 162 | + findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() { |
| 163 | + @Override |
| 164 | + public void onClick(View view) { |
| 165 | + findViewById(R.id.tv1).setSelected(!findViewById(R.id.tv1).isSelected()); |
| 166 | + } |
| 167 | + }); |
| 168 | +``` |
| 169 | +#### Use |
| 170 | +1.传入默认状态的drawable |
| 171 | +``` |
| 172 | + /* |
| 173 | + * @param drawable 传入默认状态下的drawable |
| 174 | + */ |
| 175 | + public static ShapeListBuilder create(Drawable drawable) |
| 176 | +``` |
| 177 | +2.添加对应状态的drawable |
| 178 | +``` |
| 179 | + /* |
| 180 | + * 添加状态 |
| 181 | + * @param shape 状态对应的shape |
| 182 | + * @param state 状态类型 |
| 183 | + */ |
| 184 | + public ShapeListBuilder addShape(Drawable shape, int... state) |
| 185 | +``` |
| 186 | +**(这里要注意添加的顺序,只要有一个状态与之相配,背景就会被换掉。所以不要把大范围放在前面了,会造成没有什么效果了。)** |
| 187 | +3.build(View)即可 |
| 188 | +### 三、LayerBuilder替代Layer-list |
| 189 | +#### Demo |
| 190 | +``` |
| 191 | +LayerBuilder.create(builder1.build(), builder2.build()).Bottom(1, 15).build(findViewById(R |
| 192 | + .id.tv3)); |
| 193 | +``` |
| 194 | +#### Use |
| 195 | +1.传入多个drawable |
| 196 | +``` |
| 197 | +public static LayerBuilder create(Drawable... drawables) |
| 198 | +``` |
| 199 | +2.设置index的偏移量 |
| 200 | +``` |
| 201 | +public LayerBuilder Left(int index, int px) |
| 202 | +public LayerBuilder Top(int index, int px) |
| 203 | +public LayerBuilder Right(int index, int px) |
| 204 | +public LayerBuilder Bottom(int index, int px) |
| 205 | +public LayerBuilder setInset(int index,int left,int top,int right,int bottom) |
| 206 | +``` |
| 207 | +3.build(View)即可 |
0 commit comments