package com.vansuita.materialabout.util; import android.content.res.ColorStateList; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.RippleDrawable; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.StateListDrawable; import android.graphics.drawable.shapes.RoundRectShape; import android.os.Build; import android.util.StateSet; import android.view.View; import java.util.Arrays; /** * Created by jrvansuita on 10/02/17. */ public final class RippleUtil { public static void background(View v, Drawable d) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { v.setBackgroundDrawable(d); } else { v.setBackground(d); } } public static void background(View v, Bitmap b) { background(v, new BitmapDrawable(v.getResources(), b)); } public static void backgroundRipple(View v, int color) { RippleUtil.background(v, getAdaptiveRippleDrawable(color)); } public static Drawable getAdaptiveRippleDrawable( int normalColor) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return new RippleDrawable(ColorStateList.valueOf(darker(normalColor)), getRippleMask(normalColor), null); } else { return getStateListDrawable(normalColor, darker(normalColor)); } } private static Drawable getRippleMask(int color) { float[] outerRadii = new float[8]; // 3 is radius of final ripple, // instead of 3 you can give required final radius Arrays.fill(outerRadii, 3); RoundRectShape r = new RoundRectShape(outerRadii, null, null); ShapeDrawable shapeDrawable = new ShapeDrawable(r); shapeDrawable.getPaint().setColor(color); return shapeDrawable; } private static StateListDrawable getStateListDrawable( int normalColor, int pressedColor) { StateListDrawable states = new StateListDrawable(); states.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(pressedColor)); states.addState(new int[]{android.R.attr.state_focused}, new ColorDrawable(pressedColor)); states.addState(new int[]{android.R.attr.state_activated}, new ColorDrawable(pressedColor)); states.addState(new int[]{}, new ColorDrawable(normalColor)); states.addState(StateSet.WILD_CARD, new ColorDrawable(normalColor)); return states; } public static int darker(int color) { int r = Color.red(color); int b = Color.blue(color); int g = Color.green(color); return Color.rgb((int) (r * .9), (int) (g * .9), (int) (b * .9)); } public static boolean isDark(int color) { double darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255; return !(darkness < 0.5 || color == 0); } }