下面列出了怎么用android.graphics.drawable.RippleDrawable的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings("WeakerAccess")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public Slice setRipple(@ColorInt final int mask) {
if (SDK_LOLLIPOP) {
if (mask != 0) {
ShapeDrawable shape = new ShapeDrawable(new Shape() {
@Override
public void draw(Canvas canvas, Paint paint) {
paint.setColor(mask);
canvas.drawPath(((CustomRoundRectDrawable) drawable).buildConvexPath(), paint);
}
});
RippleDrawable ripple = new RippleDrawable(buildColorStateList(mask), drawable, shape);
view.setBackground(ripple);
} else {
view.setBackground(drawable);
}
} else {
Log.i(TAG, "setRipple() only work for API 21+");
}
return this;
}
public Drawable build() {
GradientDrawable drawable = null;
StateListDrawable stateListDrawable = null;
if (hasSelectDrawable) {
stateListDrawable = getStateListDrawable();
} else {
drawable = getGradientDrawable();
}
if (rippleEnable && rippleColor != null) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Drawable contentDrawable = (stateListDrawable == null ? drawable : stateListDrawable);
return new RippleDrawable(ColorStateList.valueOf(rippleColor), contentDrawable, contentDrawable);
} else {
StateListDrawable resultDrawable = new StateListDrawable();
GradientDrawable unPressDrawable = getGradientDrawable();
unPressDrawable.setColor(rippleColor);
resultDrawable.addState(new int[]{-android.R.attr.state_pressed}, drawable);
resultDrawable.addState(new int[]{android.R.attr.state_pressed}, unPressDrawable);
return resultDrawable;
}
}
return drawable == null ? stateListDrawable : drawable;
}
public Drawable build() {
GradientDrawable drawable = null;
StateListDrawable stateListDrawable = null;
if (hasSelectDrawable) {
stateListDrawable = getStateListDrawable();
} else {
drawable = getGradientDrawable();
}
if (rippleEnable && rippleColor != null) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Drawable contentDrawable = (stateListDrawable == null ? drawable : stateListDrawable);
return new RippleDrawable(ColorStateList.valueOf(rippleColor), contentDrawable, contentDrawable);
} else {
StateListDrawable resultDrawable = new StateListDrawable();
GradientDrawable unPressDrawable = getGradientDrawable();
unPressDrawable.setColor(rippleColor);
resultDrawable.addState(new int[]{-android.R.attr.state_pressed}, drawable);
resultDrawable.addState(new int[]{android.R.attr.state_pressed}, unPressDrawable);
return resultDrawable;
}
}
return drawable == null ? stateListDrawable : drawable;
}
@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;
}
@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);
}
public void addToBeRipple(int drawable,View... v) {
if(first_time) {
first_time = false;
res = getResources();
}
int l = v.length;
rippleViews.addAll(Arrays.asList(v));
for(View view :v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.setBackground( (RippleDrawable) res.getDrawable(drawable));
} else {
//TODO: setBackground below Android L
}
view.setClickable(true);
}
}
@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);
}
/**
* 给View设置背景色
*
* @param view
* @param color
*/
private void setBackgroundSelector(View view, int color) {
Drawable selector = createSelector(color);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int[][] states = new int[][]{
new int[]{-android.R.attr.state_pressed},
new int[]{android.R.attr.state_pressed}
};
int[] colors = new int[]{
shiftColor(color),
color
};
ColorStateList rippleColors = new ColorStateList(states, colors);
setBackgroundCompat(view, new RippleDrawable(rippleColors, selector, null));
} else {
setBackgroundCompat(view, selector);
}
}
@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;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{-android.R.attr.state_enabled}, createCircleDrawable(mColorDisabled));
drawable.addState(new int[]{android.R.attr.state_pressed}, createCircleDrawable(mColorPressed));
drawable.addState(new int[]{}, createCircleDrawable(mColorNormal));
if (Util.hasLollipop()) {
RippleDrawable ripple = new RippleDrawable(new ColorStateList(new int[][]{{}},
new int[]{mColorRipple}), drawable, null);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
});
setClipToOutline(true);
mBackgroundDrawable = ripple;
return ripple;
}
mBackgroundDrawable = drawable;
return drawable;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{android.R.attr.state_pressed}, createRectDrawable(mColorPressed));
drawable.addState(new int[]{}, createRectDrawable(mColorNormal));
if (Util.hasLollipop()) {
RippleDrawable ripple = new RippleDrawable(new ColorStateList(new int[][]{{}},
new int[]{mColorRipple}), drawable, null);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
});
setClipToOutline(true);
mBackgroundDrawable = ripple;
return ripple;
}
mBackgroundDrawable = drawable;
return drawable;
}
/**
* Sets the color of the halo.
*
* @see #getHaloTintList()
* @attr ref com.google.android.material.R.styleable#Slider_haloColor
*/
public void setHaloTintList(@NonNull ColorStateList haloColor) {
if (haloColor.equals(this.haloColor)) {
return;
}
this.haloColor = haloColor;
Drawable background = getBackground();
if (!shouldDrawCompatHalo() && background instanceof RippleDrawable) {
((RippleDrawable) background).setColor(haloColor);
return;
}
haloPaint.setColor(getColorForState(haloColor));
haloPaint.setAlpha(HALO_ALPHA);
invalidate();
}
@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;
}
@SuppressLint("NewApi")
private void update(@ColorInt int color) {
innerPaint.setColor(color);
outerPaint.setColor(shiftColorDown(color));
Drawable selector = createSelector(color);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int[][] states = new int[][]{
new int[]{android.R.attr.state_pressed}
};
int[] colors = new int[]{shiftColorUp(color)};
ColorStateList rippleColors = new ColorStateList(states, colors);
setForeground(new RippleDrawable(rippleColors, selector, null));
} else {
setForeground(selector);
}
}
@SuppressWarnings("WeakerAccess")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public Slice setRipple(@ColorInt final int mask) {
if (SDK_LOLLIPOP) {
if (mask != 0) {
ShapeDrawable shape = new ShapeDrawable(new Shape() {
@Override
public void draw(Canvas canvas, Paint paint) {
paint.setColor(mask);
canvas.drawPath(((CustomRoundRectDrawable) drawable).buildConvexPath(), paint);
}
});
RippleDrawable ripple = new RippleDrawable(buildColorStateList(mask), drawable, shape);
view.setBackground(ripple);
} else {
view.setBackground(drawable);
}
} else {
Log.i(TAG, "setRipple() only work for API 21+");
}
return this;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{android.R.attr.state_pressed}, createRectDrawable(mColorPressed));
drawable.addState(new int[]{}, createRectDrawable(mColorNormal));
if (Util.hasLollipop()) {
RippleDrawable ripple = new RippleDrawable(new ColorStateList(new int[][]{{}},
new int[]{mColorRipple}), drawable, null);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
});
setClipToOutline(true);
mBackgroundDrawable = ripple;
return ripple;
}
mBackgroundDrawable = drawable;
return drawable;
}
private void update(@ColorInt int color) {
innerPaint.setColor(color);
outerPaint.setColor(shiftColorDown(color));
Drawable selector = createSelector(color);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int[][] states = new int[][]{
new int[]{android.R.attr.state_pressed}
};
int[] colors = new int[]{shiftColorUp(color)};
ColorStateList rippleColors = new ColorStateList(states, colors);
setForeground(new RippleDrawable(rippleColors, selector, null));
} else {
setForeground(selector);
}
}
/**
* 按下与选中触摸效果
*/
public static StateListDrawable getPressAndSelectedStateListRippleDrawable(Context context,
Drawable selectDrawable,
Drawable defaultDrawable,
@ColorInt int rippleColor) {
StateListDrawable stateListDrawable = new StateListDrawable();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
RippleDrawable rippleDrawable = new RippleDrawable(ColorStateList.valueOf(rippleColor),
defaultDrawable, null);
stateListDrawable.addState(new int[]{android.R.attr.state_selected}, selectDrawable);
stateListDrawable.addState(new int[]{}, rippleDrawable);
return stateListDrawable;
} else {
stateListDrawable.addState(new int[]{android.R.attr.state_selected}, selectDrawable);
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, selectDrawable);
stateListDrawable.addState(new int[]{}, defaultDrawable);
return stateListDrawable;
}
}
/**
* Sets the color on the {@code view}'s {@code selectableItemBackground} or the
* borderless variant, whichever was set as the background.
* @param view the view that should have its highlight color changed
*/
public static void setColorControlHighlight(@NonNull View view, @ColorInt int color) {
Drawable selectableItemBackground = view.getBackground();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& selectableItemBackground instanceof RippleDrawable) {
((RippleDrawable) selectableItemBackground).setColor(ColorStateList.valueOf(color));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Utils.isTv(view.getContext())) {
((RippleDrawable) selectableItemBackground).setRadius(72);
}
} else {
// Draws the color (src) onto the background (dest) *in the same plane*.
// That means the color is not overlapping (i.e. on a higher z-plane, covering)
// the background. That would be done with SRC_OVER.
// The DrawableCompat tinting APIs *could* be a viable alternative, if you
// call setTintMode(). Previous attempts using those APIs failed without
// the tint mode. However, those APIs have the overhead of mutating and wrapping
// the drawable.
selectableItemBackground.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
}
private void forceRippleAnimation(View view, MotionEvent event) {
Drawable background = view.getBackground();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && background instanceof RippleDrawable) {
final RippleDrawable rippleDrawable = (RippleDrawable) background;
rippleDrawable.setState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled});
rippleDrawable.setHotspot(event.getX(), event.getY());
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
rippleDrawable.setState(new int[]{});
}
}, 300);
}
}
private void addIconTab(final int position, Drawable drawable, CharSequence contentDescription) {
ImageView tab = new ImageView(getContext()) {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (pager.getAdapter() instanceof IconTabProvider) {
((IconTabProvider) pager.getAdapter()).customOnDraw(canvas, position);
}
}
@Override
public void setSelected(boolean selected) {
super.setSelected(selected);
Drawable background = getBackground();
if (Build.VERSION.SDK_INT >= 21 && background != null) {
int color = Theme.getColor(selected ? Theme.key_chat_emojiPanelIconSelected : Theme.key_chat_emojiBottomPanelIcon);
Theme.setSelectorDrawableColor(background, Color.argb(30, Color.red(color), Color.green(color), Color.blue(color)), true);
}
}
};
tab.setFocusable(true);
if (Build.VERSION.SDK_INT >= 21) {
RippleDrawable rippleDrawable = (RippleDrawable) Theme.createSelectorDrawable(Theme.getColor(Theme.key_chat_emojiBottomPanelIcon));
Theme.setRippleDrawableForceSoftware(rippleDrawable);
tab.setBackground(rippleDrawable);
}
tab.setImageDrawable(drawable);
tab.setScaleType(ImageView.ScaleType.CENTER);
tab.setOnClickListener(v -> {
if (pager.getAdapter() instanceof IconTabProvider) {
if (!((IconTabProvider) pager.getAdapter()).canScrollToTab(position)) {
return;
}
}
pager.setCurrentItem(position, false);
});
tabsContainer.addView(tab);
tab.setSelected(position == currentPosition);
tab.setContentDescription(contentDescription);
}
private Paint getRipplePaint() {
try {
Method method = RippleDrawable.class.getDeclaredMethod("getRipplePaint");
method.setAccessible(true);
return (Paint) method.invoke(this);
} catch (NoSuchMethodException
| InvocationTargetException
| IllegalAccessException ignored) {
}
// Normally should never happen.
return new Paint();
}
@NonNull
private Drawable createForegroundRippleDrawable() {
if (RippleUtils.USE_FRAMEWORK_RIPPLE) {
foregroundShapeDrawable = createForegroundShapeDrawable();
//noinspection NewApi
return new RippleDrawable(rippleColor, null, foregroundShapeDrawable);
}
return createCompatRippleDrawable();
}
/**
* Sets the color of the seekbar ripple
*
* @param drawable
* @param colorStateList The ColorStateList the track ripple will be changed to
*/
public static void setRippleColor(@NonNull Drawable drawable, ColorStateList colorStateList) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
((RippleDrawable) drawable).setColor(colorStateList);
} else {
((AlmostRippleDrawable) drawable).setColor(colorStateList);
}
}
@TargetApi(21)
protected void forceRippleAnimation(RippleDrawable background) {
final RippleDrawable rippleDrawable = background;
Handler handler = new Handler();
rippleDrawable.setState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled});
handler.postDelayed(() -> rippleDrawable.setState(new int[]{}), 1000);
}
@Override
public boolean shouldHandleTouchEvent(MotionEvent event) {
return Build.VERSION.SDK_INT >= LOLLIPOP
&& mDrawable != null
&& mDrawable instanceof RippleDrawable
&& event.getActionMasked() == MotionEvent.ACTION_DOWN
&& getBounds().contains((int) event.getX(), (int) event.getY());
}
private void addIconTab(final int position, Drawable drawable, CharSequence contentDescription) {
ImageView tab = new ImageView(getContext()) {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (pager.getAdapter() instanceof IconTabProvider) {
((IconTabProvider) pager.getAdapter()).customOnDraw(canvas, position);
}
}
@Override
public void setSelected(boolean selected) {
super.setSelected(selected);
Drawable background = getBackground();
if (Build.VERSION.SDK_INT >= 21 && background != null) {
int color = Theme.getColor(selected ? Theme.key_chat_emojiPanelIconSelected : Theme.key_chat_emojiBottomPanelIcon);
Theme.setSelectorDrawableColor(background, Color.argb(30, Color.red(color), Color.green(color), Color.blue(color)), true);
}
}
};
tab.setFocusable(true);
if (Build.VERSION.SDK_INT >= 21) {
RippleDrawable rippleDrawable = (RippleDrawable) Theme.createSelectorDrawable(Theme.getColor(Theme.key_chat_emojiBottomPanelIcon));
Theme.setRippleDrawableForceSoftware(rippleDrawable);
tab.setBackground(rippleDrawable);
}
tab.setImageDrawable(drawable);
tab.setScaleType(ImageView.ScaleType.CENTER);
tab.setOnClickListener(v -> {
if (pager.getAdapter() instanceof IconTabProvider) {
if (!((IconTabProvider) pager.getAdapter()).canScrollToTab(position)) {
return;
}
}
pager.setCurrentItem(position, false);
});
tabsContainer.addView(tab);
tab.setSelected(position == currentPosition);
tab.setContentDescription(contentDescription);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mTextAndBackground = (ViewGroup) findViewById(R.id.text_and_background);
ColorDrawable colorBackground = (ColorDrawable) mTextAndBackground.getBackground();
mBackgroundColor = colorBackground.getColor();
RippleDrawable rippleBackground = new RippleDrawable(ColorStateList.valueOf(
ThemeUtils.getAttrColor(getContext(), android.R.attr.colorControlHighlight)),
colorBackground, null);
mTextAndBackground.setBackground(rippleBackground);
mTitleView = (TextView) mTextAndBackground.findViewById(R.id.title);
mTextView = (TextView) mTextAndBackground.findViewById(R.id.text);
}
/**
* Sets the color of the seekbar ripple
* @param drawable
* @param colorStateList The ColorStateList the track ripple will be changed to
*/
public static void setRippleColor(@NonNull Drawable drawable, ColorStateList colorStateList) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
((RippleDrawable) drawable).setColor(colorStateList);
} else {
((AlmostRippleDrawable) drawable).setColor(colorStateList);
}
}
@Override
public void onPaletteChanged() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
((RippleDrawable)mPlayPauseButton.getBackground()).setColor(ColorStateList.valueOf(Tool.getBaseColor()));
((RippleDrawable)mPrevButton.getBackground()).setColor(ColorStateList.valueOf(Tool.getBaseColor()));
((RippleDrawable)mMenuButton.getBackground()).setColor(ColorStateList.valueOf(Tool.getBaseColor()));
((RippleDrawable)mNextButton.getBackground()).setColor(ColorStateList.valueOf(Tool.getBaseColor()));
((RippleDrawable)mPlaylistTitle.getBackground()).setColor(ColorStateList.valueOf(Tool.getBaseColor()));
}
onColorPaletteReady(Tool.ColorOne,Tool.ColorTwo,Tool.AlphaOne,Tool.AlphaTwo);
}