Skip to content

Commit

Permalink
Merge pull request JavaNoober#89 from xxjy/master
Browse files Browse the repository at this point in the history
部分属性兼容sdk21以下
  • Loading branch information
JavaNoober authored Nov 15, 2019
2 parents e927a97 + a9635df commit f4bd061
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/build
/captures
.externalNativeBuild
.idea/
16 changes: 14 additions & 2 deletions library/src/main/java/com/noober/background/BackgroundFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ private static View setViewBackground(String name, Context context, AttributeSet
TypedArray animTa = context.obtainStyledAttributes(attrs, R.styleable.bl_anim);
TypedArray multiSelTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector);
TypedArray multiTextTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector_text);
TypedArray selectorPre21Ta = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
selectorPre21Ta = context.obtainStyledAttributes(attrs, R.styleable.background_selector_pre_21);
}

try {
if (typedArray.getIndexCount() == 0 && selectorTa.getIndexCount() == 0 && pressTa.getIndexCount() == 0
Expand Down Expand Up @@ -123,8 +127,13 @@ private static View setViewBackground(String name, Context context, AttributeSet
stateListDrawable = DrawableFactory.getMultiSelectorDrawable(context, multiSelTa, typedArray);
setBackground(stateListDrawable, view, typedArray);
} else if (typedArray.getIndexCount() > 0) {
drawable = DrawableFactory.getDrawable(typedArray);
setDrawable(drawable, view, otherTa, typedArray);
if (selectorPre21Ta != null && selectorPre21Ta.getIndexCount() > 0) {
stateListDrawable = DrawableFactory.getSelectorPre21Drawable(typedArray);
setDrawable(stateListDrawable, view, otherTa, typedArray);
} else {
drawable = DrawableFactory.getDrawable(typedArray);
setDrawable(drawable, view, otherTa, typedArray);
}
} else if (animTa.getIndexCount() > 0) {
AnimationDrawable animationDrawable = DrawableFactory.getAnimationDrawable(animTa);
setBackground(animationDrawable, view, typedArray);
Expand Down Expand Up @@ -195,6 +204,9 @@ public void onClick(View view) {
animTa.recycle();
multiSelTa.recycle();
multiTextTa.recycle();
if (selectorPre21Ta != null) {
selectorPre21Ta.recycle();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static StateListDrawable getSelectorDrawable(TypedArray typedArray, Typed
return new SelectorDrawableCreator(typedArray, selectorTa).create();
}

//针对sdk21以前获取selector属性的drawable
public static StateListDrawable getSelectorPre21Drawable(TypedArray typedArray) throws Exception {
return new SelectorPre21DrawableCreator(typedArray).create();
}

//获取button 属性的drawable
public static StateListDrawable getButtonDrawable(TypedArray typedArray, TypedArray buttonTa) throws Exception {
return new ButtonDrawableCreator(typedArray, buttonTa).create();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.noober.background.drawable;

import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.AttrRes;
import android.support.annotation.StyleableRes;

import com.noober.background.R;

public class SelectorPre21DrawableCreator implements ICreateDrawable {

private TypedArray typedArray;

@SuppressWarnings("WeakerAccess")
public SelectorPre21DrawableCreator(TypedArray typedArray) {
this.typedArray = typedArray;
}

@Override
public StateListDrawable create() throws Exception {
StateListDrawable stateListDrawable = new StateListDrawable();

setSelectorDrawable(stateListDrawable, R.styleable.background_bl_checkable_solid_color, R.styleable.background_bl_checkable_stroke_color, android.R.attr.state_checkable);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unCheckable_solid_color, R.styleable.background_bl_unCheckable_stroke_color, -android.R.attr.state_checkable);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_checked_solid_color, R.styleable.background_bl_checked_stroke_color, android.R.attr.state_checked);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unChecked_solid_color, R.styleable.background_bl_unChecked_stroke_color, -android.R.attr.state_checked);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_enabled_solid_color, R.styleable.background_bl_enabled_stroke_color, android.R.attr.state_enabled);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unEnabled_solid_color, R.styleable.background_bl_unEnabled_stroke_color, -android.R.attr.state_enabled);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_selected_solid_color, R.styleable.background_bl_selected_stroke_color, android.R.attr.state_selected);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unSelected_solid_color, R.styleable.background_bl_unSelected_stroke_color, -android.R.attr.state_selected);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_pressed_solid_color, R.styleable.background_bl_pressed_stroke_color, android.R.attr.state_pressed);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unPressed_solid_color, R.styleable.background_bl_unPressed_stroke_color, -android.R.attr.state_pressed);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_focused_solid_color, R.styleable.background_bl_focused_stroke_color, android.R.attr.state_focused);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unFocused_solid_color, R.styleable.background_bl_unFocused_stroke_color, -android.R.attr.state_focused);

return stateListDrawable;
}

