以编程方式设置 RippleDrawable 角半径
Set RippleDrawable corner radius programmatically
我创建了一个如下所示的 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);
}
我该怎么做?
我遇到了和你一样的问题:如何将角半径设置为 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);
我创建了一个如下所示的 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);
}
我该怎么做?
我遇到了和你一样的问题:如何将角半径设置为 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);