Android 用涟漪效应改变晶圆厂的颜色
在Android中,我想做类似的事情(但有两种交替的颜色,黑色和白色: 用这样的涟漪效果改变颜色 我想做的是: 1) 通过XML设置默认背景色调和波纹颜色Android 用涟漪效应改变晶圆厂的颜色,android,android-layout,floating-action-button,rippledrawable,Android,Android Layout,Floating Action Button,Rippledrawable,在Android中,我想做类似的事情(但有两种交替的颜色,黑色和白色: 用这样的涟漪效果改变颜色 我想做的是: 1) 通过XML设置默认背景色调和波纹颜色 app:backgroundTint="@android:color/black" app:rippleColor="@android:color/white" 2) 在onclick方法中,将背景色更改为白色,将波纹色更改为黑色 为初始颜色设置字符串,即high\u color=“black”。那么 fab.setOnClickList
app:backgroundTint="@android:color/black"
app:rippleColor="@android:color/white"
2) 在onclick方法中,将背景色更改为白色,将波纹色更改为黑色
为初始颜色设置字符串,即high\u 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";
}
}
});
现在我得到了这样的东西:
我得到的是这个
有没有办法让这个看起来像第一个?比如减慢ripple动画的速度或诸如此类的东西?我认为你应该使用选择器 选择器可以轻松地更改背景 以类似的方式使用选择器
将其用作FAB的背景
app:background=“@drawable/selector”
这将使白色作为背景,当它被点击。否则它将是黑色的
选择器中还有更多的android代码。请在android开发者网站上阅读选择器
如果这真的对你有帮助,请回复我。好吧,我从未在FAB上尝试过,但为了简单起见,我在ImageButton上实现了相同的功能,如下所示,它可以工作。希望能有帮助。这是针对API的;s大于21(棒棒糖)。默认情况下,My ImageButton的静止高度为6dp,触摸时,它将升高到18dp(6dp+12dp),如果需要,您可以在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/tripples\u on\u 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>
<?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>
v21/lift\u on\u 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>
<?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>
看看这个库:我觉得它不像是连锁反应。你试过这个吗?检查此项:。这个:啊,如果你想举个例子。。。查看我的回购协议,它完全符合您的要求,但在CustomView中,在这个文件中:尝试将您的涟漪颜色更改为透明。。。Idk,它可能会起作用。您甚至可以在Fabapp:rippleColor=“@android:Color/#999999”