private void setSelectorDrawable(StateListDrawable stateListDrawable, @StyleableRes int solidAttr, @StyleableRes int strokeAttr, @AttrRes int functionId) throws Exception {
if (typedArray.hasValue(solidAttr) || typedArray.hasValue(strokeAttr)) {
GradientDrawable tmpDrawable = DrawableFactory.getDrawable(typedArray);
if (typedArray.hasValue(solidAttr)) {
tmpDrawable.setColor(typedArray.getColor(solidAttr, 0));
}
if (typedArray.hasValue(strokeAttr)) {
int strokeWidth = typedArray.getDimensionPixelSize(R.styleable.background_bl_stroke_width, 0);
int strokeColor = typedArray.getColor(strokeAttr, 0);
float strokeDashWidth = typedArray.getDimension(R.styleable.background_bl_stroke_dashWidth, 0f);
float strokeGap = typedArray.getDimension(R.styleable.background_bl_stroke_dashGap, 0f);
tmpDrawable.setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeGap);
}
stateListDrawable.addState(new int[]{functionId}, tmpDrawable);
}
}

}
30 changes: 30 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@
</attr>
</declare-styleable>

<declare-styleable name="background_selector_pre_21">
<attr name="bl_checkable_stroke_color" />
<attr name="bl_checked_stroke_color" />
<attr name="bl_enabled_stroke_color" />
<attr name="bl_selected_stroke_color" />
<attr name="bl_pressed_stroke_color" />
<attr name="bl_focused_stroke_color" />

<attr name="bl_unCheckable_stroke_color" />
<attr name="bl_unChecked_stroke_color" />
<attr name="bl_unEnabled_stroke_color" />
<attr name="bl_unSelected_stroke_color" />
<attr name="bl_unPressed_stroke_color" />
<attr name="bl_unFocused_stroke_color" />

<attr name="bl_checkable_solid_color" />
<attr name="bl_checked_solid_color" />
<attr name="bl_enabled_solid_color" />
<attr name="bl_selected_solid_color" />
<attr name="bl_pressed_solid_color" />
<attr name="bl_focused_solid_color" />

<attr name="bl_unCheckable_solid_color" />
<attr name="bl_unChecked_solid_color" />
<attr name="bl_unEnabled_solid_color" />
<attr name="bl_unSelected_solid_color" />
<attr name="bl_unPressed_solid_color" />
<attr name="bl_unFocused_solid_color" />
</declare-styleable>

<declare-styleable name="background_press">
<attr name="bl_unpressed_color" format="color" />
<attr name="bl_pressed_color" format="color" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.collection.ArrayMap;

import com.noober.background.drawable.DrawableFactory;
import com.noober.background.view.Const;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import androidx.annotation.Nullable;
import androidx.collection.ArrayMap;

