android.graphics.drawable.StateListDrawable Java Examples

The following examples show how to use android.graphics.drawable.StateListDrawable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PCornerUtils.java    From YImagePicker with Apache License 2.0 6 votes vote down vote up
/**
 * set btn selector with corner drawable for special position
 */
public static StateListDrawable btnSelector(float radius, int normalColor, int pressColor, int postion) {
    StateListDrawable bg = new StateListDrawable();
    Drawable normal = null;
    Drawable pressed = null;

    if (postion == 0) {// left btn
        normal = cornerDrawable(normalColor, new float[]{0, 0, 0, 0, 0, 0, radius, radius});
        pressed = cornerDrawable(pressColor, new float[]{0, 0, 0, 0, 0, 0, radius, radius});
    } else if (postion == 1) {// right btn
        normal = cornerDrawable(normalColor, new float[]{0, 0, 0, 0, radius, radius, 0, 0});
        pressed = cornerDrawable(pressColor, new float[]{0, 0, 0, 0, radius, radius, 0, 0});
    } else if (postion == -1) {// only one btn
        normal = cornerDrawable(normalColor, new float[]{0, 0, 0, 0, radius, radius, radius, radius});
        pressed = cornerDrawable(pressColor, new float[]{0, 0, 0, 0, radius, radius, radius, radius});
    } else if (postion == -2) {// for material dialog
        normal = cornerDrawable(normalColor, radius);
        pressed = cornerDrawable(pressColor, radius);
    }

    bg.addState(new int[]{-android.R.attr.state_pressed}, normal);
    bg.addState(new int[]{android.R.attr.state_pressed}, pressed);
    return bg;
}
 
Example #2
Source File: CornerUtils.java    From SprintNBA with Apache License 2.0 6 votes vote down vote up
/**
 * set btn selector with corner drawable for special position
 */
public static StateListDrawable btnSelector(float radius, int normalColor, int pressColor, int postion) {
    StateListDrawable bg = new StateListDrawable();
    Drawable normal = null;
    Drawable pressed = null;

    if (postion == 0) {// left btn
        normal = cornerDrawable(normalColor, new float[]{0, 0, 0, 0, 0, 0, radius, radius});
        pressed = cornerDrawable(pressColor, new float[]{0, 0, 0, 0, 0, 0, radius, radius});
    } else if (postion == 1) {// right btn
        normal = cornerDrawable(normalColor, new float[]{0, 0, 0, 0, radius, radius, 0, 0});
        pressed = cornerDrawable(pressColor, new float[]{0, 0, 0, 0, radius, radius, 0, 0});
    } else if (postion == -1) {// only one btn
        normal = cornerDrawable(normalColor, new float[]{0, 0, 0, 0, radius, radius, radius, radius});
        pressed = cornerDrawable(pressColor, new float[]{0, 0, 0, 0, radius, radius, radius, radius});
    } else if (postion == -2) {// for material dialog
        normal = cornerDrawable(normalColor, radius);
        pressed = cornerDrawable(pressColor, radius);
    }

    bg.addState(new int[]{-android.R.attr.state_pressed}, normal);
    bg.addState(new int[]{android.R.attr.state_pressed}, pressed);
    return bg;
}
 
Example #3
Source File: CustomFastScroller.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
CustomFastScroller(RecyclerView recyclerView, StateListDrawable verticalThumbDrawable,
             Drawable verticalTrackDrawable, StateListDrawable horizontalThumbDrawable,
             Drawable horizontalTrackDrawable, int defaultWidth, int scrollbarMinimumRange,
             int margin, boolean alwaysVisible) {
    mVerticalThumbDrawable = verticalThumbDrawable;
    mVerticalTrackDrawable = verticalTrackDrawable;
    mHorizontalThumbDrawable = horizontalThumbDrawable;
    mHorizontalTrackDrawable = horizontalTrackDrawable;
    mVerticalThumbWidth = Math.max(defaultWidth, verticalThumbDrawable.getIntrinsicWidth());
    mVerticalTrackWidth = Math.max(defaultWidth, verticalTrackDrawable.getIntrinsicWidth());
    mHorizontalThumbHeight = Math
            .max(defaultWidth, horizontalThumbDrawable.getIntrinsicWidth());
    mHorizontalTrackHeight = Math
            .max(defaultWidth, horizontalTrackDrawable.getIntrinsicWidth());
    mDefaultWidth = defaultWidth;
    mScrollbarMinimumRange = scrollbarMinimumRange;
    mMargin = margin;
    mAlwaysVisible = alwaysVisible;
    mVerticalThumbDrawable.setAlpha(SCROLLBAR_FULL_OPAQUE);
    mVerticalTrackDrawable.setAlpha(SCROLLBAR_FULL_OPAQUE);

    mShowHideAnimator.addListener(new AnimatorListener());
    mShowHideAnimator.addUpdateListener(new AnimatorUpdater());

    attachToRecyclerView(recyclerView);
}
 
Example #4
Source File: TagView.java    From fingen with Apache License 2.0 6 votes vote down vote up
private Drawable getSelector(Tag tag) {
	if (tag.background!=null)return tag.background;
	StateListDrawable states = new StateListDrawable();
	GradientDrawable gd_normal = new GradientDrawable();
	gd_normal.setColor(tag.layoutColor);
	gd_normal.setCornerRadius(tag.radius);
	if (tag.layoutBorderSize>0){
		gd_normal.setStroke(Utils.dipToPx(getContext(),tag.layoutBorderSize), tag.layoutBorderColor);
	}
	GradientDrawable gd_press = new GradientDrawable();
	gd_press.setColor(tag.layoutColorPress);
	gd_press.setCornerRadius(tag.radius);
	states.addState(new int[] { android.R.attr.state_pressed }, gd_press);
	//must add state_pressed first,or state_pressed will not take effect
	states.addState(new int[] {}, gd_normal);
	return states;
}
 
Example #5
Source File: YearPickerView.java    From PersianDateRangePicker with Apache License 2.0 6 votes vote down vote up
/**
 * @param context
 */
public YearPickerView(Context context, DatePickerController controller) {
  super(context);
  mController = controller;
  mController.registerOnDateChangedListener(this);
  ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT);
  setLayoutParams(frame);
  Resources res = context.getResources();
  mViewSize = res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height);
  mChildSize = res.getDimensionPixelOffset(R.dimen.mdtp_year_label_height);
  setVerticalFadingEdgeEnabled(true);
  setFadingEdgeLength(mChildSize / 3);
  init(context);
  setOnItemClickListener(this);
  setSelector(new StateListDrawable());
  setDividerHeight(0);
  onDateChanged();
}
 
Example #6
Source File: ThemeUtils.java    From MagicaSakura with Apache License 2.0 6 votes vote down vote up
public static boolean containsNinePatch(Drawable drawable) {
    drawable = getWrapperDrawable(drawable);
    if (drawable instanceof NinePatchDrawable
            || drawable instanceof InsetDrawable
            || drawable instanceof LayerDrawable) {
        return true;
    } else if (drawable instanceof StateListDrawable) {
        final DrawableContainer.DrawableContainerState containerState = ((DrawableContainer.DrawableContainerState) drawable.getConstantState());
        //can't get containState from drawable which is containing DrawableWrapperDonut
        //https://code.google.com/p/android/issues/detail?id=169920
        if (containerState == null) {
            return true;
        }
        for (Drawable dr : containerState.getChildren()) {
            dr = getWrapperDrawable(dr);
            if (dr instanceof NinePatchDrawable
                    || dr instanceof InsetDrawable
                    || dr instanceof LayerDrawable) {
                return true;
            }
        }
    }
    return false;
}
 
