机器人如何在Java的连锁反应连锁反应、机器人、如何在、Java

2023-09-07 14:23:18 作者:花若怜、落在谁的指尖

我有波纹XML。但我不知道怎么去在Java中相同的效果。

I have the ripple xml. But I am not sure how to get same effect in Java.

<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/blue_1"
/>

我想定义/创建/加载的波动,甚至是设置在Java中的颜色。 这意味着,在Java中,我可以加载XML波纹,然后分配一个颜色。或者,我可以做尽一切的java:纹波=新纹波

I would like to define / create / load a ripple, or even set a color in Java. That means, in java, I can load the XML ripple, then assign a color. or can i do do everything in java: Ripple = new Ripple ?

我想从这个链接的code:https://github.com/romainguy/google-io-2014/blob/master/app/src/main/java/com/example/android/io2014/DetailActivity.java.

I tried the code from this link: https://github.com/romainguy/google-io-2014/blob/master/app/src/main/java/com/example/android/io2014/DetailActivity.java.

有一个名为colorRipple方法。

There's a method called 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));
}

我试过code以上,但它给我NPE。

I tried the code above but it give me NPE.

推荐答案

您可以创建或使用类似修改 RippleDrawable 在运行时:

You can create or modify a RippleDrawable at run time using something like:

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