/** * 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; }
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; } }
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; }
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);
public static StateListDrawable makeSelector(int color) { StateListDrawable res = new StateListDrawable(); res.setExitFadeDuration(400); res.setAlpha(45); res.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(color)); res.addState(new int[]{}, new ColorDrawable(Color.TRANSPARENT)); return res; }
@Override public void addState(int[] stateSet, Drawable drawable) { super.addState(stateSet, drawable); childrenCount++; }
/** * 设置动画时长 * @param duration */ public void setAnimationDuration(@IntRange(from = 0)int duration){ this.mDuration = duration; mStateBackground.setEnterFadeDuration(mDuration); mStateBackground.setExitFadeDuration(mDuration); }
/*******************alpha animation duration********************/ public void setAnimationDuration(@IntRange(from = 0)int duration){ this.mDuration = duration; mStateBackground.setEnterFadeDuration(mDuration); }
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
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; } }
/** * helper to get the system default selectable background inclusive an active and pressed state * * @param ctx the context * @param selected_color the selected color * @param pressed_alpha 0-255 * @param animate true if you want to fade over the states (only animates if API newer than Build.VERSION_CODES.HONEYCOMB) * @return the StateListDrawable */ public static StateListDrawable getSelectablePressedBackground(Context ctx, @ColorInt int selected_color, int pressed_alpha, boolean animate) { StateListDrawable states = getSelectableBackground(ctx, selected_color, animate); ColorDrawable clrPressed = new ColorDrawable(adjustAlpha(selected_color, pressed_alpha)); states.addState(new int[]{android.R.attr.state_pressed}, clrPressed); return states; }
/** * 设置动画时长 * @param duration */ public void setAnimationDuration(@IntRange(from = 0)int duration){ this.mDuration = duration; mStateBackground.setEnterFadeDuration(mDuration); mStateBackground.setExitFadeDuration(mDuration); }
/*******************alpha animation duration********************/ public void setAnimationDuration(@IntRange(from = 0)int duration){ this.mDuration = duration; mStateBackground.setEnterFadeDuration(mDuration); }
/** * 获得选择器 * * @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; }
/** * helper to get the system default selectable background inclusive an active state * * @param ctx the context * @param selected_color the selected color * @param animate true if you want to fade over the states (only animates if API newer than Build.VERSION_CODES.HONEYCOMB) * @return the StateListDrawable */ public static StateListDrawable getSelectableBackground(Context ctx, @ColorInt int selected_color, boolean animate) { StateListDrawable states = new StateListDrawable(); ColorDrawable clrActive = new ColorDrawable(selected_color); states.addState(new int[]{android.R.attr.state_selected}, clrActive); states.addState(new int[]{}, ContextCompat.getDrawable(ctx, getSelectableBackground(ctx))); //if possible we enable animating across states if (animate) { int duration = ctx.getResources().getInteger(android.R.integer.config_shortAnimTime); states.setEnterFadeDuration(duration); states.setExitFadeDuration(duration); } return states; }
radioButton.setButtonDrawable(new StateListDrawable());
/** * 设置不同状态下的背景 * @param normal * @param pressed * @param unable */ public void setStateBackground(Drawable normal, Drawable pressed, Drawable unable){ this.mNormalDrawable = normal; this.mPressedDrawable = pressed; this.mUnableDrawable = unable; //set background if(mPressedDrawable != null) { mStateBackground.addState(states[0], mPressedDrawable); mStateBackground.addState(states[1], mPressedDrawable); } if(mUnableDrawable != null) { mStateBackground.addState(states[3], mUnableDrawable); } if(mNormalDrawable != null) { mStateBackground.addState(states[2], mNormalDrawable); } setBackgroundDrawable(mStateBackground); }
/*******************alpha animation duration********************/ public void setAnimationDuration(@IntRange(from = 0)int duration){ this.mDuration = duration; mStateBackground.setEnterFadeDuration(mDuration); }
/** * 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; }
Drawable touchInsetDrawable = new InsetDrawable(touchDrawable, paddingStartEnd, paddingTopBottom, paddingStartEnd, paddingTopBottom); StateListDrawable unselectedStates = new StateListDrawable(); unselectedStates.setEnterFadeDuration(duration); unselectedStates.setExitFadeDuration(duration); unselectedStates.addState(new int[]{android.R.attr.state_pressed}, touchInsetDrawable); unselectedStates.addState(new int[]{}, new ColorDrawable(Color.TRANSPARENT)); unselected = unselectedStates; StateListDrawable states = new StateListDrawable(); states.setEnterFadeDuration(duration); states.setExitFadeDuration(duration); states.addState(new int[]{android.R.attr.state_selected}, selected); states.addState(new int[]{}, new ColorDrawable(Color.TRANSPARENT)); states.addState(new int[]{android.R.attr.state_selected}, selected); states.addState(new int[]{}, unselected);