Example #7
Source File: YearPickerView.java    From AlarmOn with Apache License 2.0 6 votes vote down vote up
/**
 * @param context
 */
public YearPickerView(Context context, DatePickerController controller) {
    super(context);
    mController = controller;
    mController.registerOnDateChangedListener(this);
    ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    setLayoutParams(frame);
    Resources res = context.getResources();
    mViewSize = res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height);
    mChildSize = res.getDimensionPixelOffset(R.dimen.mdtp_year_label_height);
    setVerticalFadingEdgeEnabled(true);
    setFadingEdgeLength(mChildSize / 3);
    init(context);
    setOnItemClickListener(this);
    setSelector(new StateListDrawable());
    setDividerHeight(0);
    onDateChanged();
}
 
Example #8
Source File: Label.java    From ShareBox with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    void onActionDown() {
        if (mUsingStyle) {
            mBackgroundDrawable = getBackground();
        }

        if (mBackgroundDrawable instanceof StateListDrawable) {
            StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
            drawable.setState(new int[]{android.R.attr.state_pressed});
        } else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
            RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
            ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
            ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
            ripple.setVisible(true, true);
        }
//        setPressed(true);
    }
 
Example #9
Source File: CircularProgressButton.java    From ProgressButton with Apache License 2.0 6 votes vote down vote up
private void initIdleStateDrawable() {
    int colorNormal = getNormalColor(mIdleColorState);
    int colorPressed = getPressedColor(mIdleColorState);
    int colorFocused = getFocusedColor(mIdleColorState);
    int colorDisabled = getDisabledColor(mIdleColorState);
    if (background == null) {
        background = createDrawable(colorNormal);
    }

    StrokeGradientDrawable drawableDisabled = createDrawable(colorDisabled);
    StrokeGradientDrawable drawableFocused = createDrawable(colorFocused);
    StrokeGradientDrawable drawablePressed = createDrawable(colorPressed);
    mIdleStateDrawable = new StateListDrawable();

    mIdleStateDrawable.addState(new int[]{android.R.attr.state_pressed}, drawablePressed.getGradientDrawable());
    mIdleStateDrawable.addState(new int[]{android.R.attr.state_focused}, drawableFocused.getGradientDrawable());
    mIdleStateDrawable.addState(new int[]{-android.R.attr.state_enabled}, drawableDisabled.getGradientDrawable());
    mIdleStateDrawable.addState(StateSet.WILD_CARD, background.getGradientDrawable());
}
 
Example #10
Source File: SelectorUtil.java    From UtilsLib with MIT License 6 votes vote down vote up
/**
 * 根据map里面创建选择器
 *
 * @param states 封装了状态对应的Drawable对象的集合,状态选择器是有前后顺序的,所以请使用LinkedHashMap
 * @return map中的选择器
 */
public static StateListDrawable createSelectorByStates(LinkedHashMap<int[], Drawable> states) {
    //如果map里面的状态小于2个,就不能生成选择器
    if (null == states || states.size() < 2) {
        throw new IllegalStateException("You must make 2 state to create seletor!");
    }
    //创建选择器对象
    StateListDrawable stateListDrawable = new StateListDrawable();
    //遍历集合
    Set<Map.Entry<int[], Drawable>> set = states.entrySet();
    for (Map.Entry<int[], Drawable> next : set) {
        int[] state = next.getKey();
        LogUtil.e(null, state.length);
        Drawable drawable = next.getValue();
        //给选择器添加对应的状态
        stateListDrawable.addState(state, drawable);
    }
    return stateListDrawable;
}
 
