Skip to content

Commit a9635df

Browse files
committed
部分属性兼容sdk21以下的手机
1 parent e927a97 commit a9635df

9 files changed

Lines changed: 217 additions & 8 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
/build
99
/captures
1010
.externalNativeBuild
11+
.idea/

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ private static View setViewBackground(String name, Context context, AttributeSet
8484
TypedArray animTa = context.obtainStyledAttributes(attrs, R.styleable.bl_anim);
8585
TypedArray multiSelTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector);
8686
TypedArray multiTextTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector_text);
87+
TypedArray selectorPre21Ta = null;
88+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
89+
selectorPre21Ta = context.obtainStyledAttributes(attrs, R.styleable.background_selector_pre_21);
90+
}
8791

8892
try {
8993
if (typedArray.getIndexCount() == 0 && selectorTa.getIndexCount() == 0 && pressTa.getIndexCount() == 0
@@ -123,8 +127,13 @@ private static View setViewBackground(String name, Context context, AttributeSet
123127
stateListDrawable = DrawableFactory.getMultiSelectorDrawable(context, multiSelTa, typedArray);
124128
setBackground(stateListDrawable, view, typedArray);
125129
} else if (typedArray.getIndexCount() > 0) {
126-
drawable = DrawableFactory.getDrawable(typedArray);
127-
setDrawable(drawable, view, otherTa, typedArray);
130+
if (selectorPre21Ta != null && selectorPre21Ta.getIndexCount() > 0) {
131+
stateListDrawable = DrawableFactory.getSelectorPre21Drawable(typedArray);
132+
setDrawable(stateListDrawable, view, otherTa, typedArray);
133+
} else {
134+
drawable = DrawableFactory.getDrawable(typedArray);
135+
setDrawable(drawable, view, otherTa, typedArray);
136+
}
128137
} else if (animTa.getIndexCount() > 0) {
129138
AnimationDrawable animationDrawable = DrawableFactory.getAnimationDrawable(animTa);
130139
setBackground(animationDrawable, view, typedArray);
@@ -195,6 +204,9 @@ public void onClick(View view) {
195204
animTa.recycle();
196205
multiSelTa.recycle();
197206
multiTextTa.recycle();
207+
if (selectorPre21Ta != null) {
208+
selectorPre21Ta.recycle();
209+
}
198210
}
199211
}
200212

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public static StateListDrawable getSelectorDrawable(TypedArray typedArray, Typed
2424
return new SelectorDrawableCreator(typedArray, selectorTa).create();
2525
}
2626

27+
//针对sdk21以前获取selector属性的drawable
28+
public static StateListDrawable getSelectorPre21Drawable(TypedArray typedArray) throws Exception {
29+
return new SelectorPre21DrawableCreator(typedArray).create();
30+
}
31+
2732
//获取button 属性的drawable
2833
public static StateListDrawable getButtonDrawable(TypedArray typedArray, TypedArray buttonTa) throws Exception {
2934
return new ButtonDrawableCreator(typedArray, buttonTa).create();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.noober.background.drawable;
2+
3+
import android.content.res.TypedArray;
4+
import android.graphics.drawable.GradientDrawable;
5+
import android.graphics.drawable.StateListDrawable;
6+
import android.support.annotation.AttrRes;
7+
import android.support.annotation.StyleableRes;
8+
9+
import com.noober.background.R;
10+
11+
public class SelectorPre21DrawableCreator implements ICreateDrawable {
12+
13+
private TypedArray typedArray;
14+
15+
@SuppressWarnings("WeakerAccess")
16+
public SelectorPre21DrawableCreator(TypedArray typedArray) {
17+
this.typedArray = typedArray;
18+
}
19+
20+
@Override
21+
public StateListDrawable create() throws Exception {
22+
StateListDrawable stateListDrawable = new StateListDrawable();
23+
24+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_checkable_solid_color, R.styleable.background_bl_checkable_stroke_color, android.R.attr.state_checkable);
25+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unCheckable_solid_color, R.styleable.background_bl_unCheckable_stroke_color, -android.R.attr.state_checkable);
26+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_checked_solid_color, R.styleable.background_bl_checked_stroke_color, android.R.attr.state_checked);
27+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unChecked_solid_color, R.styleable.background_bl_unChecked_stroke_color, -android.R.attr.state_checked);
28+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_enabled_solid_color, R.styleable.background_bl_enabled_stroke_color, android.R.attr.state_enabled);
29+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unEnabled_solid_color, R.styleable.background_bl_unEnabled_stroke_color, -android.R.attr.state_enabled);
30+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_selected_solid_color, R.styleable.background_bl_selected_stroke_color, android.R.attr.state_selected);
31+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unSelected_solid_color, R.styleable.background_bl_unSelected_stroke_color, -android.R.attr.state_selected);
32+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_pressed_solid_color, R.styleable.background_bl_pressed_stroke_color, android.R.attr.state_pressed);
33+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unPressed_solid_color, R.styleable.background_bl_unPressed_stroke_color, -android.R.attr.state_pressed);
34+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_focused_solid_color, R.styleable.background_bl_focused_stroke_color, android.R.attr.state_focused);
35+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unFocused_solid_color, R.styleable.background_bl_unFocused_stroke_color, -android.R.attr.state_focused);
36+
37+
return stateListDrawable;
38+
}
39+
40+
private void setSelectorDrawable(StateListDrawable stateListDrawable, @StyleableRes int solidAttr, @StyleableRes int strokeAttr, @AttrRes int functionId) throws Exception {
41+
if (typedArray.hasValue(solidAttr) || typedArray.hasValue(strokeAttr)) {
42+
GradientDrawable tmpDrawable = DrawableFactory.getDrawable(typedArray);
43+
if (typedArray.hasValue(solidAttr)) {
44+
tmpDrawable.setColor(typedArray.getColor(solidAttr, 0));
45+
}
46+
if (typedArray.hasValue(strokeAttr)) {
47+
int strokeWidth = typedArray.getDimensionPixelSize(R.styleable.background_bl_stroke_width, 0);
48+
int strokeColor = typedArray.getColor(strokeAttr, 0);
49+
float strokeDashWidth = typedArray.getDimension(R.styleable.background_bl_stroke_dashWidth, 0f);
50+
float strokeGap = typedArray.getDimension(R.styleable.background_bl_stroke_dashGap, 0f);
51+
tmpDrawable.setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeGap);
52+
}
53+
stateListDrawable.addState(new int[]{functionId}, tmpDrawable);
54+
}
55+
}
56+
57+
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,36 @@
126126
</attr>
127127
</declare-styleable>
128128

