|
| 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 | +} |
0 commit comments