Example #11
Source File: FloatingActionButton.java    From clear-todolist with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[]{-android.R.attr.state_enabled}, createCircleDrawable(mColorDisabled));
    drawable.addState(new int[]{android.R.attr.state_pressed}, createCircleDrawable(mColorPressed));
    drawable.addState(new int[]{}, createCircleDrawable(mColorNormal));

    if (Util.hasLollipop()) {
        RippleDrawable ripple = new RippleDrawable(new ColorStateList(new int[][]{{}},
                new int[]{mColorRipple}), drawable, null);
        setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setOval(0, 0, view.getWidth(), view.getHeight());
            }
        });
        setClipToOutline(true);
        mBackgroundDrawable = ripple;
        return ripple;
    }

    mBackgroundDrawable = drawable;
    return drawable;
}
 
Example #12
Source File: DayView.java    From material-calendarview with MIT License 6 votes vote down vote up
private static Drawable generateBackground(int color, int fadeTime, Rect bounds) {
  StateListDrawable drawable = new StateListDrawable();
  drawable.setExitFadeDuration(fadeTime);
  drawable.addState(new int[] { android.R.attr.state_checked }, generateCircleDrawable(color));
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    drawable.addState(
        new int[] { android.R.attr.state_pressed },
        generateRippleDrawable(color, bounds)
    );
  } else {
    drawable.addState(new int[] { android.R.attr.state_pressed }, generateCircleDrawable(color));
  }

  drawable.addState(new int[] { }, generateCircleDrawable(Color.TRANSPARENT));

  return drawable;
}
 
Example #13
Source File: PXDrawableUtil.java    From pixate-freestyle-android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a {@link Map} that holds a mapping from the existing state-lists
 * in the background {@link Drawable}.
 * 
 * @param background A {@link Drawable} background (
 *            {@link StateListDrawable}).
 * @return A {@link Map}. An empty map in case the background
 *         <code>null</code>, or is not a {@link StateListDrawable}.
 */
public static Map<int[], Drawable> getExistingStates(Drawable background) {
    LinkedHashMap<int[], Drawable> map = new LinkedHashMap<int[], Drawable>();
    if (background instanceof StateListDrawable) {
        // Grab the existing states. Note that the API hides some of the
        // public functionality with the @hide tag, so we have to access
        // those through reflection...
        StateListDrawable stateList = (StateListDrawable) background;
        DrawableContainerState containerState = (DrawableContainerState) stateList
                .getConstantState();
        Drawable[] children = containerState.getChildren();
        try {
            // This method is public but hidden ("pending API council")
            Method method = stateList.getClass().getMethod("getStateSet", int.class);
            for (int i = 0; i < containerState.getChildCount(); i++) {
                Object state = method.invoke(stateList, i);
                if (state instanceof int[]) {
                    map.put((int[]) state, children[i]);
                }
            }
        } catch (Exception e) {
            PXLog.e(TAG, e, "Error getting the state set");
        }
    }
    return map;
}
 
Example #14
Source File: FloatingActionButton.java    From ShareBox with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[]{-android.R.attr.state_enabled}, createCircleDrawable(mColorDisabled));
    drawable.addState(new int[]{android.R.attr.state_pressed}, createCircleDrawable(mColorPressed));
    drawable.addState(new int[]{}, createCircleDrawable(mColorNormal));

    if (Util.hasLollipop()) {
        RippleDrawable ripple = new RippleDrawable(new ColorStateList(new int[][]{{}},
                new int[]{mColorRipple}), drawable, null);
        setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setOval(0, 0, view.getWidth(), view.getHeight());
            }
        });
        setClipToOutline(true);
        mBackgroundDrawable = ripple;
        return ripple;
    }

    mBackgroundDrawable = drawable;
    return drawable;
}
 
Example #15
Source File: Label.java    From clear-todolist with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[]{android.R.attr.state_pressed}, createRectDrawable(mColorPressed));
    drawable.addState(new int[]{}, createRectDrawable(mColorNormal));

    if (Util.hasLollipop()) {
        RippleDrawable ripple = new RippleDrawable(new ColorStateList(new int[][]{{}},
                new int[]{mColorRipple}), drawable, null);
        setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setOval(0, 0, view.getWidth(), view.getHeight());
            }
        });
        setClipToOutline(true);
        mBackgroundDrawable = ripple;
        return ripple;
    }

    mBackgroundDrawable = drawable;
    return drawable;
}
 