public class BackgroundFactory implements LayoutInflater.Factory2 {

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

try {
if (typedArray.getIndexCount() == 0 && selectorTa.getIndexCount() == 0 && pressTa.getIndexCount() == 0
Expand Down Expand Up @@ -124,8 +127,13 @@ private static View setViewBackground(String name, Context context, AttributeSet
stateListDrawable = DrawableFactory.getMultiSelectorDrawable(context, multiSelTa, typedArray);
setBackground(stateListDrawable, view, typedArray);
} else if(typedArray.getIndexCount() > 0){
drawable = DrawableFactory.getDrawable(typedArray);
setDrawable(drawable, view, otherTa, typedArray);
if (selectorPre21Ta != null && selectorPre21Ta.getIndexCount() > 0) {
stateListDrawable = DrawableFactory.getSelectorPre21Drawable(typedArray);
setDrawable(stateListDrawable, view, otherTa, typedArray);
} else {
drawable = DrawableFactory.getDrawable(typedArray);
setDrawable(drawable, view, otherTa, typedArray);
}
} else if(animTa.getIndexCount() > 0){
AnimationDrawable animationDrawable = DrawableFactory.getAnimationDrawable(animTa);
setBackground(animationDrawable, view, typedArray);
Expand Down Expand Up @@ -196,6 +204,9 @@ public void onClick(View view) {
animTa.recycle();
multiSelTa.recycle();
multiTextTa.recycle();
if (selectorPre21Ta != null) {
selectorPre21Ta.recycle();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static StateListDrawable getSelectorDrawable(TypedArray typedArray, Typed
return (StateListDrawable) new SelectorDrawableCreator(typedArray, selectorTa).create();
}

//针对sdk21以前获取selector属性的drawable
public static StateListDrawable getSelectorPre21Drawable(TypedArray typedArray) throws Exception {
return new SelectorPre21DrawableCreator(typedArray).create();
}

//获取button 属性的drawable
public static StateListDrawable getButtonDrawable(TypedArray typedArray, TypedArray buttonTa) throws Exception {
return (StateListDrawable) new ButtonDrawableCreator(typedArray, buttonTa).create();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.noober.background.drawable;

import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;

import com.noober.background.R;

import androidx.annotation.AttrRes;
import androidx.annotation.StyleableRes;

public class SelectorPre21DrawableCreator implements ICreateDrawable {

private TypedArray typedArray;

@SuppressWarnings("WeakerAccess")
public SelectorPre21DrawableCreator(TypedArray typedArray) {
this.typedArray = typedArray;
}

@Override
public StateListDrawable create() throws Exception {
StateListDrawable stateListDrawable = new StateListDrawable();

setSelectorDrawable(stateListDrawable, R.styleable.background_bl_checkable_solid_color, R.styleable.background_bl_checkable_stroke_color, android.R.attr.state_checkable);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unCheckable_solid_color, R.styleable.background_bl_unCheckable_stroke_color, -android.R.attr.state_checkable);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_checked_solid_color, R.styleable.background_bl_checked_stroke_color, android.R.attr.state_checked);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unChecked_solid_color, R.styleable.background_bl_unChecked_stroke_color, -android.R.attr.state_checked);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_enabled_solid_color, R.styleable.background_bl_enabled_stroke_color, android.R.attr.state_enabled);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unEnabled_solid_color, R.styleable.background_bl_unEnabled_stroke_color, -android.R.attr.state_enabled);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_selected_solid_color, R.styleable.background_bl_selected_stroke_color, android.R.attr.state_selected);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unSelected_solid_color, R.styleable.background_bl_unSelected_stroke_color, -android.R.attr.state_selected);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_pressed_solid_color, R.styleable.background_bl_pressed_stroke_color, android.R.attr.state_pressed);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unPressed_solid_color, R.styleable.background_bl_unPressed_stroke_color, -android.R.attr.state_pressed);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_focused_solid_color, R.styleable.background_bl_focused_stroke_color, android.R.attr.state_focused);
setSelectorDrawable(stateListDrawable, R.styleable.background_bl_unFocused_solid_color, R.styleable.background_bl_unFocused_stroke_color, -android.R.attr.state_focused);

return stateListDrawable;
}

private void setSelectorDrawable(StateListDrawable stateListDrawable, @StyleableRes int solidAttr, @StyleableRes int strokeAttr, @AttrRes int functionId) throws Exception {
if (typedArray.hasValue(solidAttr) || typedArray.hasValue(strokeAttr)) {
GradientDrawable tmpDrawable = DrawableFactory.getDrawable(typedArray);
if (typedArray.hasValue(solidAttr)) {
tmpDrawable.setColor(typedArray.getColor(solidAttr, 0));
}
if (typedArray.hasValue(strokeAttr)) {
int strokeWidth = typedArray.getDimensionPixelSize(R.styleable.background_bl_stroke_width, 0);
int strokeColor = typedArray.getColor(strokeAttr, 0);
float strokeDashWidth = typedArray.getDimension(R.styleable.background_bl_stroke_dashWidth, 0f);
float strokeGap = typedArray.getDimension(R.styleable.background_bl_stroke_dashGap, 0f);
tmpDrawable.setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeGap);
}
stateListDrawable.addState(new int[]{functionId}, tmpDrawable);
}
}

}
30 changes: 30 additions & 0 deletions libraryx/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@
</attr>
</declare-styleable>

<declare-styleable name="background_selector_pre_21">
<attr name="bl_checkable_stroke_color" />
<attr name="bl_checked_stroke_color" />
<attr name="bl_enabled_stroke_color" />
<attr name="bl_selected_stroke_color" />
<attr name="bl_pressed_stroke_color" />
<attr name="bl_focused_stroke_color" />

<attr name="bl_unCheckable_stroke_color" />
<attr name="bl_unChecked_stroke_color" />
<attr name="bl_unEnabled_stroke_color" />
<attr name="bl_unSelected_stroke_color" />
<attr name="bl_unPressed_stroke_color" />
<attr name="bl_unFocused_stroke_color" />

<attr name="bl_checkable_solid_color" />
<attr name="bl_checked_solid_color" />
<attr name="bl_enabled_solid_color" />
<attr name="bl_selected_solid_color" />
<attr name="bl_pressed_solid_color" />
<attr name="bl_focused_solid_color" />

<attr name="bl_unCheckable_solid_color" />
<attr name="bl_unChecked_solid_color" />
<attr name="bl_unEnabled_solid_color" />
<attr name="bl_unSelected_solid_color" />
<attr name="bl_unPressed_solid_color" />
<attr name="bl_unFocused_solid_color" />
</declare-styleable>

<declare-styleable name="background_press">
<attr name="bl_unpressed_color" format="color" />
<attr name="bl_pressed_color" format="color" />
Expand Down

0 comments on commit f4bd061

Please sign in to comment.