StateListDrawable states = new StateListDrawable(); states.addState(new int[] {android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.pressed)); states.addState(new int[] {android.R.attr.state_focused}, getResources().getDrawable(R.drawable.focused)); states.addState(new int[] { }, getResources().getDrawable(R.drawable.normal)); imageView.setImageDrawable(states);
StateListDrawable states = new StateListDrawable(); states.addState(new int[] {android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.pressed)); states.addState(new int[] {android.R.attr.state_focused}, getResources().getDrawable(R.drawable.focused)); states.addState(new int[] { }, getResources().getDrawable(R.drawable.normal)); imageView.setImageDrawable(states); //YOUR IMAGE HERE //AND FOR BUTTON button.setBackgroundDrawable(states);//FOR BUTTON
/** * helper to create a StateListDrawable for the drawer item background * * @param selected_color * @return */ public static StateListDrawable getDrawerItemBackground(int selected_color) { ColorDrawable clrActive = new ColorDrawable(selected_color); StateListDrawable states = new StateListDrawable(); states.addState(new int[]{android.R.attr.state_selected}, clrActive); return states; }
Bitmap bc1 = Bitmap.createBitmap(b.getWidth() + ICON_OFFSET, b.getHeight() + ICON_OFFSET, Bitmap.Config.ARGB_8888); Canvas c1 = new Canvas(bc1); c1.drawBitmap(b, 0, 0, null); Bitmap bc2 = Bitmap.createBitmap(b.getWidth() + ICON_OFFSET, b.getHeight() + ICON_OFFSET, Bitmap.Config.ARGB_8888); Canvas c2 = new Canvas(bc2); c2.drawBitmap(b, ICON_OFFSET, ICON_OFFSET, null); mIcon = new StateListDrawable(); mIcon.addState(new int[] { android.R.attr.state_pressed }, new BitmapDrawable(bc2)); mIcon.addState(StateSet.WILD_CARD, new BitmapDrawable(bc1));
/** * helper to create a stateListDrawable for the icon * * @param icon * @param selectedIcon * @return */ public static StateListDrawable getIconStateList(Drawable icon, Drawable selectedIcon) { StateListDrawable iconStateListDrawable = new StateListDrawable(); iconStateListDrawable.addState(new int[]{android.R.attr.state_selected}, selectedIcon); iconStateListDrawable.addState(new int[]{}, icon); return iconStateListDrawable; }
/** * 获得选择器 * * @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; }
public static StateListDrawable getCheckableIconStateList(Context ctx, Drawable icon, Drawable checkedIcon, boolean animate) { StateListDrawable iconStateListDrawable = new StateListDrawable(); iconStateListDrawable.addState(new int[]{android.R.attr.state_checked}, checkedIcon); iconStateListDrawable.addState(new int[]{-android.R.attr.state_checked}, icon); if (animate) { int duration = ctx.getResources().getInteger(android.R.integer.config_shortAnimTime); iconStateListDrawable.setEnterFadeDuration(duration); iconStateListDrawable.setExitFadeDuration(duration); } return iconStateListDrawable; } }
Button testButton = new Button(context); testButton.setText("Test"); StateListDrawable sld = new StateListDrawable(); GradientDrawable drawable = new GradientDrawable( Orientation.TOP_BOTTOM, new int[] { Color.BLUE, Color.RED }); sld.addState(new int[] { android.R.attr.state_pressed }, drawable); sld.addState(StateSet.WILD_CARD, new ColorDrawable(Color.YELLOW)); testButton.setBackgroundDrawable(sld); mainLayout.addView(testButton);
Switch switchInput = new Switch(this); int colorOn = 0xFF323E46; int colorOff = 0xFF666666; int colorDisabled = 0xFF333333; StateListDrawable thumbStates = new StateListDrawable(); thumbStates.addState(new int[]{android.R.attr.state_checked}, new ColorDrawable(colorOn)); thumbStates.addState(new int[]{-android.R.attr.state_enabled}, new ColorDrawable(colorDisabled)); thumbStates.addState(new int[]{}, new ColorDrawable(this.app.colorOff)); // this one has to come last switchInput.setThumbDrawable(thumbStates);
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 static StateListDrawable createStateListDrawable(Drawable drawable[]) { if (drawable == null || drawable.length < 4) return null; StateListDrawable states = new StateListDrawable(); states.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, drawable[0]); states.addState(new int[]{android.R.attr.state_focused, android.R.attr.state_enabled}, drawable[1]); states.addState(new int[]{android.R.attr.state_enabled}, drawable[2]); states.addState(new int[]{-android.R.attr.state_enabled}, drawable[3]); return states; }
AppCompatImageButton image = (AppCompatImageButton) findViewById(R.id.btnEnter); if (image != null) { VectorDrawableCompat vcAccept = VectorDrawableCompat.create(getResources(), R.drawable.vc_accept, getTheme()); VectorDrawableCompat vcAcceptWhite = VectorDrawableCompat.create(getResources(), R.drawable.vc_accept_white, getTheme()); StateListDrawable stateList = new StateListDrawable(); stateList.addState(new int[]{android.R.attr.state_focused, -android.R.attr.state_pressed}, vcAccept); stateList.addState(new int[]{android.R.attr.state_focused, android.R.attr.state_pressed}, vcAcceptWhite); stateList.addState(new int[]{-android.R.attr.state_focused, android.R.attr.state_pressed}, vcAcceptWhite); stateList.addState(new int[]{}, vcAccept); image.setImageDrawable(stateList); }
/** * 获取设置之后的Selector * * @return stateListDrawable */ public StateListDrawable getSelector() { StateListDrawable stateListDrawable = new StateListDrawable(); //注意该处的顺序,只要有一个状态与之相配,背景就会被换掉 //所以不要把大范围放在前面了,如果sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了 stateListDrawable.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, getDrawable(android.R.attr.state_pressed)); stateListDrawable.addState(new int[]{}, getDrawable(android.R.attr.state_enabled)); return stateListDrawable; }
/** * 获取设置之后的Selector * * @return stateListDrawable */ public StateListDrawable getSelector() { StateListDrawable stateListDrawable = new StateListDrawable(); //注意该处的顺序,只要有一个状态与之相配,背景就会被换掉 //所以不要把大范围放在前面了,如果sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了 stateListDrawable.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, getDrawable(android.R.attr.state_pressed)); stateListDrawable.addState(new int[]{-android.R.attr.state_enabled}, getDrawable(-android.R.attr.state_enabled)); stateListDrawable.addState(new int[]{}, getDrawable(android.R.attr.state_enabled)); return stateListDrawable; }
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; }
@Test public void testAddStateWithDrawable() { Drawable drawable = ShadowDrawable.createFromPath("/foo"); StateListDrawable stateListDrawable = new StateListDrawable(); int[] states = {R.attr.state_pressed}; stateListDrawable.addState(states, drawable); ShadowStateListDrawable shadow = shadowOf(stateListDrawable); Drawable drawableForState = shadow.getDrawableForState(states); assertNotNull(drawableForState); assertThat(((ShadowBitmapDrawable) shadowOf(drawableForState)).getPath()).isEqualTo("/foo"); }
@Test public void testAddDrawableWithWildCardState() { Drawable drawable = ShadowDrawable.createFromPath("/foo"); StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(StateSet.WILD_CARD, drawable); ShadowStateListDrawable shadow = shadowOf(stateListDrawable); Drawable drawableForState = shadow.getDrawableForState(StateSet.WILD_CARD); assertNotNull(drawableForState); assertThat(((ShadowBitmapDrawable) shadowOf(drawableForState)).getPath()).isEqualTo("/foo"); } }
public StateListDrawable build(Context ctx) { StateListDrawable stateListDrawable = new StateListDrawable(); GradientDrawable normal = (GradientDrawable) AppCompatResources.getDrawable(ctx, mStyle.getGradientDrawable()); GradientDrawable selected = (GradientDrawable) normal.getConstantState().newDrawable().mutate(); ColorHolder.applyToOrTransparent(mStyle.getColor(), ctx, normal); if (mStyle.getColorPressed() == null) { ColorHolder.applyToOrTransparent(mStyle.getColor(), ctx, selected); } else { ColorHolder.applyToOrTransparent(mStyle.getColorPressed(), ctx, selected); } if (mStyle.getCorners() != null) { normal.setCornerRadius(mStyle.getCorners().asPixel(ctx)); selected.setCornerRadius(mStyle.getCorners().asPixel(ctx)); } stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, selected); stateListDrawable.addState(StateSet.WILD_CARD, normal); return stateListDrawable; } }
static StateListDrawable bootstrapAlertCloseIcon(Context context, int width, int height, int inset) { StateListDrawable stateListDrawable = new StateListDrawable(); int defaultColor = ColorUtils.resolveColor(R.color.bootstrap_alert_cross_default, context); int activeColor = ColorUtils.resolveColor(R.color.bootstrap_gray, context); int disabledColor = ColorUtils.resolveColor(R.color.bootstrap_alert_cross_default, context); if (Build.VERSION.SDK_INT >= 14) { stateListDrawable.addState(new int[]{android.R.attr.state_hovered}, createCloseCrossIcon(context, width, height, activeColor, inset)); } stateListDrawable.addState(new int[]{android.R.attr.state_activated}, createCloseCrossIcon(context, width, height, activeColor, inset)); stateListDrawable.addState(new int[]{android.R.attr.state_focused}, createCloseCrossIcon(context, width, height, activeColor, inset)); stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, createCloseCrossIcon(context, width, height, activeColor, inset)); stateListDrawable.addState(new int[]{android.R.attr.state_selected}, createCloseCrossIcon(context, width, height, activeColor, inset)); stateListDrawable.addState(new int[]{-android.R.attr.state_enabled}, createCloseCrossIcon(context, width, height, disabledColor, inset)); stateListDrawable.addState(new int[]{}, createCloseCrossIcon(context, width, height, defaultColor, inset)); return stateListDrawable; }
static StateListDrawable bootstrapDropDownArrow(Context context, int width, int height, ExpandDirection expandDirection, boolean outline, BootstrapBrand brand) { StateListDrawable stateListDrawable = new StateListDrawable(); int defaultColor = outline ? brand.defaultFill(context) : brand.defaultTextColor(context); int activeColor = outline ? ColorUtils.resolveColor(android.R.color.white, context) : brand.activeTextColor(context); int disabledColor = outline ? brand.disabledFill(context) : brand.disabledTextColor(context); if (Build.VERSION.SDK_INT >= 14) { stateListDrawable.addState(new int[]{android.R.attr.state_hovered}, createArrowIcon(context, width, height, activeColor, expandDirection)); } stateListDrawable.addState(new int[]{android.R.attr.state_activated}, createArrowIcon(context, width, height, activeColor, expandDirection)); stateListDrawable.addState(new int[]{android.R.attr.state_focused}, createArrowIcon(context, width, height, activeColor, expandDirection)); stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, createArrowIcon(context, width, height, activeColor, expandDirection)); stateListDrawable.addState(new int[]{android.R.attr.state_selected}, createArrowIcon(context, width, height, activeColor, expandDirection)); stateListDrawable.addState(new int[]{-android.R.attr.state_enabled}, createArrowIcon(context, width, height, disabledColor, expandDirection)); stateListDrawable.addState(new int[]{}, createArrowIcon(context, width, height, defaultColor, expandDirection)); return stateListDrawable; }