Example #16
Source File: DiscussionPostsSpinnerAdapter.java    From edx-app-android with Apache License 2.0 6 votes vote down vote up
@Override
@NonNull
public View getDropDownView(int position, @Nullable View convertView,
        @NonNull ViewGroup parent) {
    TextView textView = initTextView(position, convertView, parent);
    if (textView != convertView) {
        Drawable icon = createIcon();
        StateListDrawable statefulIcon = new StateListDrawable();
        statefulIcon.setBounds(icon.getBounds());
        statefulIcon.addState(ACTIVATED_STATE_SET, icon);
        TextViewCompat.setCompoundDrawablesRelative(
                textView, statefulIcon, null, null, null);
    }
    ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();
    if (layoutParams.height == ViewGroup.LayoutParams.MATCH_PARENT) {
        layoutParams.height = spinner.getHeight() -
                spinner.getPaddingTop() - spinner.getPaddingBottom();
    }
    return textView;
}
 
Example #17
Source File: ThemeUtils.java    From timecat with Apache License 2.0 6 votes vote down vote up
public static boolean containsNinePatch(Drawable drawable) {
    drawable = getWrapperDrawable(drawable);
    if (drawable instanceof NinePatchDrawable || drawable instanceof InsetDrawable || drawable instanceof LayerDrawable) {
        return true;
    } else if (drawable instanceof StateListDrawable) {
        final DrawableContainer.DrawableContainerState containerState = ((DrawableContainer.DrawableContainerState) drawable.getConstantState());
        //can't get containState from drawable which is containing DrawableWrapperDonut
        //https://code.google.com/p/android/issues/detail?id=169920
        if (containerState == null) {
            return true;
        }
        for (Drawable dr : containerState.getChildren()) {
            dr = getWrapperDrawable(dr);
            if (dr instanceof NinePatchDrawable || dr instanceof InsetDrawable || dr instanceof LayerDrawable) {
                return true;
            }
        }
    }
    return false;
}
 
Example #18
Source File: LessonsFragment.java    From zhangshangwuda with Apache License 2.0 6 votes vote down vote up
private StateListDrawable getStateSelector(String color) {
	StateListDrawable stalistDrawable = new StateListDrawable();
	// int normal = android.R.attr.state_empty;
	// int pressed = android.R.attr.state_pressed;
	// int focused = android.R.attr.state_focused;
	// int selected = android.R.attr.state_selected;
	ColorDrawable normalBG = new ColorDrawable(Color.parseColor(color));
	ColorDrawable pressedBG = new ColorDrawable(Color.parseColor("#AAAAAA"));
	int pressed = android.R.attr.state_pressed;
	int window_focused = android.R.attr.state_window_focused;
	int focused = android.R.attr.state_focused;
	int selected = android.R.attr.state_selected;
	stalistDrawable.addState(new int[] { pressed, window_focused },
			pressedBG);
	stalistDrawable.addState(new int[] { pressed, -focused }, pressedBG);
	stalistDrawable.addState(new int[] { selected }, pressedBG);
	stalistDrawable.addState(new int[] { focused }, pressedBG);
	// 没有任何状态时显示的图片,我们给它设置我空集合
	stalistDrawable.addState(new int[] {}, normalBG);
	return stalistDrawable;
}
 
