private void update() { StateListDrawable background = new StateListDrawable(); background.addState(new int[]{android.R.attr.state_pressed}, createDrawable(backgroundColorPressed, cornerColorPressed, cornerWidth, cornerRadius)); background.addState(new int[]{-android.R.attr.state_enabled}, createDrawable(backgroundColorDisabled, cornerColorDisabled, cornerWidth, cornerRadius)); background.addState(StateSet.WILD_CARD, createDrawable(backgroundColor, cornerColor, cornerWidth, cornerRadius)); setBackground(background); setTextColor(new ColorStateList( new int[][]{ new int[]{android.R.attr.state_pressed}, new int[]{-android.R.attr.state_enabled}, new int[]{} }, new int[]{ textColorPressed, textColorDisabled, textColor } )); }
@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; }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) public void setBackgroundColor(int colorPressed, int colorNormal){ StateListDrawable sd = new StateListDrawable(); int[] state_pressed = new int[]{android.R.attr.state_pressed}; int[] state_normal = new int[]{android.R.attr.state_enabled}; GradientDrawable pressed = new GradientDrawable(); pressed.setColor(colorPressed); pressed.setCornerRadius(10); GradientDrawable enable = new GradientDrawable(); enable.setColor(colorNormal); enable.setCornerRadius(10); sd.addState(state_pressed, pressed); sd.addState(state_normal, enable); bg.setBackground(sd); }
@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); }
/** * create state list drawable * * @param context * @param idNormal * @param idPressed * @param idFocused * @param idUnable * @return */ public static StateListDrawable createStateListDrawable(Context context, int idNormal, int idPressed, int idFocused, int idUnable) { StateListDrawable bg = new StateListDrawable(); Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal); Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed); Drawable focused = idFocused == -1 ? null : context.getResources().getDrawable(idFocused); Drawable unable = idUnable == -1 ? null : context.getResources().getDrawable(idUnable); // View.PRESSED_ENABLED_STATE_SET bg.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed); // View.ENABLED_FOCUSED_STATE_SET bg.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focused); // View.ENABLED_STATE_SET bg.addState(new int[]{android.R.attr.state_enabled}, normal); // View.FOCUSED_STATE_SET bg.addState(new int[]{android.R.attr.state_focused}, focused); // View.WINDOW_FOCUSED_STATE_SET bg.addState(new int[]{android.R.attr.state_window_focused}, unable); // View.EMPTY_STATE_SET bg.addState(new int[]{}, normal); return bg; }
public void setCircleButtonStateListDrawable(View circleButton, int radius,int color) { WeakReference<Bitmap> imageNormal = new WeakReference<>(Bitmap.createBitmap(2 * radius, 2 * radius, Bitmap.Config.ARGB_8888)); Canvas canvasNormal = new Canvas(imageNormal.get()); Paint paintNormal = new Paint(); paintNormal.setAntiAlias(true); paintNormal.setColor(color); canvasNormal.drawCircle(radius, radius, radius, paintNormal); StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(StateSet.WILD_CARD, new BitmapDrawable(circleButton.getContext().getResources(), imageNormal.get())); if (android.os.Build.VERSION.SDK_INT >= 16) { circleButton.setBackground(stateListDrawable); } else { circleButton.setBackgroundDrawable(stateListDrawable); } }
/** * Generate bg drawable drawable. * * @param radii the radii * @param pressColor the press color * @param defaultColor the default color * @return the drawable */ public static Drawable generateRoundDrawable(Resources res, float radii, int pressColor, int defaultColor) { radii = dpToPx(res, radii); //外环的圆角矩形 float[] outRadii = new float[]{radii, radii, radii, radii, radii, radii, radii, radii};//四个角的 圆角幅度,8个可以设置的值,每个角都有2个边 2*4=8个 //按下状态 Shape roundRectShape = new RoundRectShape(outRadii, null, null);//圆角背景 ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape shopDrawablePress.getPaint().setColor(pressColor);//设置颜色 //正常状态 Shape roundRectShapeNormal = new RoundRectShape(outRadii, null, 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; }
/** * 正常 圆角边框; * 按下 圆角色块 */ public static Drawable generateBorderDrawable(float radii, float borderWidth, int pressColor, int defaultColor) { //外环的圆角矩形 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, null, null);//圆角背景 ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape shopDrawablePress.getPaint().setColor(pressColor);//设置颜色 //正常状态 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_pressed}, shopDrawablePress);//按下状态 bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态 return bgStateDrawable; }
public YearPickerView(Context context, DatePickerController controller) { super(context); this.mController = controller; this.mController.registerOnDateChangedListener(this); setLayoutParams(new LayoutParams(-1, -2)); Resources res = context.getResources(); this.mViewSize = res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height); this.mChildSize = res.getDimensionPixelOffset(R.dimen.mdtp_year_label_height); setVerticalFadingEdgeEnabled(true); setFadingEdgeLength(this.mChildSize / 3); init(context); setOnItemClickListener(this); setSelector(new StateListDrawable()); setDividerHeight(0); onDateChanged(); }
/** * 通过代码配置一个selector XML对象 . <br> * @author liulongzhenhai 2012-7-4 上午10:45:03 <br> * @param normal 没有状态 * @param pressed 按下状态 * @param focused 获取焦点状态 * @param unable 无状态 * @return 返回selector 的对象布局 */ public static StateListDrawable getNewSelector(final Drawable normal, final Drawable pressed, final Drawable focused, final Drawable unable) { final StateListDrawable bg = new StateListDrawable(); // View.PRESSED_ENABLED_STATE_SET bg.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed); // View.ENABLED_FOCUSED_STATE_SET bg.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused); // View.ENABLED_STATE_SET bg.addState(new int[] { android.R.attr.state_enabled }, normal); // View.FOCUSED_STATE_SET bg.addState(new int[] { android.R.attr.state_focused }, focused); // View.WINDOW_FOCUSED_STATE_SET bg.addState(new int[] { android.R.attr.state_window_focused }, unable); // View.EMPTY_STATE_SET bg.addState(new int[] {}, normal); return bg; }
@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; }
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()); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) void onActionUp() { if (mUsingStyle) { mBackgroundDrawable = getBackground(); } if (mBackgroundDrawable instanceof StateListDrawable) { StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable; drawable.setState(new int[]{}); } else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) { RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable; ripple.setState(new int[]{}); ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2); ripple.setVisible(true, true); } // setPressed(false); }
/** * Tests the {@link Coloring#createMultiStateDrawable(Drawable, Drawable, Drawable, boolean)} method. * <p> * Unfortunately some Drawable properties are not shadowed by Robolectric yet, so we can test only the basic stuff here. */ @Test public final void testCreateMultiStateDrawable() { // noinspection deprecation - can't enforce Lollipop here final BitmapDrawable normal = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_on); assertNotNull("Normal drawable is null", normal); // noinspection deprecation - can't enforce Lollipop here final BitmapDrawable clicked = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_off); assertNotNull("Clicked drawable is null", clicked); // noinspection deprecation - can't enforce Lollipop here final BitmapDrawable checked = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.star_off); assertNotNull("Checked drawable is null", checked); final StateListDrawable stateList = Coloring.createMultiStateDrawable(normal, clicked, checked, true); assertNotNull("Contrast state drawable is null", stateList); assertTrue("Contrast state drawable is not stateful", stateList.isStateful()); final Drawable.ConstantState constantState = stateList.getConstantState(); assertNotNull("Constant state is null", constantState); }
public StateListDrawable build(Context ctx) { StateListDrawable stateListDrawable = new StateListDrawable(); GradientDrawable normal = (GradientDrawable) ContextCompat.getDrawable(ctx, R.drawable.action_item_badge); GradientDrawable selected = (GradientDrawable) normal.getConstantState().newDrawable().mutate(); normal.setColor(mColor); selected.setColor(mColorPressed); if (mStroke > -1) { normal.setStroke(mStroke, mStrokeColor); selected.setStroke(mStroke, mStrokeColor); } if (mCorners > -1) { normal.setCornerRadius(mCorners); selected.setCornerRadius(mCorners); } stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, selected); stateListDrawable.addState(StateSet.WILD_CARD, normal); return stateListDrawable; }
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 getBaseApplication 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; }
public Drawable getCheckStatusDrawable(String origin, String checked, String pressed, String checkpressed, boolean is9Png) { int originId = this.mResources.getIdentifier(origin, "drawable", this.packageName); int selectId = this.mResources.getIdentifier(checked, "drawable", this.packageName); int pressedId = this.mResources.getIdentifier(pressed, "drawable", this.packageName); int selectpressId = this.mResources.getIdentifier(checkpressed, "drawable", this.packageName); if (originId == 0 && selectId == 0 && pressedId == 0 && selectpressId == 0) { String suffix = (is9Png ? ".9" : "") + ".png"; return new StateListDrawableBuilder(this.mContext, "drawable/" + origin + suffix).setPressDrawable("drawable/" + pressed + suffix).setCheckedDrawable("drawable/" + checked + suffix).setPressCheckedDrawable("drawable/" + checkpressed + suffix).create(); } Drawable mStateListDrawable = new StateListDrawable(); mStateListDrawable.addState(new int[]{16842912}, this.mResources.getDrawable(selectId)); mStateListDrawable.addState(new int[]{16842919}, this.mResources.getDrawable(pressedId)); mStateListDrawable.addState(new int[]{16842919, 16842912}, this.mResources.getDrawable(selectpressId)); mStateListDrawable.addState(new int[0], this.mResources.getDrawable(originId)); return mStateListDrawable; }
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); }
/** * Generate bg drawable drawable. * * @param pressColor the press color * @param defaultColor the default color * @return the drawable */ public static Drawable generateRoundDrawable(float radii, int pressColor, int defaultColor) { //圆角 Shape roundRectShape = new RoundRectShape(new float[]{radii, radii, radii, radii, radii, radii, radii, radii}, null, null);//圆角背景 //按下状态 ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape shopDrawablePress.getPaint().setColor(pressColor);//设置颜色 //正常状态 ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShape); shopDrawableNormal.getPaint().setColor(defaultColor); StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态 bgStateDrawable.addState(new int[]{-android.R.attr.state_enabled}, shopDrawablePress); bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态 return bgStateDrawable; }
private StateListDrawable createDefaultOverButtonBgDrawable() { int dp12 = (int) ThemeUtils.applyDimensionDp(this, 12.f); int dp8 = (int) ThemeUtils.applyDimensionDp(this, 8.f); float dp4 = ThemeUtils.applyDimensionDp(this, 4.f); float[] round = new float[]{dp4, dp4, dp4, dp4, dp4, dp4, dp4, dp4}; ShapeDrawable pressedDrawable = new ShapeDrawable(new RoundRectShape(round, null, null)); pressedDrawable.setPadding(dp12, dp8, dp12, dp8); int pressedColor = ThemeUtils.resolveColor(this, R.attr.gallery_toolbar_over_button_pressed_color, R.color.gallery_default_toolbar_over_button_pressed_color); pressedDrawable.getPaint().setColor(pressedColor); int normalColor = ThemeUtils.resolveColor(this, R.attr.gallery_toolbar_over_button_normal_color, R.color.gallery_default_toolbar_over_button_normal_color); ShapeDrawable normalDrawable = new ShapeDrawable(new RoundRectShape(round, null, null)); normalDrawable.setPadding(dp12, dp8, dp12, dp8); normalDrawable.getPaint().setColor(normalColor); StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable); stateListDrawable.addState(new int[]{}, normalDrawable); return stateListDrawable; }
private StateListDrawable getDrawableSeletor(Drawable normal, Drawable press) { StateListDrawable bg = new StateListDrawable(); if (null != normal) { bg.addState(new int[] { android.R.attr.state_pressed }, press); } if (null != press) { bg.addState(new int[] { android.R.attr.state_enabled }, normal); } return bg; }
private void setScanBtnBg(){ int defColor = CustomAttrValueUtil.getAttrColorValue(R.attr.colorAccent,R.color.colorAccent,this); int pressColor = CustomAttrValueUtil.getAttrColorValue(R.attr.press_color,R.color.colorAccent,this); Drawable backgroundDrawable = scanBtn.getBackground(); StateListDrawable sld = (StateListDrawable) backgroundDrawable;// 通过向下转型,转回原型,selector对应的Java类为:StateListDrawable SelectorUtil.changeViewColor(sld,new int[]{pressColor,defColor}); }
private static StateListDrawable getStateListDrawable(@ColorInt int normalColor, @ColorInt int pressedColor) { StateListDrawable states = new StateListDrawable(); states.addState(new int[]{android.R.attr.state_activated}, getColorDrawable(pressedColor)); states.addState(new int[]{}, getColorDrawable(normalColor)); // Animating across states. // It seems item background is lost on scrolling out of the screen, 21 <= API <= 23 if (!Utils.hasLollipop() || Utils.hasNougat()) { int duration = 200; //android.R.integer.config_shortAnimTime states.setEnterFadeDuration(duration); states.setExitFadeDuration(duration); } return states; }
private Drawable getPressedEffectBeforeLollipop(CORNER_POSITION cornerPosition, int color) { ShapeDrawable colorDrawablePressed = getCornerStateDrawable(cornerPosition); colorDrawablePressed.getPaint().setColor(color); colorDrawablePressed.getPaint().setStyle(Paint.Style.FILL); colorDrawablePressed.getPaint().setAlpha(PRESSED_ALPHA_VALUE); colorDrawablePressed.mutate(); StateListDrawable textBgDrawable = new StateListDrawable(); textBgDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, colorDrawablePressed); return textBgDrawable; }
/** * helper to create an StateListDrawable for the given normal and pressed color * * @param normalColor the normal color * @param pressedColor the pressed color * @return the StateListDrawable */ 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[]{}, new ColorDrawable(normalColor)); return states; }
/** * more advanced usage for fillable in alpha * * @param circleRect the defined rectangle * @return StateListDrawable item */ protected StateListDrawable createFillDrawable(RectF circleRect) { StateListDrawable drawable = new StateListDrawable(); drawable.addState(new int[]{android.R.attr.state_pressed}, createAlphaDrawble(circleRect, mColorPressed, mAlpha_press)); drawable.addState(new int[]{}, createAlphaDrawble(circleRect, mColorNormal, mAlpha_normal)); return drawable; }
/** * @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; }
/** * @param circleRect the defined rectangle * @return StateListDrawable */ protected StateListDrawable createFillDrawable(RectF circleRect) { StateListDrawable drawable = new StateListDrawable(); drawable.addState(new int[]{android.R.attr.state_pressed}, createCircleDrawable(circleRect, mColorPressed)); drawable.addState(new int[]{}, createCircleDrawable(circleRect, mColorNormal)); return drawable; }
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; }
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); } }
private void setColors(View view, int[] colors) { Drawable drawable = view.getBackground(); if (drawable instanceof StateListDrawable) { StateListDrawable stateListDrawable = (StateListDrawable) drawable; DrawableContainerState drawableContainerState = (DrawableContainerState) stateListDrawable.getConstantState(); if (drawableContainerState != null) { Drawable[] drawables = drawableContainerState.getChildren(); for (int i = 0; i < colors.length; i++) if (drawables[i] instanceof GradientDrawable) ((GradientDrawable) drawables[i]).setColor(colors[i]); } } else Log.w(TAG, "Drawable of view must be StateListDrawable in WoWoStateListColorAnimation"); }
private StateListDrawable createDefaultBackground() { TypedValue value = new TypedValue(); if (!getContext().getTheme().resolveAttribute(R.attr.colorControlHighlight, value, true)) { return null; } StateListDrawable drawable = new StateListDrawable(); drawable.addState(CHECKED_STATE_SET, new ColorDrawable(value.data)); drawable.addState(EMPTY_STATE_SET, new ColorDrawable(0)); return drawable; }
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; }
public StateListDrawable create() { StateListDrawable selector = new StateListDrawable(); //enabled = false if (hasSetDisabledBgColor || hasSetDisabledStrokeColor) { GradientDrawable disabledShape = getItemShape(mShape, mCornerRadius, mDisabledBgColor, mStrokeWidth, mDisabledStrokeColor); selector.addState(new int[]{-android.R.attr.state_enabled}, disabledShape); } //pressed = true if (hasSetPressedBgColor || hasSetPressedStrokeColor) { GradientDrawable pressedShape = getItemShape(mShape, mCornerRadius, mPressedBgColor, mStrokeWidth, mPressedStrokeColor); selector.addState(new int[]{android.R.attr.state_pressed}, pressedShape); } //selected = true if (hasSetSelectedBgColor || hasSetSelectedStrokeColor) { GradientDrawable selectedShape = getItemShape(mShape, mCornerRadius, mSelectedBgColor, mStrokeWidth, mSelectedStrokeColor); selector.addState(new int[]{android.R.attr.state_selected}, selectedShape); } //focused = true if (hasSetFocusedBgColor || hasSetFocusedStrokeColor) { GradientDrawable focusedShape = getItemShape(mShape, mCornerRadius, mFocusedBgColor, mStrokeWidth, mFocusedStrokeColor); selector.addState(new int[]{android.R.attr.state_focused}, focusedShape); } //default GradientDrawable defaultShape = getItemShape(mShape, mCornerRadius, mDefaultBgColor, mStrokeWidth, mDefaultStrokeColor); selector.addState(new int[]{}, defaultShape); return selector; }