首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编程设置RippleDrawable角半径

编程设置RippleDrawable角半径
EN

Stack Overflow用户
提问于 2017-05-29 15:14:21
回答 1查看 1.4K关注 0票数 5

我创建了如下所示的RippleDrawable。但我不能改变RippleDrawable的拐角半径。它没有像setCornerRadii(float[] f)这样的方法。

代码语言:javascript
复制
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;
}

其他的功能是

代码语言:javascript
复制
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);
}

我该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-01 16:00:59

我正面临着和你一样的问题:如何将拐角半径设置为RippleDrawable。

一种简单的处理方式是使用GradientDrawable。可以使用setCornerRadius设置radius,然后将配置的实例作为RippleDrawable构造函数的第二个参数传递。

下面是一个示例:

代码语言:javascript
复制
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);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44245757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档