android – 用涟漪效果改变FAB颜色

android – 用涟漪效果改变FAB颜色,第1张

概述在 Android中,我想做这样的事情(但有2种交替颜色黑白: 像这样用涟漪效果改变颜色 我试图做的是: 1)设置默认backgroundTint&通过XML涟漪颜色 app:backgroundTint="@android:color/black"app:rippleColor="@android:color/white" 2)在onclick方法中,将backgroundTint更改为白色并 在 Android中,我想做这样的事情(但有2种交替颜色黑白:

像这样用涟漪效果改变颜色

我试图做的是:

1)设置默认backgroundTint&通过XML涟漪颜色

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

现在我得到这样的东西:

我得到的是这个

无论如何要让这个看起来像第一个?比如减慢涟漪动画的速度或类似的东西?

解决方法 好吧,从来没有在FAB上试过这个,但为了简单起见,我在Imagebutton上实现了相同的功能,如下所示,它可以工作.希望能帮助到你.这是针对API; s大于21(Lollipop).我的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颜色全部内容,希望文章能够帮你解决android – 用涟漪效果改变FAB颜色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1125992.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存