@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; }
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); } }
/** * Sets a flag indicating whether this seek bar is <b>discrete</b> or not. * <p> * SeekBarWidget in the discrete mode draws, above the progress track and the thumb, a discrete * indicator to show to a user current progress value. Discrete indicator's drawable can be set * via {@link #setDiscreteIndicator(android.graphics.drawable.Drawable)}. * * @param discrete {@code True} to enable discrete mode, {@code false} otherwise. * @see R.attr#uiDiscrete ui:uiDiscrete * @see #isDiscrete() */ public void setDiscrete(boolean discrete) { if (discrete && UiConfig.MATERIALIZED) { final Drawable background = getBackground(); if (background instanceof RippleDrawable) { // This is a little bit harsh, but the RippleDrawable background is showing a ripple // in discrete mode in top left corner of this view's bounds which is kind of weird // behaviour. this.mRippleBackgroundDrawable = background; setBackgroundDrawable(null); } } else if (!discrete && mRippleBackgroundDrawable != null) { setBackgroundDrawable(mRippleBackgroundDrawable); this.mRippleBackgroundDrawable = null; } this.ensureDecorator(); if (mDecorator.hasPrivateFlag(PFLAG_DISCRETE) != discrete) { mDecorator.updatePrivateFlags(PFLAG_DISCRETE, discrete); this.updateThumb(mThumb); this.updateDiscreteIndicator(mDiscreteIndicator); requestLayout(); } }
private static void setupForeground(View view, Drawable drawable, Integer color, Integer alpha) { if (view instanceof IForeground) { if (color != null) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { if (drawable instanceof RippleDrawable) { RippleDrawable rippleDrawable = (RippleDrawable) drawable; rippleDrawable.setColor(ColorStateList.valueOf(color)); if (alpha != null) { rippleDrawable.setAlpha(alpha); } } } } ((IForeground) view).setForeground(drawable); } }
public void showStopwatchRipple() { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { View stopwatchView = findViewById(R.id.stopwatch); if(stopwatchView!=null) { RippleDrawable rd = (RippleDrawable) stopwatchView.getBackground(); rd.setState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }); rd.setState(new int[] { }); } } }); }
private Drawable createBackgroundDrawable(int width, int height) { if (isCircular && height > width) { width = height; } else if (isCircular && width > height) { height = width; } Drawable content = createContentDrawable(width, height); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Log.d(TAG, "RIPPLE APPLIED, with color: " + mRippleColor); Drawable mask = createMaskDrawable(width, height); ColorStateList stateList = ColorStateList.valueOf(mRippleColor); return new RippleDrawable(stateList, content, mask); } else { return content; } }
@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; }
@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); }
@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); }
@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); }
@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); }
/** * Creates a new {@link RippleDrawable} introduced in Lollipop. * * @param normalColor Color for the idle/normal state * @param rippleColor Color for the ripple effect * @param bounds Clipping bounds for the ripple state. Set to {@code null} to get a borderless ripple * @param cornerRadius Set to round the corners on rectangular drawables, 0 to disable * @return A fully colored RippleDrawable, new instance each time */ @NonNull @RequiresApi(Build.VERSION_CODES.LOLLIPOP) public static RippleDrawable createRippleDrawable(@ColorInt final int normalColor, @ColorInt final int rippleColor, @Nullable final Rect bounds, @IntRange(from = 0) final int cornerRadius) { Drawable maskDrawable = null; if (bounds != null) { // clip color is white maskDrawable = createColoredDrawable(Color.WHITE, bounds); if (maskDrawable instanceof GradientDrawable) { ((GradientDrawable) maskDrawable).setCornerRadius(cornerRadius); } maskDrawable.setBounds(bounds); } Drawable normalStateDrawable = null; // transparent has no idle state if (normalColor != Color.TRANSPARENT) { normalStateDrawable = createColoredDrawable(normalColor, bounds); if (normalStateDrawable instanceof GradientDrawable) { ((GradientDrawable) normalStateDrawable).setCornerRadius(cornerRadius); } } return new RippleDrawable(ColorStateList.valueOf(rippleColor), normalStateDrawable, maskDrawable); }
/** * 创建按钮并且设置对应功能 * * @param line * @param sc */ public void createBtnAndSetFunc(LinearLayout line, ShortCut sc) { int iconScale = DataHook.iconScale; LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); p.weight = 1; p.gravity = Gravity.CENTER; Context context = line.getContext(); ImageView btn = new ImageView(context); String iconPath = sc.getIconPath(); Bitmap iconBitmap = null; if (iconPath != null) { iconBitmap = ImageUtil.zoomBitmap(iconPath, iconScale); } if (iconBitmap == null) { iconBitmap = ImageUtil.byte2Bitmap(mMapImgRes.get(sc.getCode())); iconBitmap = ImageUtil.zommBitmap(iconBitmap, iconScale); } btn.setImageBitmap(iconBitmap); ColorStateList colorStateList = createColorStateList(0xffffffff, 0xffffff00, 0xff0000ff, 0xffff0000); RippleDrawable ripple = new RippleDrawable(colorStateList, null, null); btn.setBackground(ripple); btn.setScaleType(ImageView.ScaleType.CENTER); btn.setOnClickListener(getBtnFuncOfName(sc)); btn.setOnLongClickListener(getBtnLongFuncOfName(sc.getCode())); line.addView(btn, p); }
@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); } }
@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); } }
@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 (!isInEditMode() && 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; }
@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); } }
@Override void setBackgroundDrawable(ColorStateList backgroundTint, PorterDuff.Mode backgroundTintMode, int rippleColor, int borderWidth) { // Now we need to tint the shape background with the tint mShapeDrawable = DrawableCompat.wrap(createShapeDrawable()); DrawableCompat.setTintList(mShapeDrawable, backgroundTint); if (backgroundTintMode != null) { DrawableCompat.setTintMode(mShapeDrawable, backgroundTintMode); } final Drawable rippleContent; if (borderWidth > 0) { mBorderDrawable = createBorderDrawable(borderWidth, backgroundTint); rippleContent = new LayerDrawable(new Drawable[]{mBorderDrawable, mShapeDrawable}); } else { mBorderDrawable = null; rippleContent = mShapeDrawable; } mRippleDrawable = new RippleDrawable(ColorStateList.valueOf(rippleColor), rippleContent, null); mContentBackground = mRippleDrawable; mShadowViewDelegate.setBackgroundDrawable(mRippleDrawable); }
@Override public void onClick(View view) { if (mItem instanceof RadioButtonItem) { mAdapter.clearRadioGroup(mItem); } if (view.getBackground() instanceof RippleDrawable) { view.postDelayed(new Runnable() { @Override public void run() { if (mItem != null) { mItem.onSelected(); } } }, view.getResources().getInteger(R.integer.side_panel_ripple_anim_duration)); } else { mItem.onSelected(); } }
@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() && mUsingRipple) { 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) 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); }
@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); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public void setRipple(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+"); } }