129+
<declare-styleable name="background_selector_pre_21">
130+
<attr name="bl_checkable_stroke_color" />
131+
<attr name="bl_checked_stroke_color" />
132+
<attr name="bl_enabled_stroke_color" />
133+
<attr name="bl_selected_stroke_color" />
134+
<attr name="bl_pressed_stroke_color" />
135+
<attr name="bl_focused_stroke_color" />
136+
137+
<attr name="bl_unCheckable_stroke_color" />
138+
<attr name="bl_unChecked_stroke_color" />
139+
<attr name="bl_unEnabled_stroke_color" />
140+
<attr name="bl_unSelected_stroke_color" />
141+
<attr name="bl_unPressed_stroke_color" />
142+
<attr name="bl_unFocused_stroke_color" />
143+
144+
<attr name="bl_checkable_solid_color" />
145+
<attr name="bl_checked_solid_color" />
146+
<attr name="bl_enabled_solid_color" />
147+
<attr name="bl_selected_solid_color" />
148+
<attr name="bl_pressed_solid_color" />
149+
<attr name="bl_focused_solid_color" />
150+
151+
<attr name="bl_unCheckable_solid_color" />
152+
<attr name="bl_unChecked_solid_color" />
153+
<attr name="bl_unEnabled_solid_color" />
154+
<attr name="bl_unSelected_solid_color" />
155+
<attr name="bl_unPressed_solid_color" />
156+
<attr name="bl_unFocused_solid_color" />
157+
</declare-styleable>
158+
129159
<declare-styleable name="background_press">
130160
<attr name="bl_unpressed_color" format="color" />
131161
<attr name="bl_pressed_color" format="color" />

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@
1919
import android.widget.CompoundButton;
2020
import android.widget.TextView;
2121

22-
import androidx.annotation.Nullable;
23-
import androidx.collection.ArrayMap;
24-
2522
import com.noober.background.drawable.DrawableFactory;
26-
import com.noober.background.view.Const;
2723