Example #19
Source File: SelectorDrawableCreator.java    From BackgroundLibrary with Apache License 2.0 6 votes vote down vote up
private void setSelectorDrawable(TypedArray typedArray, TypedArray selectorTa,
                                 StateListDrawable stateListDrawable, int attr, @AttrRes int functionId) throws Exception {
    int color = 0;
    Drawable resDrawable = null;

    //这里用try catch先判断是否是颜色而不是直接调用getDrawable,为了方便填入的是颜色时可以沿用其他属性,
    //否则如果是其他资源会覆盖app:corners_radius等其他shape属性设置的效果
    try {
        color = selectorTa.getColor(attr, 0);
        if (color == 0) {
            resDrawable = selectorTa.getDrawable(attr);
        }
    } catch (Exception e) {
        resDrawable = selectorTa.getDrawable(attr);
    }
    if (resDrawable == null && color != 0) {
        GradientDrawable tmpDrawable = DrawableFactory.getDrawable(typedArray);
        tmpDrawable.setColor(color);
        stateListDrawable.addState(new int[]{functionId}, tmpDrawable);
    } else {
        stateListDrawable.addState(new int[]{functionId}, resDrawable);
    }
}
 
Example #20
Source File: YearPickerView.java    From StyleableDateTimePicker with MIT License 6 votes vote down vote up
/**
 * @param context
 */
public YearPickerView(Context context, DatePickerController controller) {
    super(context);
    mController = controller;
    mController.registerOnDateChangedListener(this);
    ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    setLayoutParams(frame);
    Resources res = context.getResources();
    mViewSize = res.getDimensionPixelOffset(R.dimen.date_picker_view_animator_height);
    mChildSize = res.getDimensionPixelOffset(R.dimen.year_label_height);
    setVerticalFadingEdgeEnabled(true);
    setFadingEdgeLength(mChildSize / 3);
    init(context);
    setOnItemClickListener(this);
    setSelector(new StateListDrawable());
    setDividerHeight(0);
    onDateChanged();
}
 
Example #21
Source File: FastScrollerEx.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
public FastScrollerEx(RecyclerView recyclerView, StateListDrawable verticalThumbDrawable,
        Drawable verticalTrackDrawable, StateListDrawable horizontalThumbDrawable,
        Drawable horizontalTrackDrawable, int defaultWidth, int scrollbarMinimumRange,
        int margin) {
    mVerticalThumbDrawable = verticalThumbDrawable;
    mVerticalTrackDrawable = verticalTrackDrawable;
    mHorizontalThumbDrawable = horizontalThumbDrawable;
    mHorizontalTrackDrawable = horizontalTrackDrawable;
    mVerticalThumbWidth = Math.max(defaultWidth, verticalThumbDrawable.getIntrinsicWidth());
    mVerticalTrackWidth = Math.max(defaultWidth, verticalTrackDrawable.getIntrinsicWidth());
    mHorizontalThumbHeight = Math
        .max(defaultWidth, horizontalThumbDrawable.getIntrinsicWidth());
    mHorizontalTrackHeight = Math
        .max(defaultWidth, horizontalTrackDrawable.getIntrinsicWidth());
    mScrollbarMinimumRange = scrollbarMinimumRange;
    mMargin = margin;
    mVerticalThumbDrawable.setAlpha(SCROLLBAR_FULL_OPAQUE);
    mVerticalTrackDrawable.setAlpha(SCROLLBAR_FULL_OPAQUE);

    mShowHideAnimator.addListener(new AnimatorListener());
    mShowHideAnimator.addUpdateListener(new AnimatorUpdater());

    attachToRecyclerView(recyclerView);
}
 
