android - 使用波纹效果更改 FAB 颜色

标签 android android-layout floating-action-button rippledrawable

在 Android 中,我想做这样的事情(但是有 2 种交替的颜色,黑色和白色:

像这样用波纹效果改变颜色

enter image description here

我尝试做的是:

1) 通过 XML 设置默认的 backgroundTint 和波纹颜色

app:backgroundTint="@android:color/black"
app:rippleColor="@android:color/white"

2) onclick方法中,将backgroundTint改为白色,波纹颜色改为黑色

为初始颜色设置一个字符串,即 high_color = "black"。那么,

fab.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onClick(View v) {
            if(high_color.equals("black")){
                fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.white)));
                fab.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.black)));
                fab.setRippleColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
                high_color = "white";
            }else {
                fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.black)));
                fab.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.white)));
                fab.setRippleColor(ContextCompat.getColor(getApplicationContext(), R.color.whites));
                high_color = "black";
            }
        }
    });

现在我得到这样的东西:

我得到的是这个

enter image description here

有没有办法让这个看起来像第一个?比如减慢波纹动画速度或类似的东西?

最佳答案

好吧,从来没有在 FAB 上尝试过这个,但是为了简单起见,我已经在 ImageButton 上实现了相同的功能,如下所示并且它有效。希望能帮助到你。这是针对大于 21 (Lollipop) 的 API。默认情况下,我的 ImageButton 的静止高度为 6dp,触摸时它会升高到 18dp(6dp + 12dp),如果需要,您可以在 lift_on_touch.xml 中更改它.此外,我正在使用 StateListAnimator,它会根据 ImageButton 的状态变化而变化。

<ImageButton
    android:id="@+id/share_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:background="@drawable/ripples_on_touch"
    android:elevation="6dp"
    android:padding="16dp"
    android:src="@drawable/ic_share_white_24dp"
    android:stateListAnimator="@animator/lift_on_touch"
    tools:targetApi="lollipop" />

v21/ripples_on_touch.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#F83E0ACC"
tools:targetApi="lollipop">
<item>
    <shape android:shape="oval">
        <solid android:color="#FFFF6F00" />
    </shape>
</item>
</ripple>

v21/lift_on_touch.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <set>
        <objectAnimator
            android:duration="@android:integer/config_longAnimTime"
            android:propertyName="scaleX"
            android:valueTo="1.250"
            android:valueType="floatType" />
        <objectAnimator
            android:duration="@android:integer/config_longAnimTime"
            android:propertyName="scaleY"
            android:valueTo="1.250"
            android:valueType="floatType" />
        <objectAnimator
            android:duration="@android:integer/config_longAnimTime"
            android:propertyName="translationZ"
            android:valueTo="12dp"
            android:valueType="floatType" />
    </set>
</item>

<item>
    <set>
        <objectAnimator
            android:duration="@android:integer/config_longAnimTime"
            android:propertyName="scaleX"
            android:valueTo="1.0"
            android:valueType="floatType" />
        <objectAnimator
            android:duration="@android:integer/config_longAnimTime"
            android:propertyName="scaleY"
            android:valueTo="1.0"
            android:valueType="floatType" />
        <objectAnimator
            android:duration="@android:integer/config_longAnimTime"
            android:propertyName="translationZ"
            android:valueTo="0dp"
            android:valueType="floatType" />
    </set>
</item>
</selector>

关于android - 使用波纹效果更改 FAB 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41871943/

相关文章:

android - Lollipop 前的 FloatingActionButton 阴影

android - 如何更改 FAB 背景颜色

android - 在 AudioTrack 上播放非阻塞数据

java - Android:使用 Intent 和 FileProvider 打开 Word 文档

java - 发送带有 boolean 值的模型类以改进有时不接受该 boolean 值的端点

android - 如何创建带有高程的 android 按钮背景 svg?

安卓注解

EditText 中的 Android 文本光标

android - 覆盖后退按钮默认 Activity 堆栈操作

android - 单击时切换 FloatingActionButton 图标