2824
import java.lang.reflect.Constructor;
2925
import java.lang.reflect.InvocationTargetException;
3026
import java.lang.reflect.Method;
3127
import java.util.HashMap;
3228
import java.util.Map;
3329

30+
import androidx.annotation.Nullable;
31+
import androidx.collection.ArrayMap;
32+
3433
public class BackgroundFactory implements LayoutInflater.Factory2 {
3534

3635
private LayoutInflater.Factory mViewCreateFactory;
@@ -85,6 +84,10 @@ private static View setViewBackground(String name, Context context, AttributeSet
8584
TypedArray animTa = context.obtainStyledAttributes(attrs, R.styleable.bl_anim);
8685
TypedArray multiSelTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector);
8786
TypedArray multiTextTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector_text);
87+
TypedArray selectorPre21Ta = null;
88+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
89+
selectorPre21Ta = context.obtainStyledAttributes(attrs, R.styleable.background_selector_pre_21);
90+
}
8891

8992
try {
9093
if (typedArray.getIndexCount() == 0 && selectorTa.getIndexCount() == 0 && pressTa.getIndexCount() == 0
@@ -124,8 +127,13 @@ private static View setViewBackground(String name, Context context, AttributeSet
124127
stateListDrawable = DrawableFactory.getMultiSelectorDrawable(context, multiSelTa, typedArray);
125128
setBackground(stateListDrawable, view, typedArray);
126129
} else if(typedArray.getIndexCount() > 0){
127-
drawable = DrawableFactory.getDrawable(typedArray);
128-
setDrawable(drawable, view, otherTa, typedArray);
130+
if (selectorPre21Ta != null && selectorPre21Ta.getIndexCount() > 0) {
131+
stateListDrawable = DrawableFactory.getSelectorPre21Drawable(typedArray);
132+
setDrawable(stateListDrawable, view, otherTa, typedArray);
133+
} else {
134+
drawable = DrawableFactory.getDrawable(typedArray);
135+
setDrawable(drawable, view, otherTa, typedArray);
136+
}
129137
} else if(animTa.getIndexCount() > 0){
130138
AnimationDrawable animationDrawable = DrawableFactory.getAnimationDrawable(animTa);
131139
setBackground(animationDrawable, view, typedArray);
@@ -196,6 +204,9 @@ public void onClick(View view) {
196204
animTa.recycle();
197205
multiSelTa.recycle();
198206
multiTextTa.recycle();
207+
if (selectorPre21Ta != null) {
208+
selectorPre21Ta.recycle();
209+
}
199210
}
200211
}
201212

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public static StateListDrawable getSelectorDrawable(TypedArray typedArray, Typed
2424
return (StateListDrawable) new SelectorDrawableCreator(typedArray, selectorTa).create();
2525
}
2626

27+
//针对sdk21以前获取selector属性的drawable
28+
public static StateListDrawable getSelectorPre21Drawable(TypedArray typedArray) throws Exception {
29+
return new SelectorPre21DrawableCreator(typedArray).create();
30+
}
31+
2732
//获取button 属性的drawable
2833
public static StateListDrawable getButtonDrawable(TypedArray typedArray, TypedArray buttonTa) throws Exception {
2934
return (StateListDrawable) new ButtonDrawableCreator(typedArray, buttonTa).create();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.noober.background.drawable;
2+
3+
import android.content.res.TypedArray;
4+
import android.graphics.drawable.GradientDrawable;
5+
import android.graphics.drawable.StateListDrawable;
6+
7+
import com.noober.background.R;
8+
9+
import androidx.annotation.AttrRes;
10+
import androidx.annotation.StyleableRes;
11+
12+
public class SelectorPre21DrawableCreator implements ICreateDrawable {
13+
14+
private TypedArray typedArray;
15+
16+
@SuppressWarnings("WeakerAccess")
17+
public SelectorPre21DrawableCreator(TypedArray typedArray) {
18+
this.typedArray = typedArray;
19+
}
20+
21+
@Override
22+
public StateListDrawable create() throws Exception {
23+
StateListDrawable stateListDrawable = new StateListDrawable();
24+
25+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_checkable_solid_color, R.styleable.background_bl_checkable_stroke_color, android.R.attr.state_checkable);
26+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unCheckable_solid_color, R.styleable.background_bl_unCheckable_stroke_color, -android.R.attr.state_checkable);
27+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_checked_solid_color, R.styleable.background_bl_checked_stroke_color, android.R.attr.state_checked);
28+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unChecked_solid_color, R.styleable.background_bl_unChecked_stroke_color, -android.R.attr.state_checked);
29+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_enabled_solid_color, R.styleable.background_bl_enabled_stroke_color, android.R.attr.state_enabled);
30+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unEnabled_solid_color, R.styleable.background_bl_unEnabled_stroke_color, -android.R.attr.state_enabled);
31+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_selected_solid_color, R.styleable.background_bl_selected_stroke_color, android.R.attr.state_selected);
32+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unSelected_solid_color, R.styleable.background_bl_unSelected_stroke_color, -android.R.attr.state_selected);
33+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_pressed_solid_color, R.styleable.background_bl_pressed_stroke_color, android.R.attr.state_pressed);
34+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unPressed_solid_color, R.styleable.background_bl_unPressed_stroke_color, -android.R.attr.state_pressed);
35+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_focused_solid_color, R.styleable.background_bl_focused_stroke_color, android.R.attr.state_focused);
36+
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unFocused_solid_color, R.styleable.background_bl_unFocused_stroke_color, -android.R.attr.state_focused);
37+
38+
return stateListDrawable;
39+
}
40+
41+
private void setSelectorDrawable(StateListDrawable stateListDrawable, @StyleableRes int solidAttr, @StyleableRes int strokeAttr, @AttrRes int functionId) throws Exception {
42+
if (typedArray.hasValue(solidAttr) || typedArray.hasValue(strokeAttr)) {
43+
GradientDrawable tmpDrawable = DrawableFactory.getDrawable(typedArray);
44+
if (typedArray.hasValue(solidAttr)) {
45+
tmpDrawable.setColor(typedArray.getColor(solidAttr, 0));
46+
}
47+
if (typedArray.hasValue(strokeAttr)) {
48+
int strokeWidth = typedArray.getDimensionPixelSize(R.styleable.background_bl_stroke_width, 0);
49+
int strokeColor = typedArray.getColor(strokeAttr, 0);
50+
float strokeDashWidth = typedArray.getDimension(R.styleable.background_bl_stroke_dashWidth, 0f);
51+
float strokeGap = typedArray.getDimension(R.styleable.background_bl_stroke_dashGap, 0f);
52+
tmpDrawable.setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeGap);
53+
}
54+
stateListDrawable.addState(new int[]{functionId}, tmpDrawable);
55+
}
56+
}
57+
58+
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,36 @@
126126
</attr>
127127
</declare-styleable>
128128

