Created
August 23, 2017 05:07
-
-
Save adam-hurwitz/68c25c6a484a85c35467d0bfe91af654 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RippleEffect { | |
public static void addRippleEffect(View view, boolean | |
rippleEnabled, int backgroundColor, int rippleColor) { | |
if (rippleEnabled && Build.VERSION.SDK_INT >= | |
Build.VERSION_CODES.LOLLIPOP) { | |
//Create RippleDrawable | |
view.setBackground( | |
getPressedColorRippleDrawable( | |
backgroundColor, | |
rippleColor)); | |
//Customize Ripple color | |
RippleDrawable rippleDrawable = | |
(RippleDrawable) view.getBackground(); | |
int[][] states = new int[][]{new int[] | |
{android.R.attr.state_enabled}}; | |
int[] colors = new int[]{rippleColor}; | |
ColorStateList colorStateList = | |
new ColorStateList(states, colors); | |
rippleDrawable.setColor(colorStateList); | |
view.setBackground(rippleDrawable); | |
} else if (rippleEnabled) { | |
//Create Selector for pre Lollipop | |
ViewCompat.setBackground(view, | |
createStateListDrawable(backgroundColor, rippleColor)); | |
} else { | |
//Ripple Disabled | |
view.setBackground(new ColorDrawable(backgroundColor)); | |
} | |
} | |
private static StateListDrawable createStateListDrawable(int | |
backgroundColor, int rippleColor) { | |
StateListDrawable stateListDrawable = | |
new StateListDrawable(); | |
stateListDrawable.addState( | |
new int[] { | |
android.R.attr.state_pressed}, | |
createDrawable(rippleColor)); | |
stateListDrawable.addState( | |
StateSet.WILD_CARD, | |
createDrawable(backgroundColor)); | |
return stateListDrawable; | |
} | |
private static Drawable createDrawable(int background){ | |
ShapeDrawable shapeDrawable = | |
new ShapeDrawable(new RectShape()); | |
shapeDrawable.getPaint().setColor(background); | |
return shapeDrawable; | |
} | |
private static Drawable getPressedColorRippleDrawable( | |
int normalColor, int pressedColor) { | |
return new RippleDrawable( | |
getPressedColorSelector(normalColor, pressedColor), | |
getColorDrawableFromColor(normalColor), null); | |
} | |
private static ColorStateList getPressedColorSelector( | |
int normalColor, int pressedColor) { | |
return new ColorStateList( | |
new int[][] | |
{ | |
new int[]{} | |
}, | |
new int[] | |
{ | |
pressedColor | |
} | |
); | |
} | |
private static ColorDrawable getColorDrawableFromColor( | |
int color) { | |
return new ColorDrawable(color); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment