【问题标题】:Set RippleDrawable corner radius programmatically以编程方式设置 RippleDrawable 圆角半径
【发布时间】:2017-05-29 15:14:21
【问题描述】:

我创建了一个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;
}

而其他功能是

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

我该怎么做?

【问题讨论】:

  • 为什么不能用xml做呢?
  • @billynomates '因为我正在从服务器检索 RGB 颜色代码以进行 A/B 测试

标签: android drawable rippledrawable


【解决方案1】:

我遇到了和你一样的问题:如何将圆角半径设置为 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);

【讨论】:

    猜你喜欢
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 2017-08-19
    • 2019-04-13
    • 2017-10-01
    • 1970-01-01
    相关资源
    最近更新 更多