下面列出了android.graphics.drawable.StateListDrawable#setExitFadeDuration() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Drawable generateBackground(@Selection int selection, int color, int transparentColor, int fadeTime, Rect bounds) {
StateListDrawable drawable = new StateListDrawable();
drawable.setExitFadeDuration(fadeTime);
int resultColor = (selection == SELECTION_NORMAL || selection == SELECTION_FIRST || selection == SELECTION_LAST)
? color : transparentColor;
drawable.addState(new int[]{android.R.attr.state_checked}, generateCircleDrawable(resultColor));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable.addState(new int[]{android.R.attr.state_pressed}, generateRippleDrawable(resultColor, bounds));
} else {
drawable.addState(new int[]{android.R.attr.state_pressed}, generateCircleDrawable(transparent(resultColor, 0.5f)));
}
drawable.addState(new int[]{}, generateCircleDrawable(Color.TRANSPARENT));
return drawable;
}
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));
if (!FlexibleUtils.hasLollipop()) {
states.addState(new int[]{android.R.attr.state_pressed}, getColorDrawable(pressedColor));
}
states.addState(new int[]{}, getColorDrawable(normalColor));
// Animating across states.
// It seems item background is lost on scrolling out of the screen on 21 <= API <= 23
if (!FlexibleUtils.hasLollipop() || FlexibleUtils.hasNougat()) {
int duration = 200; //android.R.integer.config_shortAnimTime
states.setEnterFadeDuration(duration);
states.setExitFadeDuration(duration);
}
return states;
}
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;
}
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;
}
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;
}
private static Drawable generateBackground(int color, int fadeTime) {
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));
} else {
drawable.addState(new int[]{android.R.attr.state_pressed}, generateCircleDrawable(color));
}
drawable.addState(new int[]{}, generateCircleDrawable(Color.TRANSPARENT));
return drawable;
}
/**
* 设置shape属性
* 设置完所有属性后调用设置背景
*/
public void init() {
if (mView instanceof EditText) {
Log.i("v", "click:" + mView.isClickable() + ";enable:" + mView.isEnabled());
}
//获取view当前drawable--用于判断是否通过默认属性设置背景
Drawable mDrawable = mView.getBackground();
//判断是否使用自定义颜色值
boolean isSetBg = mBackgroundColor != Integer.MAX_VALUE
|| mBackgroundPressedColor != Integer.MAX_VALUE
|| mBackgroundDisabledColor != Integer.MAX_VALUE
|| mBackgroundSelectedColor != Integer.MAX_VALUE
|| mStrokeWidth > 0 || mRadius > 0
|| mTopLeftRadius > 0 || mTopLeftRadius > 0 || mBottomLeftRadius > 0 || mBottomRightRadius > 0;
setDrawable(mBackgroundChecked, mBackgroundCheckedColor, mStrokeCheckedColor);
setDrawable(mBackgroundSelected, mBackgroundSelectedColor, mStrokeSelectedColor);
setDrawable(mBackground, mBackgroundColor, mStrokeColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& mRippleEnable
&& mView.isEnabled()
&& mView.isClickable()) {//5.0以上且设置水波属性并且可操作
RippleDrawable rippleDrawable = new RippleDrawable(
new ColorStateList(
new int[][]{
new int[]{mStatePressed},
new int[]{}
},
new int[]{
mRippleColor != Integer.MAX_VALUE ? mRippleColor : getBackColor(mBackgroundPressedColor),
mRippleColor
}
)
, getContentDrawable(mDrawable, isSetBg)
, null);
mView.setBackground(rippleDrawable);
} else {
if (!isSetBg) {
return;
}
setDrawable(mBackgroundPressed, mBackgroundPressedColor, mStrokePressedColor);
setDrawable(mBackgroundDisabled, mBackgroundDisabledColor, mStrokeDisabledColor);
StateListDrawable mStateDrawable = new StateListDrawable();
mStateDrawable.setEnterFadeDuration(mEnterFadeDuration);
mStateDrawable.setExitFadeDuration(mExitFadeDuration);
mStateDrawable.addState(new int[]{mStatePressed}, mBackgroundPressed);
mStateDrawable.addState(new int[]{mStateSelected}, mBackgroundSelected);
mStateDrawable.addState(new int[]{mStateChecked}, mBackgroundChecked);
mStateDrawable.addState(new int[]{mStateDisabled}, mBackgroundDisabled);
//默认状态--放置在最后否则其它状态不生效
mStateDrawable.addState(new int[]{}, mBackground);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mView.setBackground(mStateDrawable);
} else {
mView.setBackgroundDrawable(mStateDrawable);
}
}
return;
}
/**
* Creates a new {@code StateListDrawable} drawable. States that should be provided are "normal",<br>
* "clicked" (pressed) and "checked" (selected). All states are actually integer colors.<br>
* Optionally, {@code shouldFade} can be set to false to avoid the fading effect.<br>
* <br>
* Note: <i>{@link Color#TRANSPARENT} can be used to supply a transparent state.</i>
*
* @param normal Color for the idle state
* @param clicked Color for the clicked/pressed state
* @param checked Color for the checked/selected state
* @param shouldFade Set to true to enable the fading effect, false otherwise
* @return A {@link StateListDrawable} drawable object ready for use
*/
@SuppressLint({
"InlinedApi", "NewApi"
})
public Drawable createStateDrawable(int normal, int clicked, int checked, boolean shouldFade) {
// init state arrays
int[] selectedState = new int[] {
android.R.attr.state_selected
};
int[] pressedState = new int[] {
android.R.attr.state_pressed
};
int[] checkedState = new int[] {
android.R.attr.state_checked
};
int[] focusedState = new int[] {
android.R.attr.state_focused
};
int[] activatedState = new int[] {};
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
activatedState = new int[] {
android.R.attr.state_activated
};
}
// init normal state drawable
Drawable normalDrawable = new GradientDrawable(Orientation.BOTTOM_TOP, new int[] {
normal, normal
}).mutate();
if (normal == Color.TRANSPARENT)
normalDrawable.setAlpha(0);
else
normalDrawable.setBounds(BOUNDS, BOUNDS, BOUNDS, BOUNDS);
// init clicked state drawable
Drawable clickedDrawable = new GradientDrawable(Orientation.BOTTOM_TOP, new int[] {
clicked, clicked
}).mutate();
if (clicked == Color.TRANSPARENT)
clickedDrawable.setAlpha(0);
else
clickedDrawable.setBounds(BOUNDS, BOUNDS, BOUNDS, BOUNDS);
// init checked state drawable
Drawable checkedDrawable = new GradientDrawable(Orientation.BOTTOM_TOP, new int[] {
checked, checked
}).mutate();
if (checked == Color.TRANSPARENT)
checkedDrawable.setAlpha(0);
else
checkedDrawable.setBounds(BOUNDS, BOUNDS, BOUNDS, BOUNDS);
// init focused state drawable (use normal color)
Drawable focusedDrawable = new GradientDrawable(Orientation.BOTTOM_TOP, new int[] {
normal, normal
}).mutate();
if (normal == Color.TRANSPARENT)
focusedDrawable.setAlpha(0);
else
focusedDrawable.setBounds(BOUNDS, BOUNDS, BOUNDS, BOUNDS);
// prepare state list (order of adding states is important!)
StateListDrawable states = new StateListDrawable();
states.addState(pressedState, clickedDrawable);
if (!shouldFade) {
states.addState(selectedState, clickedDrawable);
states.addState(focusedState, focusedDrawable);
states.addState(checkedState, checkedDrawable);
}
// add fade effect if applicable
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
if (shouldFade) {
states.addState(new int[] {}, normalDrawable);
states.setEnterFadeDuration(0);
states.setExitFadeDuration(FADE_DURATION);
} else {
states.addState(activatedState, clickedDrawable);
states.addState(new int[] {}, normalDrawable);
}
} else {
states.addState(new int[] {}, normalDrawable);
}
return states;
}
/**
* Similar to {@link #createBackgroundDrawable(int, int, int, boolean)} but with additional {@code original} drawable parameter.
*
* @param context Which context to use
* @param normal Color normal state of the drawable to this color
* @param clickedBackground Background color of the View that will show when view is clicked
* @param shouldFade Set to true if the state list should have a fading effect
* @param original This drawable will be contrasted to the {@code clickedBackground} color on press
* @return The state list drawable that is in contrast with the on-click background color
*/
@SuppressLint({
"InlinedApi", "NewApi"
})
public Drawable createContrastStateDrawable(Context context, int normal, int clickedBackground, boolean shouldFade, Drawable original) {
if (original == null || original instanceof StateListDrawable) {
if (original != null) {
Log.i(LOG_TAG, "Original drawable is already a StateListDrawable");
original = original.getCurrent();
}
// overridden in previous if clause, so check again
if (original == null) {
return null;
}
}
// init state arrays
int[] selectedState = new int[] {
android.R.attr.state_selected
};
int[] pressedState = new int[] {
android.R.attr.state_pressed
};
int[] checkedState = new int[] {
android.R.attr.state_checked
};
int[] activatedState = new int[] {};
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
activatedState = new int[] {
android.R.attr.state_activated
};
}
Drawable normalStateDrawable = colorDrawable(context, original, normal);
Drawable clickedStateDrawable = colorDrawable(context, original, getContrastColor(clickedBackground));
Drawable checkedStateDrawable = colorDrawable(context, original, getContrastColor(clickedBackground));
// prepare state list (order of adding states is important!)
StateListDrawable states = new StateListDrawable();
states.addState(pressedState, clickedStateDrawable);
if (!shouldFade) {
states.addState(selectedState, clickedStateDrawable);
states.addState(checkedState, checkedStateDrawable);
}
// add fade effect if applicable
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
if (shouldFade) {
states.addState(new int[] {}, normalStateDrawable);
states.setEnterFadeDuration(0);
states.setExitFadeDuration(FADE_DURATION);
} else {
states.addState(activatedState, clickedStateDrawable);
states.addState(new int[] {}, normalStateDrawable);
}
} else {
states.addState(new int[] {}, normalStateDrawable);
}
return states;
}