@TargetApi(Build.VERSION_CODES.LOLLIPOP) private Drawable getRippleDrawable(Drawable defaultDrawable, Drawable focusDrawable, Drawable disabledDrawable) { if (!mEnabled) { return disabledDrawable; } else { return new RippleDrawable(ColorStateList.valueOf(mFocusBackgroundColor), defaultDrawable, focusDrawable); } }
RippleDrawable rippleDrawable = (RippleDrawable)view.getBackground(); // assumes bg is a RippleDrawable int[][] states = new int[][] { new int[] { android.R.attr.state_enabled} }; int[] colors = new int[] { Color.BLUE }; // sets the ripple color to blue ColorStateList colorStateList = new ColorStateList(states, colors); rippleDrawable.setColor(colorStateList);
@TargetApi(Build.VERSION_CODES.LOLLIPOP) void onActionUp() { if (mBackgroundDrawable instanceof StateListDrawable) { StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable; drawable.setState(new int[]{android.R.attr.state_enabled}); } else if (Util.hasLollipop()) { RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable; ripple.setState(new int[]{android.R.attr.state_enabled}); ripple.setHotspot(calculateCenterX(), calculateCenterY()); ripple.setVisible(true, true); } }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static Drawable generateRippleDrawable(final int color, Rect bounds) { ColorStateList list = ColorStateList.valueOf(color); Drawable mask = generateCircleDrawable(Color.WHITE); RippleDrawable rippleDrawable = new RippleDrawable(list, null, mask); // API 21 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) { rippleDrawable.setBounds(bounds); } // API 22. Technically harmless to leave on for API 21 and 23, but not worth risking for 23+ if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) { int center = (bounds.left + bounds.right) / 2; rippleDrawable.setHotspotBounds(center, bounds.top, center, bounds.bottom); } return rippleDrawable; }
RippleDrawable ripple = new RippleDrawable( new ColorDrawable(Color.WHITE), null); ripple.setColor(myList);
assert ripple != null; ripple.setColor(ColorStateList.valueOf(pressed.color)); ripple.setDrawableByLayerId(android.R.id.mask, normal.drawable); // 这里用的normal时的drawable做边界 view.setBackground(selector); } else { ripple.setDrawableByLayerId(android.R.id.content, selector); view.setBackground(ripple);
public static RippleDrawable createRipple(@ColorInt int color, @FloatRange(from = 0f, to = 1f) float alpha, boolean bounded) { color = ColorUtils.modifyAlpha(color, alpha); return new RippleDrawable(ColorStateList.valueOf(color), null, bounded ? new ColorDrawable(Color.WHITE) : null); }
ColorStateList csl = ColorStateList.valueOf(Color.BLUE); RippleDrawable d = new RippleDrawable(csl, null, null); // Change the color, if desired. ColorStateList otherCsl = ColorStateList.valueOf(Color.RED); d.setColor(otherCsl);
@TargetApi(Build.VERSION_CODES.LOLLIPOP) void onActionDown() { if (mBackgroundDrawable instanceof StateListDrawable) { StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable; drawable.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}); } else if (Util.hasLollipop()) { RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable; ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}); ripple.setHotspot(calculateCenterX(), calculateCenterY()); ripple.setVisible(true, true); } }
public static RippleDrawable createRipple(@ColorInt int color, @FloatRange(from = 0f, to = 1f) float alpha, boolean bounded) { color = ColorUtils.modifyAlpha(color, alpha); return new RippleDrawable(ColorStateList.valueOf(color), null, bounded ? new ColorDrawable(Color.WHITE) : null); }
RippleDrawable rippleDrawable = (RippleDrawable)view.getBackground(); // assumes bg is a RippleDrawable int[][] states = new int[][] { new int[] { android.R.attr.state_enabled} }; int[] colors = new int[] { Color.BLUE }; // sets the ripple color to blue ColorStateList colorStateList = new ColorStateList(states, colors); rippleDrawable.setColor(colorStateList);
@TargetApi(Build.VERSION_CODES.LOLLIPOP) void onActionDown() { if (mUsingStyle) { mBackgroundDrawable = getBackground(); } if (mBackgroundDrawable instanceof StateListDrawable) { StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable; drawable.setState(new int[]{android.R.attr.state_pressed}); } else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) { RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable; ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}); ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2); ripple.setVisible(true, true); } // setPressed(true); }
/** * helper to create an ripple drawable with the given normal and pressed color * * @param normalColor the normal color * @param pressedColor the pressed color * @param radius the button radius * @return the ripple drawable */ public static Drawable getRippleDrawable(@ColorInt int normalColor, @ColorInt int pressedColor, int radius) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return new RippleDrawable(ColorStateList.valueOf(pressedColor), new ColorDrawable(normalColor), getRippleMask(normalColor, radius)); } else { return getStateListDrawable(normalColor, pressedColor); } }
@Override public void onBindViewHolder(ViewHolder holder, int position) { Drawable drawable = holder.getBackground(); if (drawable instanceof GradientDrawable) { GradientDrawable gd = (GradientDrawable) drawable.getCurrent(); gd.setColor(Color.parseColor(colors[position])); } else if (drawable instanceof RippleDrawable) { RippleDrawable rd = (RippleDrawable) drawable; int color = Color.parseColor(colors[position]); rd.setColor(newColorStateList(color)); } }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) void onActionUp() { if (mUsingStyle) { mBackgroundDrawable = getBackground(); } if (mBackgroundDrawable instanceof StateListDrawable) { StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable; drawable.setState(new int[]{}); } else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) { RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable; ripple.setState(new int[]{}); ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2); ripple.setVisible(true, true); } // setPressed(false); }
MaterialItemView v = mItems.get(i); if (Build.VERSION.SDK_INT >= 21) { v.setBackground(new RippleDrawable(new ColorStateList(new int[][]{{}}, new int[]{0xFFFFFF & checkedColors.get(i) | 0x56000000}), null, null)); } else { v.setBackgroundResource(R.drawable.material_item_background);
rd.setColor(newColorStateList(color));
@TargetApi(Build.VERSION_CODES.LOLLIPOP) void onActionUp() { if (mBackgroundDrawable instanceof StateListDrawable) { StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable; drawable.setState(new int[]{android.R.attr.state_enabled}); } else if (Util.hasLollipop()) { RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable; ripple.setState(new int[]{android.R.attr.state_enabled}); ripple.setHotspot(calculateCenterX(), calculateCenterY()); ripple.setVisible(true, true); } }
public void initialization(String title, Drawable drawable, Drawable checkedDrawable, boolean tintIcon, int color, int checkedColor) { mTitle = title; mDefaultColor = color; mCheckedColor = checkedColor; mTintIcon = tintIcon; if (mTintIcon) { mDefaultDrawable = Utils.tinting(drawable, mDefaultColor); mCheckedDrawable = Utils.tinting(checkedDrawable, mCheckedColor); } else { mDefaultDrawable = drawable; mCheckedDrawable = checkedDrawable; } mIcon.setImageDrawable(mDefaultDrawable); if (Build.VERSION.SDK_INT >= 21) { setBackground(new RippleDrawable(new ColorStateList(new int[][]{{}}, new int[]{0xFFFFFF & checkedColor | 0x56000000}), null, null)); } else { setBackgroundResource(R.drawable.material_item_background); } }
public static void setRippleColorForCardView(CardView cardView, int color) { if (DeviceUtil.hasLollipopApi()) { RippleDrawable rp = (RippleDrawable) cardView.getForeground(); rp.setColor(ColorStateList.valueOf(color)); } else { if (sSldMap == null) { sSldMap = new SparseArray<>(); } StateListDrawable sld = sSldMap.get(color); if (sld == null) { sld = new StateListDrawable(); sld.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(color)); sld.addState(new int[]{-android.R.attr.state_pressed}, new ColorDrawable(Color.TRANSPARENT)); sSldMap.put(color, sld); } cardView.setForeground(sld); } }