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

The following are Jave code examples for showing how to use setEnterFadeDuration() 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: GitHub   File: FastAdapterUIUtils.java   View Source Code Vote up 6 votes
/**
 * 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;
}
 
Example 2
Project: GitHub   File: DrawableUtils.java   View Source Code Vote up 5 votes
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;
}
 
Example 3
Project: silly-android   File: Coloring.java   View Source Code Vote up 4 votes
/**
 * Creates a new {@link StateListDrawable}. Colors that need to be provided are backgrounds for drawable states: "normal" (or "idle"),
 * "clicked" (or "pressed") and "checked" (or "selected"). Optionally, a <i>fade</i> argument can be set to {@code false} to avoid the
 * fading effect when the drawable animates.
 *
 * <b>Note</b>: Use {@link Color#TRANSPARENT} to set a transparent state.
 *
 * @param context      Which context to use
 * @param normal       Color for the normal/idle state
 * @param clicked      Color for the clicked/pressed state
 * @param checked      Color for the checked/selected state (makes sense only for Honeycomb and later)
 * @param shouldFade   Set to {@code true} to enable the fading effect, {@code false} to disable it
 * @param cornerRadius Set to round the corners on rectangular drawables, 0 to disable
 * @return A {@link StateListDrawable} drawable object, new instance each time
 */
@NonNull
public static StateListDrawable createStateList(@NonNull final Context context, @ColorInt final int normal, @ColorInt final int clicked,
                                                @ColorInt final int checked, final boolean shouldFade, @IntRange(from = 0) int cornerRadius) {
    // initialize state arrays (they're in arrays because you can use different drawables for reverse transitions..)
    final int[] normalState = new int[] {};
    final int[] clickedState = new int[] { android.R.attr.state_pressed };
    final int[] checkedState = new int[] { android.R.attr.state_checked };
    final int[] selectedState = new int[] { android.R.attr.state_selected };
    final int[] focusedState = new int[] { android.R.attr.state_focused };
    int[] activatedState = new int[] {};
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        activatedState = new int[] { android.R.attr.state_activated };
    }

    // normal state drawable
    final Drawable normalDrawable = createColoredDrawable(normal, new Rect(0, 0, DEFAULT_BOUNDS, DEFAULT_BOUNDS));
    if (normalDrawable instanceof GradientDrawable) {
        ((GradientDrawable) normalDrawable).setCornerRadius(cornerRadius);
    }
    // clicked state drawable
    final Drawable clickedDrawable = createColoredDrawable(clicked, new Rect(0, 0, DEFAULT_BOUNDS, DEFAULT_BOUNDS));
    if (clickedDrawable instanceof GradientDrawable) {
        ((GradientDrawable) clickedDrawable).setCornerRadius(cornerRadius);
    }
    // checked state drawable
    final Drawable checkedDrawable = createColoredDrawable(checked, new Rect(0, 0, DEFAULT_BOUNDS, DEFAULT_BOUNDS));
    if (checkedDrawable instanceof GradientDrawable) {
        ((GradientDrawable) checkedDrawable).setCornerRadius(cornerRadius);
    }
    // focused state drawable (same as normal, only lighter)
    final Drawable focusedDrawable = createColoredDrawable(lightenColor(normal), new Rect(0, 0, DEFAULT_BOUNDS, DEFAULT_BOUNDS));
    if (focusedDrawable instanceof GradientDrawable) {
        ((GradientDrawable) focusedDrawable).setCornerRadius(cornerRadius);
    }

    // prepare the state list (order of the states is extremely important!)
    final StateListDrawable states = new StateListDrawable();

    if (!shouldFade) {
        // no fading, add all applicable states
        states.addState(clickedState, clickedDrawable); // !
        states.addState(selectedState, focusedDrawable); // reuse the focused drawable
        states.addState(focusedState, focusedDrawable);
        states.addState(checkedState, checkedDrawable);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            states.addState(activatedState, focusedDrawable);
        }
        states.addState(normalState, normalDrawable); // !
        return states;
    } else {
        // fade enabled, add only normal and pressed states (Honeycomb bug..)
        states.addState(clickedState, clickedDrawable); // !
        states.addState(normalState, normalDrawable); // !
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // fading only works on Honeycomb and later..
            states.setEnterFadeDuration(0);
            states.setExitFadeDuration(DEFAULT_FADE_DURATION);
        }
        return states;
    }
}
 
Example 4
Project: silly-android   File: Coloring.java   View Source Code Vote up 4 votes
/**
 * Creates a new {@link StateListDrawable} used for {@link android.widget.ImageView} icon coloring. The resulting state list's "pressed" icon color will be
 * <b>contrasted</b> to the "pressed" color of the {@link android.widget.ImageView}'s background drawable.
 * For example, since {@link android.widget.ImageButton} extends {@link android.widget.ImageView}, you can configure the ImageView's background to be black
 * when "idle" and white when "clicked", and set a fully white "idle" icon. This means that, when you click the {@link android.widget.ImageView}, its
 * background becomes white, making the white icon impossible to see. This method returns a drawable state list that will switch the icon color to black
 * when pressed, making the icon visible even on a white background; the only thing you really need to provide here is the background color you used for
 * the pressed state.
 *
 * @param context          Which context to use
 * @param normalColor      Color normal/idle icon state to this color
 * @param pressedBackColor Background color of the View that shows up when the View is pressed
 * @param shouldFade       Set to {@code true} if the state transition should have a fading effect
 * @param original         The "idle" state icon. This is the coloring base for all states
 * @return The color state list that takes care of contrasted colors
 */
