我有波纹 xml。但我不确定如何在 Java 中获得相同的效果。
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/blue_1"
/>
我想定义/创建/加载波纹,甚至在Java中设置颜色。
这意味着,在 java 中,我可以加载 XML 波纹,然后分配颜色。或者我可以用java做所有事情:Ripple = new Ripple吗?
我尝试了此链接中的代码:https://github.com/romainguy/google-io-2014/blob/master/app/src/main/java/com/example/android/io2014/DetailActivity.java https://github.com/romainguy/google-io-2014/blob/master/app/src/main/java/com/example/android/io2014/DetailActivity.java.
有一种方法称为 colorRipple。
private void colorRipple(int id, int bgColor, int tintColor) {
View buttonView = findViewById(id);
RippleDrawable ripple = (RippleDrawable) buttonView.getBackground();
GradientDrawable rippleBackground = (GradientDrawable) ripple.getDrawable(0);
rippleBackground.setColor(bgColor);
ripple.setColor(ColorStateList.valueOf(tintColor));
}
我尝试了上面的代码,但它给了我 NPE。
您可以创建或修改RippleDrawable
在运行时使用类似的东西:
ColorStateList csl = ColorStateList.valueOf(Color.BLUE);
RippleDrawable d = new RippleDrawable(csl, null, null);
// Change the color, if desired.
ColorStateList otherCsl = ColorStateList.valueOf(Color.RED);
d.setColor(otherCsl);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)