Skip to content

Commit 3683cfa

Browse files
author
肖琪(融汇/产品技术中心)
committed
1.7.3 新增bl_shape_alpha
1 parent af5d5c2 commit 3683cfa

File tree

7 files changed

+71
-2
lines changed

7 files changed

+71
-2
lines changed

BackgroundLibrary.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
<option name="XML" value="true" />
66
</context>
77
</template>
8+
<template name="bl_shape_alpha" value="app:bl_shape_alpha=&quot;$value$&quot;" description="shape透明度" toReformat="false" toShortenFQNames="true">
9+
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
10+
<context>
11+
<option name="XML" value="true" />
12+
</context>
13+
</template>
814
<template name="bl_solid_color" value="app:bl_solid_color=&quot;$value$&quot;" description="填充颜色" toReformat="true" toShortenFQNames="true">
915
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
1016
<context>

library/src/main/java/com/noober/background/BackgroundFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,18 @@ private static void setBackground(Drawable drawable, View view, TypedArray typed
301301
((LayerDrawable) drawable).setLayerInset(0, (int) leftValue, (int) topValue, (int) rightValue, (int) bottomValue);
302302
}
303303

304+
if(typedArray.hasValue(R.styleable.background_bl_shape_alpha)){
305+
float alpha = typedArray.getFloat(R.styleable.background_bl_shape_alpha, 0f);
306+
if(alpha >= 1){
307+
alpha = 255;
308+
}else if(alpha <= 0){
309+
alpha = 0;
310+
}else {
311+
alpha = alpha * 255;
312+
}
313+
drawable.setAlpha((int) alpha);
314+
}
315+
304316
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
305317
view.setBackground(drawable);
306318
} else {

library/src/main/java/com/noober/background/drawable/DrawableCreator.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public static class Builder {
139139
private Integer unPressedTextColor;
140140
private Integer unFocusedTextColor;
141141
private int textColorCount;
142+
private float alpha = -1;
142143

143144
private boolean hasSelectDrawable = false;
144145

@@ -150,6 +151,11 @@ public Builder setShape(Shape shape) {
150151
return this;
151152
}
152153

154+
public Builder setShapeAlpha(float alpha) {
155+
this.alpha = alpha;
156+
return this;
157+
}
158+
153159
public Builder setSolidColor(int solidColor) {
154160
this.solidColor = solidColor;
155161
return this;
@@ -528,8 +534,19 @@ public Drawable build() {
528534
return resultDrawable;
529535
}
530536
}
537+
Drawable result = drawable == null ? stateListDrawable : drawable;
531538

532-
return drawable == null ? stateListDrawable : drawable;
539+
if(alpha != -1){
540+
if(alpha >= 1){
541+
alpha = 255;
542+
}else if(alpha <= 0){
543+
alpha = 0;
544+
}else {
545+
alpha = alpha * 255;
546+
}
547+
result.setAlpha((int) alpha);
548+
}
549+
return result;
533550
}
534551

535552
public ColorStateList buildTextColor() {

library/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
<enum name="ring" value="3" />
4545
</attr>
4646

47+
<attr name="bl_shape_alpha" format="float" />
48+
4749
<attr name="bl_solid_color" format="color" />
4850

4951
<attr name="bl_corners_radius" format="dimension" />

libraryx/src/main/java/com/noober/background/BackgroundFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,18 @@ private static void setBackground(Drawable drawable, View view, TypedArray typed
304304
((LayerDrawable) drawable).setLayerInset(0, (int) leftValue, (int) topValue, (int) rightValue, (int) bottomValue);
305305
}
306306

307+
if(typedArray.hasValue(R.styleable.background_bl_shape_alpha)){
308+
float alpha = typedArray.getFloat(R.styleable.background_bl_shape_alpha, 0f);
309+
if(alpha >= 1){
310+
alpha = 255;
311+
}else if(alpha <= 0){
312+
alpha = 0;
313+
}else {
314+
alpha = alpha * 255;
315+
}
316+
drawable.setAlpha((int) alpha);
317+
}
318+
307319
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
308320
view.setBackground(drawable);
309321
} else {

libraryx/src/main/java/com/noober/background/drawable/DrawableCreator.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public static class Builder {
137137
private Integer unPressedTextColor;
138138
private Integer unFocusedTextColor;
139139
private int textColorCount;
140+
private float alpha = -1;
140141

141142
private boolean hasSelectDrawable = false;
142143

@@ -148,6 +149,11 @@ public Builder setShape(Shape shape) {
148149
return this;
149150
}
150151

152+
public Builder setShapeAlpha(float alpha) {
153+
this.alpha = alpha;
154+
return this;
155+
}
156+
151157
public Builder setSolidColor(int solidColor) {
152158
this.solidColor = solidColor;
153159
return this;
@@ -527,7 +533,19 @@ public Drawable build() {
527533
}
528534
}
529535

530-
return drawable == null ? stateListDrawable : drawable;
536+
Drawable result = drawable == null ? stateListDrawable : drawable;
537+
538+
if(alpha != -1){
539+
if(alpha >= 1){
540+
alpha = 255;
541+
}else if(alpha <= 0){
542+
alpha = 0;
543+
}else {
544+
alpha = alpha * 255;
545+
}
546+
result.setAlpha((int) alpha);
547+
}
548+
return result;
531549
}
532550

533551
public ColorStateList buildTextColor() {

libraryx/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<enum name="ring" value="3" />
1010
</attr>
1111

12+
<attr name="bl_shape_alpha" format="float" />
13+
1214
<attr name="bl_solid_color" format="color" />
1315

1416
<attr name="bl_corners_radius" format="dimension" />

0 commit comments

Comments
 (0)