以编程方式设置 RippleDrawable 角半径

Sea*_*rzi 5 android drawable rippledrawable

我在RippleDrawable下面创建了一个喜欢。但是我无法更改 RippleDrawable 的圆角半径。它没有像setCornerRadii(float[] f).

public static RippleDrawable getPressedColorRippleDrawable(int normalColor, int pressedColor) {
    if (Build.VERSION.SDK_INT>=21) {
        RippleDrawable rippleDrawable = new RippleDrawable(getPressedColorSelector(normalColor, pressedColor), getColorDrawableFromColor(normalColor), null);
        //rippleDrawable.setRadius((int) Manager.convertDpToPixel(5));
        return rippleDrawable;
    }
    else
        return null;
}
Run Code Online (Sandbox Code Playgroud)

其他功能是

public 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
                    }
    );
}

public static ColorDrawable getColorDrawableFromColor(int color) {
    return new ColorDrawable(color);
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

Lau*_*ent 4

我遇到了和你一样的问题:如何设置RippleDrawable的角半径。

一种简单的继续方法是使用GradientDrawable. 您可以设置半径setCornerRadius,然后将配置的实例作为RippleDrawable构造函数的第二个参数传递。

这是一个例子:

ColorStateList pressedStates = ColorStateList.valueOf(Color.BLUE);

GradientDrawable contentDrawable = new GradientDrawable();
contentDrawable.setColor(Color.WHITE);
contentDrawable.setCornerRadius(16);

RippleDrawable rippleDrawable = new RippleDrawable(pressedStates, contentDrawable, null);
container.setBackground(rippleDrawable);
Run Code Online (Sandbox Code Playgroud)