【发布时间】:2019-01-29 19:07:20
【问题描述】:
我有一个 Android 项目,我正在使用 following approach 更改在我的一个视图中发生的波纹效果的颜色
虽然它可以工作,但我确实需要根据我的风格将此颜色重置为默认值。 下面我将展示我的 Style 文件,我希望有一种方法可以将 RippleDrawable 设置回它的默认颜色。
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="editTextColor">@android:color/white</item>
<item name="font">@font/roboto_regular</item>
<item name="colorControlNormal">@android:color/white</item>
<item name="android:scrollbarThumbVertical">@drawable/nice_scrollbar</item>
<item name="android:textColor">@color/darkGrey</item>
<item name="android:editTextColor">@android:color/black</item>
<item name="android:textColorHighlight">@color/colorPrimary</item>
<item name="android:colorBackground">@color/darkerWhite</item>
<item name="android:windowBackground">@color/darkerWhite</item>
<item name="windowNoTitle">true</item>
<item name="android:windowNoTitle">true</item>
</style>
可以看出,我正在使用 MaterialComponents。
您将在下面找到我用来更改颜色以及在给定 x/y 处对视图施加波纹效果的当前方法:
private void forceRippleAnimation(View view, int x, int y) {
Drawable background = view.getBackground();
if (Build.VERSION.SDK_INT >= 21 && background instanceof RippleDrawable) {
RippleDrawable rippleDrawable = (RippleDrawable) background;
rippleDrawable.setHotspot(x, y);
rippleDrawable.setColor(new ColorStateList(
new int[][]{
new int[]{}
},
new int[]{
getContext().getResources().getColor(R.color.rippleColor)
}
));
rippleDrawable.setState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled});
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override public void run(){
rippleDrawable.setState(view.getDrawableState());
}
}, 650);
}
}
【问题讨论】:
-
请展示您如何更改颜色 - 链接问题至少有两个有用的答案,我不知道哪一个在您眼中看起来最有希望。
-
你好@0X0nosugar,我刚刚编辑了我的问题,描述了我如何强制产生连锁反应。一种可能的方法是从 RippleDrawable 中找到初始 ColorStateList,但是没有 getColorStateList,也没有找到默认值的方法......
标签: java android material-components-android rippledrawable