Example #22
Source File: SelectorDrawableCreator.java    From BackgroundLibrary with Apache License 2.0 6 votes vote down vote up
private void setSelectorDrawable(TypedArray typedArray, TypedArray selectorTa,
                                 StateListDrawable stateListDrawable, int attr, @AttrRes int functionId) throws Exception {
    int color = 0;
    Drawable resDrawable = null;

    //这里用try catch先判断是否是颜色而不是直接调用getDrawable,为了方便填入的是颜色时可以沿用其他属性,
    //否则如果是其他资源会覆盖app:corners_radius等其他shape属性设置的效果
    try {
        color = selectorTa.getColor(attr, 0);
        if (color == 0) {
            resDrawable = selectorTa.getDrawable(attr);
        }
    } catch (Exception e) {
        resDrawable = selectorTa.getDrawable(attr);
    }
    if (resDrawable == null && color != 0) {
        GradientDrawable tmpDrawable = DrawableFactory.getDrawable(typedArray);
        tmpDrawable.setColor(color);
        stateListDrawable.addState(new int[]{functionId}, tmpDrawable);
    } else {
        stateListDrawable.addState(new int[]{functionId}, resDrawable);
    }
}
 
Example #23
Source File: TabletTabRecyclerAdapter.java    From ChromeLikeTabSwitcher with Apache License 2.0 6 votes vote down vote up
@NonNull
@Override
protected final View onInflateTabView(@NonNull final LayoutInflater inflater,
                                      @Nullable final ViewGroup parent,
                                      @NonNull final AbstractTabViewHolder viewHolder) {
    View view = inflater.inflate(R.layout.tablet_tab, parent, false);
    StateListDrawable backgroundDrawable = new StateListDrawable();
    Drawable defaultDrawable = ContextCompat
            .getDrawable(getModel().getContext(), R.drawable.tablet_tab_background);
    Drawable selectedDrawable = ContextCompat
            .getDrawable(getModel().getContext(), R.drawable.tablet_tab_background_selected);
    backgroundDrawable.addState(new int[]{android.R.attr.state_selected}, selectedDrawable);
    backgroundDrawable.addState(StateSet.WILD_CARD, defaultDrawable);
    ViewUtil.setBackground(view, backgroundDrawable);
    return view;
}
 
Example #24
Source File: RadiusCompoundDelegate.java    From UIWidget with Apache License 2.0 6 votes vote down vote up
/**
 * 设置CompoundButton的setButtonDrawable属性
 */
private void setButtonDrawable() {
    mButton = (CompoundButton) mView;
    if (mButtonDrawableSystemEnable) {
        return;
    }
    Log.i("setButtonDrawable", "id:" + mButton.getId() + ";mButtonDrawable:" + mButtonDrawable);
    if (mButtonDrawable == null
            && mButtonPressedDrawable == null
            && mButtonDisabledDrawable == null
            && mButtonSelectedDrawable == null
            && mButtonCheckedDrawable == null) {
        mButton.setButtonDrawable(null);
        return;
    }
    float radius = mButtonDrawableColorCircleEnable ?
            mButtonDrawableWidth + mButtonDrawableHeight / 2 : mButtonDrawableColorRadius;
    mStateButtonDrawable = new StateListDrawable();
    mStateButtonDrawable.addState(new int[]{mStateChecked}, getStateDrawable(mButtonCheckedDrawable, radius, mButtonDrawableWidth, mButtonDrawableHeight));
    mStateButtonDrawable.addState(new int[]{mStateSelected}, getStateDrawable(mButtonSelectedDrawable, radius, mButtonDrawableWidth, mButtonDrawableHeight));
    mStateButtonDrawable.addState(new int[]{mStatePressed}, getStateDrawable(mButtonPressedDrawable, radius, mButtonDrawableWidth, mButtonDrawableHeight));
    mStateButtonDrawable.addState(new int[]{mStateDisabled}, getStateDrawable(mButtonDisabledDrawable, radius, mButtonDrawableWidth, mButtonDrawableHeight));
    mStateButtonDrawable.addState(new int[]{}, getStateDrawable(mButtonDrawable, radius, mButtonDrawableWidth, mButtonDrawableHeight));
    DrawableUtil.setDrawableWidthHeight(mStateButtonDrawable, mButtonDrawableWidth, mButtonDrawableHeight);
    mButton.setButtonDrawable(mStateButtonDrawable);
}
 
Example #25
Source File: QButton.java    From QButton with MIT License 6 votes vote down vote up
private void notifyChanges() {

        Float factor = 0.8f, factorStorke = 0.9f;

        if (!isEnable) {
            //handling for button disable state
            setAlpha(0.6f);
        }

        if (mStrokeColor == 0) {
            mStrokeColor = manipulateColor(mBackgroundColor, factorStorke);
        }

        Drawable pressed = getDrawable1(manipulateColor(mBackgroundColor, factor), mRadius);
        Drawable normal = getDrawable1(mBackgroundColor, mRadius);

        StateListDrawable states = new StateListDrawable();
        states.addState(new int[]{android.R.attr.state_pressed}, pressed);
        states.addState(new int[]{}, normal);
        setBackground(states);

    }
 
Example #26
Source File: FloatingActionButton.java    From FloatingActionButton with Apache License 2.0 5 votes vote down vote up
private StateListDrawable createFillDrawable(float strokeWidth) {
  StateListDrawable drawable = new StateListDrawable();
  drawable.addState(new int[] { -android.R.attr.state_enabled }, createCircleDrawable(mColorDisabled, strokeWidth));
  drawable.addState(new int[] { android.R.attr.state_pressed }, createCircleDrawable(mColorPressed, strokeWidth));
  drawable.addState(new int[] { }, createCircleDrawable(mColorNormal, strokeWidth));
  return drawable;
}
 
Example #27
Source File: AppHelper.java    From KAM with GNU General Public License v3.0 5 votes vote down vote up
public static StateListDrawable selector(int color) {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[]{android.R.attr.state_pressed}, getColorDrawable(color));
    drawable.addState(new int[]{android.R.attr.state_focused}, getColorDrawable(color));
    drawable.addState(new int[]{android.R.attr.state_selected}, getColorDrawable(color));
    drawable.addState(new int[]{android.R.attr.state_activated}, getColorDrawable(color));
    return drawable;
}
 
Example #28
Source File: SimplePhasedAdapter.java    From android-phased-seek-bar with GNU Lesser General Public License v3.0 5 votes vote down vote up
public SimplePhasedAdapter(Resources resources, int[] items) {
    int size = items.length;
    mItems = new StateListDrawable[size];
    Drawable drawable;
    for (int i = 0; i < size; i++) {
        drawable = resources.getDrawable(items[i]);
        if (drawable instanceof StateListDrawable) {
            mItems[i] = (StateListDrawable) drawable;
        } else {
            mItems[i] = new StateListDrawable();
            mItems[i].addState(new int[] {}, drawable);
        }
    }
}
 
Example #29
Source File: MsgView.java    From likequanmintv with Apache License 2.0 5 votes vote down vote up
public void setBgSelector() {
    StateListDrawable bg = new StateListDrawable();

    setDrawable(gd_background, backgroundColor, strokeColor);
    bg.addState(new int[]{-android.R.attr.state_pressed}, gd_background);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {//16
        setBackground(bg);
    } else {
        //noinspection deprecation
        setBackgroundDrawable(bg);
    }
}
 
Example #30
Source File: BookPageFragment.java    From IslamicLibraryAndroid with GNU General Public License v3.0 5 votes vote down vote up
private void setBookMarkIcon(@NonNull MenuItem item) {
    TypedValue typedvalueattr = new TypedValue();
    getActivity().getTheme().resolveAttribute(R.attr.menuBookmarkIcon, typedvalueattr, true);
    StateListDrawable stateListDrawable = (StateListDrawable) getResources().getDrawable(typedvalueattr.resourceId);
    int[] state = {item.isChecked() ? android.R.attr.state_checked : -android.R.attr.state_checked};
    stateListDrawable.setState(state);
    item.setIcon(stateListDrawable.getCurrent());
    item.setTitle(item.isChecked() ? R.string.action_remove_book_mark : R.string.action_add_book_mark);
}