@NonNull
public static StateListDrawable createContrastStateDrawable(@NonNull final Context context, @ColorInt final int normalColor, @ColorInt final int
        pressedBackColor,
                                                            final boolean shouldFade, @NonNull final Drawable original) {
    // migrate to a static drawable
    Drawable originalState = original;
    if (originalState instanceof StateListDrawable) {
        originalState = originalState.getCurrent();
    }

    // initialize state arrays (they're in arrays because you can use different colors for reverse transitions..)
    final int[] normalState = new int[] {};
    final int[] clickedState = new int[] { android.R.attr.state_pressed };
    final int[] checkedState = new int[] { android.R.attr.state_checked };
    final int[] selectedState = new int[] { android.R.attr.state_selected };
    final int[] focusedState = new int[] { android.R.attr.state_focused };
    int[] activatedState = new int[] {};
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        activatedState = new int[] { android.R.attr.state_activated };
    }

    final Drawable normalDrawable = colorDrawable(context, originalState, normalColor);
    final Drawable clickedDrawable = colorDrawable(context, originalState, contrastColor(pressedBackColor));
    final Drawable checkedDrawable = colorDrawable(context, originalState, contrastColor(pressedBackColor));
    final Drawable focusedDrawable = colorDrawable(context, originalState, contrastColor(darkenColor(pressedBackColor)));

    // prepare the state list (order of the states is extremely important!)
    final StateListDrawable states = new StateListDrawable();

    if (!shouldFade) {
        // no fading, add all applicable states
        states.addState(clickedState, clickedDrawable); // !
        states.addState(selectedState, focusedDrawable); // reuse the focused drawable
        states.addState(focusedState, focusedDrawable);
        states.addState(checkedState, checkedDrawable);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            states.addState(activatedState, focusedDrawable);
        }
        states.addState(normalState, normalDrawable); // !
        return states;
    } else {
        // fade enabled, add only normal and pressed states (Honeycomb bug..)
        states.addState(clickedState, clickedDrawable); // !
        states.addState(normalState, normalDrawable); // !
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // fading only works on Honeycomb and later..
            states.setEnterFadeDuration(0);
            states.setExitFadeDuration(DEFAULT_FADE_DURATION);
        }
        return states;
    }
}
 
Example 5
Project: silly-android   File: Coloring.java   View Source Code Vote up 4 votes
/**
 * Creates a new, multi-state {@link StateListDrawable} using the provided static drawables.
 *
 * @param normalDrawable  Used for the normal/idle and focused states
 * @param clickedDrawable Used for the clicked/pressed state
 * @param checkedDrawable Used for the checked/selected and active states
 * @param shouldFade      Set to {@code true} if the state transition should have a fading effect
 * @return A multi-state {@link StateListDrawable} consisting out of provided drawables, always a new instance
 */
@NonNull
public static StateListDrawable createMultiStateDrawable(@NonNull final Drawable normalDrawable, @NonNull final Drawable clickedDrawable,
                                                         @NonNull final Drawable checkedDrawable, final boolean shouldFade) {
    // migrate to static drawables
    Drawable normalState = normalDrawable;
    if (normalState instanceof StateListDrawable) {
        normalState = normalState.getCurrent();
    }
    Drawable clickedState = clickedDrawable;
    if (clickedState instanceof StateListDrawable) {
        clickedState = clickedState.getCurrent();
    }
    Drawable checkedState = checkedDrawable;
    if (checkedState instanceof StateListDrawable) {
        checkedState = checkedState.getCurrent();
    }

    // initialize state arrays (they're in arrays because you can use different colors for reverse transitions..)
    final int[] normalStates = new int[] {};
    final int[] clickedStates = new int[] { android.R.attr.state_pressed };
    final int[] checkedStates = new int[] { android.R.attr.state_checked };
    final int[] selectedStates = new int[] { android.R.attr.state_selected };
    final int[] focusedStates = new int[] { android.R.attr.state_focused };
    int[] activatedState = new int[] {};
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        activatedState = new int[] { android.R.attr.state_activated };
    }

    // prepare the state list (order of the states is extremely important!)
    final StateListDrawable states = new StateListDrawable();

    if (!shouldFade) {
        // no fading, add all applicable states
        states.addState(clickedStates, clickedState); // !
        states.addState(selectedStates, checkedState);
        states.addState(focusedStates, normalState);
        states.addState(checkedStates, checkedState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            states.addState(activatedState, checkedState);
        }
        states.addState(normalStates, normalState); // !
        return states;
    } else {
        // fade enabled, add only normal and pressed states (Honeycomb bug..)
        states.addState(clickedStates, clickedState); // !
        states.addState(normalStates, normalState); // !
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // fading only works on Honeycomb and later..
            states.setEnterFadeDuration(0);
            states.setExitFadeDuration(DEFAULT_FADE_DURATION);
        }
        return states;
    }
}