android - 如何在带有 Android 5.0 Lollipop 的代码(不是 xml)中以编程方式使用 RippleDrawable?

标签 android android-5.0-lollipop rippledrawable

我的波纹有以下代码:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:id="@+id/rip">

        <shape android:shape="oval">
            <solid android:color="?android:colorAccent"/>
        </shape>
    </item>
</ripple>

现在我想让用户可以选择自己的颜色,所以我需要以编程方式创建波纹。
我找到了 this我认为这是正确的做法,但我不知道如何处理。

波纹将在这里使用:

<ImageButton
    android:id="@+id/add_button"
    android:layout_width="@dimen/diameter"
    android:layout_height="@dimen/diameter"
    android:layout_gravity="end|bottom"
    android:layout_marginBottom="@dimen/add_button_margin"
    android:layout_marginEnd="@dimen/add_button_margin"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:src="@drawable/ic_action_add_person"
    android:tint="@android:color/white"
    android:background="@drawable/oval_ripple"
    android:elevation="@dimen/elevation_low"
    android:stateListAnimator="@anim/button_elevation"
    android:contentDescription="Neuer Spieler" />

我需要像这样将背景设置为 RippleDrawable:

addButton.setBackground(ripple);

最佳答案

这就是我能够做到这一点的方式。

请注意,这只是 Api 21+,因此如果您支持较低版本,则必须回退到正常的 Drawable。

public static RippleDrawable getPressedColorRippleDrawable(int normalColor, int pressedColor)
{
    return new RippleDrawable(getPressedColorSelector(normalColor, pressedColor), getColorDrawableFromColor(normalColor), 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);
}

编辑: 我对此进行了更多修改,发现 ColorStateList 不需要像上述解决方案那样复杂。我已将其简化为以下代码段。 (上面代码块中的其他所有内容都是一样的。我只是更改了 ColorStateList 创建。)我将保留上面的代码块作为原始代码块,以防此方法不适用于某人。

new ColorStateList(
    new int[][]
        {
            new int[]{}
        },
    new int[]
        {
            pressedColor
        }
);

关于android - 如何在带有 Android 5.0 Lollipop 的代码(不是 xml)中以编程方式使用 RippleDrawable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27787870/

相关文章:

Android 纹波 : How do other apps make their ripple so transparent without affecting the original colour of the view

android - setOnEditorActionListener 不适用于 Android Lollipop

Android 5.0 RuntimeException with AppCompat Caused by : java. lang.IllegalStateException : You need to use a Theme. AppCompat 主题

java - Android - Java 中的 "android:theme="是什么?

java - setText 不适用于纵向布局(android)

android - ClassNotFoundException:找不到类 "android.os.PersistableBundle"Otto Android 5.0

Android 纹波和时序

java - Android:如何在另一个包的 Activity 中显示异步任务的进度对话框?

安卓图库组件

Android单选按钮取消选中