本文介绍了以编程方式设置RippleDrawable拐角半径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建如下的RippleDrawable.但是我无法更改RippleDrawable的拐角半径.它没有像setCornerRadii(float[] f)这样的方法.

I create a RippleDrawable like below. But I can't change the corner radius of the RippleDrawable. It doesn't have a method like 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);
}

我该怎么办?

推荐答案

我正面临着与您相同的问题:如何将拐角半径设置为 RippleDrawable .

I was facing the same issue as you: how to set a corner radius to a RippleDrawable.

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

A simple manner to proceed is to use a GradientDrawable. You can set a radius with setCornerRadius and then pass the configured instance as the second parameter of the RippleDrawable constructor.

这里是一个例子:

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

这篇关于以编程方式设置RippleDrawable拐角半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:47