android Ripple effect 点击 水波效果 自定义背景颜色

自动转换的点击效果 设置一个normal颜色即可, 在5.0以上为 ripple, 在5.0一下表现为selector;

public class RippleDrawableUtils {

    /**
     * for general button with ripple above 5.0 and selector lower than.
     *
     * @param normalColorID id for normal color
     * @return backgroundDrawable
     */
    public static Drawable getButtonRippleBackground(
            int normalColorID) {

        float radius = HSDisplayUtils.dip2px(2);
        int normalColor = HSApplication.getContext().getResources().getColor(normalColorID);
        getRippleColor(normalColor);

        return getCompatRippleDrawable(normalColor, getRippleColor(normalColor), radius);
    }

    public static Drawable getCompatRippleDrawable(
            int normalColor, int pressedColor, float radius) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return new RippleDrawable(getPressedColorSelector(normalColor, pressedColor),
                    getShapeDrawable(normalColor, radius), getRippleMask(normalColor));
        } else {
            return getStateListDrawable(normalColor, pressedColor, radius);
        }
    }

    private static int getRippleColor(int normalColor) {
        int r = (int) (((normalColor >> 16) & 0xFF) * 0.8);
        int g = (int) (((normalColor >> 8) & 0xFF) * 0.8);
        int b = (int) (((normalColor) & 0xFF) * 0.8);
        return Color.rgb(r, g, b);
    }


    private static ColorStateList getPressedColorSelector(int normalColor, int pressedColor) {
        return new ColorStateList(
                new int[][]
                        {
                                new int[]{android.R.attr.state_pressed},
                                new int[]{android.R.attr.state_focused},
                                new int[]{android.R.attr.state_activated},
                                new int[]{}
                        },
                new int[]
                        {
                                pressedColor,
                                pressedColor,
                                pressedColor,
                                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, float radius) {
        StateListDrawable states = new StateListDrawable();
        states.addState(new int[]{android.R.attr.state_pressed},
                getShapeDrawable(pressedColor, radius));
        states.addState(new int[]{android.R.attr.state_focused},
                getShapeDrawable(pressedColor, radius));
        states.addState(new int[]{android.R.attr.state_activated},
                getShapeDrawable(pressedColor, radius));
        states.addState(new int[]{},
                getShapeDrawable(normalColor, radius));
        return states;
    }

    private static GradientDrawable getShapeDrawable(int color, float radius) {
        GradientDrawable shape = new GradientDrawable();
        shape.setCornerRadius(radius);
        shape.setColor(color);
        return shape;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值