forked from JavaNoober/BackgroundLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request JavaNoober#89 from xxjy/master
部分属性兼容sdk21以下
- Loading branch information
Showing
9 changed files
with
217 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
/build | ||
/captures | ||
.externalNativeBuild | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
library/src/main/java/com/noober/background/drawable/SelectorPre21DrawableCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
libraryx/src/main/java/com/noober/background/drawable/SelectorPre21DrawableCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters