Java Code Examples for android.graphics.drawable.StateListDrawable.addState()

The following are Jave code examples for showing how to use addState() of the android.graphics.drawable.StateListDrawable class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Toodoo   File: Label.java   View Source Code Vote up 6 votes
@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 2
Project: ChromeLikeTabSwitcher   File: TabletTabRecyclerAdapter.java   View Source Code Vote up 6 votes
@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 3
Project: RLibrary   File: ResUtil.java   View Source Code Vote up 6 votes
public static Drawable generateRoundDrawable(float radiiL, float radiiR, int pressColor, int defaultColor) {
    //外环的圆角矩形
    float[] outRadii = new float[]{radiiL, radiiL, radiiR, radiiR, radiiR, radiiR, radiiL, radiiL};//四个角的 圆角幅度,8个可以设置的值,每个角都有2个边 2*4=8个

    //与内环的距离
    RectF inset = new RectF(0, 0, 0, 0);

    //按下状态
    Shape roundRectShape = new RoundRectShape(outRadii, inset, null);//圆角背景
    ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape
    shopDrawablePress.getPaint().setColor(pressColor);//设置颜色

    //正常状态
    Shape roundRectShapeNormal = new RoundRectShape(outRadii, inset, null);
    ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShapeNormal);
    shopDrawableNormal.getPaint().setColor(defaultColor);

    StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape
    bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态
    bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态

    return bgStateDrawable;
}
 
Example 4
Project: RLibrary   File: EmojiTabLayout.java   View Source Code Vote up 6 votes
public void addItem(@DrawableRes int ico) {
    EmojiTabItemView itemView = new EmojiTabItemView(getContext());
    itemView.setImageResource(ico);
    if (mItemBackgroundRes == 0) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_checked},
                new ColorDrawable(getResources().getColor(R.color.default_base_bg_press)));
        stateListDrawable.addState(new int[]{android.R.attr.state_pressed},
                new ColorDrawable(getResources().getColor(R.color.default_base_bg_press)));
        stateListDrawable.addState(new int[]{}, new ColorDrawable(Color.TRANSPARENT));
        itemView.setBackground(stateListDrawable);
    } else {
        itemView.setBackgroundResource(mItemBackgroundRes);
    }
    addView(itemView, -1, -1);
    itemView.setOnClickListener(this);
}
 
Example 5
Project: MVPtemplate   File: MsgView.java   View Source Code Vote up 5 votes
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 6
Project: PrettyDialog   File: PrettyDialogButton.java   View Source Code Vote up 5 votes
private StateListDrawable makeSelector(int color) {
    StateListDrawable res = new StateListDrawable();
    res.setExitFadeDuration(150);
    GradientDrawable pressed_drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,new int[] {getLightenColor(color),getLightenColor(color)});
    pressed_drawable.setCornerRadius(resources.getDimensionPixelSize(R.dimen.pdlg_corner_radius));
    GradientDrawable default_drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,new int[] {color,color});
    default_drawable.setCornerRadius(resources.getDimensionPixelSize(R.dimen.pdlg_corner_radius));
    res.addState(new int[]{android.R.attr.state_pressed}, pressed_drawable);
    res.addState(new int[]{}, default_drawable);
    return res;
}
 
Example 7
Project: MyCalendar   File: FloatingActionButtonLibrary.java   View Source Code Vote up 5 votes
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 8
Project: Android_Songshuhui   File: MsgView.java   View Source Code Vote up 5 votes
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 9
Project: HeadlineNews   File: SelectorFactory.java   View Source Code Vote up 5 votes
public StateListDrawable create() {
    StateListDrawable selector = new StateListDrawable();
    if (hasSetDisabledDrawable)
        selector.addState(new int[]{-android.R.attr.state_enabled}, mDisabledDrawable);
    if (hasSetPressedDrawable)
        selector.addState(new int[]{android.R.attr.state_pressed}, mPressedDrawable);
    if (hasSetSelectedDrawable)
        selector.addState(new int[]{android.R.attr.state_selected}, mSelectedDrawable);
    if (hasSetFocusedDrawable)
        selector.addState(new int[]{android.R.attr.state_focused}, mFocusedDrawable);
    selector.addState(new int[]{}, mDefaultDrawable);
    return selector;
}
 
Example 10
Project: AppCommonFrame   File: BitmapUtil.java   View Source Code Vote up 5 votes
/**
 * 通过代码配置一个selector XML对象 . . <br>
 * @author liulongzhenhai 2012-8-1 下午5:27:28 <br>
 * @param normal 没有状态
 * @param selected 选择状态
 * @param pressed 按下状态
 * @return 返回selector 的对象布局
 */
public static StateListDrawable getRadioButtonSelector(final Drawable normal, final Drawable selected,
                                                          final Drawable pressed) {
	final StateListDrawable drawable = new StateListDrawable();
	drawable.addState(new int[] { android.R.attr.state_focused, android.R.attr.state_enabled }, pressed);
	drawable.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, normal);
	drawable.addState(new int[] { android.R.attr.state_checked, android.R.attr.state_enabled }, pressed);
	drawable.addState(new int[] { android.R.attr.state_selected, android.R.attr.state_enabled }, pressed);
	drawable.addState(new int[] {}, normal);
	return drawable;
}
 
Example 11
Project: GmArchMvvm   File: GmRipple.java   View Source Code Vote up 5 votes
@NonNull
public static Drawable getSelectableDrawableFor(int color) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(
                new int[]{android.R.attr.state_pressed},
                new ColorDrawable(lightenOrDarken(color, 0.40D))
        );
        stateListDrawable.addState(
                new int[]{android.R.attr.state_focused},
                new ColorDrawable(lightenOrDarken(color, 0.80D))
        );
        stateListDrawable.addState(
                new int[]{},
                new ColorDrawable(color)
        );
        return stateListDrawable;
    } else {
        ColorStateList pressedColor = ColorStateList.valueOf(lightenOrDarken(color, 0.4D));
        ColorDrawable defaultColor = new ColorDrawable(color);
        ShapeDrawable shapeDrawable = (ShapeDrawable) getShapeWithColor(color);
        Drawable rippleColor = getRippleColor(color);
        return new RippleDrawable(
                pressedColor,
                shapeDrawable,
                rippleColor
        );
    }
}
 
Example 12
Project: GitHub   File: FloatingActionButton.java   View Source Code Vote up 5 votes
/**
 * @param circleRect the defined rectangle
 * @param alpha      between  0 - 1
 * @return StateListDrawable
 */
protected StateListDrawable createFillDrawable(RectF circleRect, float alpha) {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[]{android.R.attr.state_pressed}, createAlphaDrawble(circleRect, mColorPressed, alpha));
    drawable.addState(new int[]{}, createAlphaDrawble(circleRect, mColorNormal, alpha));
    return drawable;
}
 
Example 13
Project: RLibrary   File: ResUtil.java   View Source Code Vote up 5 votes
public static Drawable generateStrokeDrawable(float radii, float borderWidth, int pressColor, int defaultColor, int disableColor) {
    float[] outRadii = new float[]{radii, radii, radii, radii, radii, radii, radii, radii};//四个角的 圆角幅度,8个可以设置的值,每个角都有2个边 2*4=8个

    //与内环的距离
    RectF inset = new RectF(borderWidth, borderWidth, borderWidth, borderWidth);

    //按下状态
    Shape roundRectShape = new RoundRectShape(outRadii, inset, outRadii);//圆角背景
    ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape
    shopDrawablePress.getPaint().setColor(pressColor);//设置颜色

    ShapeDrawable shopDrawableEnable = new ShapeDrawable(roundRectShape);
    shopDrawableEnable.getPaint().setColor(disableColor);

    //正常状态
    Shape roundRectShapeNormal = new RoundRectShape(outRadii, inset, outRadii);
    ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShapeNormal);
    shopDrawableNormal.getPaint().setColor(defaultColor);

    StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape
    bgStateDrawable.addState(new int[]{-android.R.attr.state_enabled}, shopDrawableEnable);//按下状态
    bgStateDrawable.addState(new int[]{android.R.attr.state_checked}, shopDrawablePress);//按下状态
    bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态
    bgStateDrawable.addState(new int[]{android.R.attr.state_focused}, shopDrawablePress);//焦点状态
    bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态

    return bgStateDrawable;
}
 
Example 14
Project: boohee_v5.6   File: DayView.java   View Source Code Vote up 5 votes
private static Drawable generateBackground(int color, int fadeTime) {
    StateListDrawable drawable = new StateListDrawable();
    drawable.setExitFadeDuration(fadeTime);
    drawable.addState(new int[]{16842912}, generateCircleDrawable(color));
    if (VERSION.SDK_INT >= 21) {
        drawable.addState(new int[]{16842919}, generateRippleDrawable(color));
    } else {
        drawable.addState(new int[]{16842919}, generateCircleDrawable(color));
    }
    drawable.addState(new int[0], generateCircleDrawable(0));
    return drawable;
}
 
Example 15
Project: RLibrary   File: ResUtil.java   View Source Code Vote up 5 votes
public static Drawable selectorChecked(Drawable normalDrawable, Drawable pressDrawable) {
    StateListDrawable listDrawable = new StateListDrawable();//状态shape
    listDrawable.addState(new int[]{android.R.attr.state_pressed}, pressDrawable);//按下状态
    listDrawable.addState(new int[]{android.R.attr.state_checked}, pressDrawable);//按下状态
    listDrawable.addState(new int[]{}, normalDrawable);//其他状态
    return listDrawable;
}
 
Example 16
Project: GitHub   File: DayView.java   View Source Code Vote up 5 votes
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 17
Project: mvvm-template   File: ViewHelper.java   View Source Code Vote up 5 votes
@NonNull private static StateListDrawable getStateListDrawable(int normalColor, int pressedColor) {
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(pressedColor));
    states.addState(new int[]{android.R.attr.state_focused}, new ColorDrawable(pressedColor));
    states.addState(new int[]{android.R.attr.state_activated}, new ColorDrawable(pressedColor));
    states.addState(new int[]{android.R.attr.state_selected}, new ColorDrawable(pressedColor));
    states.addState(new int[]{}, new ColorDrawable(normalColor));
    return states;
}
 
Example 18
Project: NeiHanDuanZiTV   File: MsgView.java   View Source Code Vote up 5 votes
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 19
Project: MusicX-music-player   File: NavigationViewProcessor.java   View Source Code Vote up 4 votes
@Override
    public void process(@NonNull Context context, @Nullable String key, @Nullable NavigationView view, @Nullable Void extra) {
        if (view == null || !Config.navigationViewThemed(context, key))
            return;

        boolean darkTheme = false;
        if (view.getBackground() != null && view.getBackground() instanceof ColorDrawable) {
            final ColorDrawable cd = (ColorDrawable) view.getBackground();
            darkTheme = !ATEUtil.isColorLight(cd.getColor());
        }

        final ColorStateList iconSl = new ColorStateList(
                new int[][]{
                        new int[]{-android.R.attr.state_checked},
                        new int[]{android.R.attr.state_checked}
                },
                new int[]{
                        Config.navigationViewNormalIcon(context, key, darkTheme),
                        Config.navigationViewSelectedIcon(context, key, darkTheme)
                });
        final ColorStateList textSl = new ColorStateList(
                new int[][]{
                        new int[]{-android.R.attr.state_checked},
                        new int[]{android.R.attr.state_checked}
                },
                new int[]{
                        Config.navigationViewNormalText(context, key, darkTheme),
                        Config.navigationViewSelectedText(context, key, darkTheme)
                });
        view.setItemTextColor(textSl);
        view.setItemIconTintList(iconSl);

        StateListDrawable bgDrawable = new StateListDrawable();
        bgDrawable.addState(new int[]{android.R.attr.state_checked}, new ColorDrawable(
                Config.navigationViewSelectedBg(context, key, darkTheme)));
        view.setItemBackground(bgDrawable);

        // TODO not needed since the layout inflater will catch it?
//        final View headerView = view.getHeaderView(0);
//        if (headerView != null) ATE.themeView(context, headerView, key);
    }
 
Example 20
Project: MVPArms_Fragment-fragment   File: DrawableProvider.java   View Source Code Vote up 3 votes
/**
 * 获得选择器
 *
 * @param normalDrawable
 * @param pressDrawable
 * @return
 */
public static Drawable getStateListDrawable(Drawable normalDrawable, Drawable pressDrawable) {
    StateListDrawable stateListDrawable = new StateListDrawable();
    stateListDrawable.addState(new int[]{android.R.attr.state_checked}, pressDrawable);
    stateListDrawable.addState(new int[]{}, normalDrawable);
    return stateListDrawable;
}