机器人如何在Java的连锁反应 [英] Android how to create ripple effect in Java

查看:213
本文介绍了机器人如何在Java的连锁反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有波纹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:<一href="https://github.com/romainguy/google-io-2014/blob/master/app/src/main/java/com/example/android/io2014/DetailActivity.java" rel="nofollow">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);

这篇关于机器人如何在Java的连锁反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