129+
<declare-styleable name="background_selector_pre_21">
130+
<attr name="bl_checkable_stroke_color" />
131+
<attr name="bl_checked_stroke_color" />
132+
<attr name="bl_enabled_stroke_color" />
133+
<attr name="bl_selected_stroke_color" />
134+
<attr name="bl_pressed_stroke_color" />
135+
<attr name="bl_focused_stroke_color" />
136+
137+
<attr name="bl_unCheckable_stroke_color" />
138+
<attr name="bl_unChecked_stroke_color" />
139+
<attr name="bl_unEnabled_stroke_color" />
140+
<attr name="bl_unSelected_stroke_color" />
141+
<attr name="bl_unPressed_stroke_color" />
142+
<attr name="bl_unFocused_stroke_color" />
143+
144+
<attr name="bl_checkable_solid_color" />
145+
<attr name="bl_checked_solid_color" />
146+
<attr name="bl_enabled_solid_color" />
147+
<attr name="bl_selected_solid_color" />
148+
<attr name="bl_pressed_solid_color" />
149+
<attr name="bl_focused_solid_color" />
150+
151+
<attr name="bl_unCheckable_solid_color" />
152+
<attr name="bl_unChecked_solid_color" />
153+
<attr name="bl_unEnabled_solid_color" />
154+
<attr name="bl_unSelected_solid_color" />
155+
<attr name="bl_unPressed_solid_color" />
156+
<attr name="bl_unFocused_solid_color" />
157+
</declare-styleable>
158+
129159
<declare-styleable name="background_press">
130160
<attr name="bl_unpressed_color" format="color" />
131161
<attr name="bl_pressed_color" format="color" />

0 commit comments

Comments
 (0)