本文整理匯總了Java中android.graphics.drawable.StateListDrawable.setEnterFadeDuration方法的典型用法代碼示例。如果您正苦於以下問題:Java StateListDrawable.setEnterFadeDuration方法的具體用法?Java StateListDrawable.setEnterFadeDuration怎麽用?Java StateListDrawable.setEnterFadeDuration使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.graphics.drawable.StateListDrawable
的用法示例。
在下文中一共展示了StateListDrawable.setEnterFadeDuration方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getSelectableBackground
import android.graphics.drawable.StateListDrawable; //導入方法依賴的package包/類
/**
* 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;
}
示例2: getStateListDrawable
import android.graphics.drawable.StateListDrawable; //導入方法依賴的package包/類
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;
}
示例3: createStateList
import android.graphics.drawable.StateListDrawable; //導入方法依賴的package包/類
/**
* 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;
}
}
示例4: createContrastStateDrawable
import android.graphics.drawable.StateListDrawable; //導入方法依賴的package包/類
/**
* 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;
}
}
示例5: createMultiStateDrawable
import android.graphics.drawable.StateListDrawable; //導入方法依賴的package包/類
/**
* 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;
}
}